gofiber/fiber · error

maxLen constraint requires an argument

Error message

maxLen constraint requires an argument

What it means

Returned by maxLenConstraintType.Analyze (constraint.go:362-364) when the maxLen constraint is declared without an integer argument. Symmetric with minLen: maxLen needs the maximum acceptable parameter length to enforce, and Analyze signals the route is malformed without it.

Solutions

  1. Supply an integer maximum: "/:name<maxLen(32)>".
  2. Use len for an exact length or betweenLen for a range.
  3. Write a registration test asserting the constraint compiles.

Example fix

// before
app.Get("/:name<maxLen>", handler)

// after
app.Get("/:name<maxLen(32)>", handler)
Defensive patterns

Strategy: validation

Validate before calling

func parseMaxLenArg(args []string) (int, error) {
    if len(args) == 0 { return 0, errors.New("maxLen constraint requires an argument") }
    return strconv.Atoi(args[0])
}

Try / catch

// Same registration-time test pattern as other constraints;
// see Test_ConstraintAnalyze_MissingArgs.

Prevention

When it happens

Trigger: Registering app.Get("/:name<maxLen>", ...) instead of "/:name<maxLen(32)>".

Common situations: Same family as the other length constraints — typo, copy/paste from a no-arg constraint like int, or docs that omitted the argument.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/0720070003093fba. Report an issue: GitHub.

Appendix: source

Thrown at constraint.go:363

func (minLenConstraintType) Execute(param string, data []any) bool {
	if len(data) == 0 {
		return false
	}
	limit, ok := data[0].(int)
	if !ok {
		return false
	}
	return len(param) >= limit
}

type maxLenConstraintType struct{}

func (maxLenConstraintType) Name() string { return ConstraintMaxLen }
func (maxLenConstraintType) Analyze(args []string) ([]any, error) {
	args = parseConstraintArgs(args)
	if len(args) == 0 {
		return nil, errors.New("maxLen constraint requires an argument")
	}
	n, err := strconv.Atoi(args[0])
	if err != nil {
		return nil, fmt.Errorf("parse constraint arg: %w", err)
	}
	return []any{n}, nil
}

func (maxLenConstraintType) Execute(param string, data []any) bool {
	if len(data) == 0 {
		return false
	}
	limit, ok := data[0].(int)
	if !ok {
		return false
	}
	return len(param) <= limit
}

View on GitHub (pinned to a105acad6c)