gofiber/fiber · error

max constraint requires an argument

Error message

max constraint requires an argument

What it means

Thrown by maxConstraintType.Analyze (constraint.go:477) when the 'max' route-parameter constraint is registered without its required numeric argument. 'max(N)' enforces that the integer parameter is less-than-or-equal to N; without N there is no upper bound, so Analyze refuses to compile the constraint. As with all built-in constraint Analyze failures, route registration discards the error (newConstraint only stores typedData on success), meaning the route will compile but never match a request.

Source

Thrown at constraint.go:477

func (minConstraintType) Execute(param string, data []any) bool {
	if len(data) == 0 {
		return false
	}
	limit, ok := data[0].(int)
	if !ok {
		return false
	}
	num, err := strconv.Atoi(param)
	return err == nil && num >= limit
}

type maxConstraintType struct{}

func (maxConstraintType) Name() string { return ConstraintMax }
func (maxConstraintType) Analyze(args []string) ([]any, error) {
	args = parseConstraintArgs(args)
	if len(args) == 0 {
		return nil, errors.New("max 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 (maxConstraintType) Execute(param string, data []any) bool {
	if len(data) == 0 {
		return false
	}
	limit, ok := data[0].(int)
	if !ok {
		return false
	}
	num, err := strconv.Atoi(param)
	return err == nil && num <= limit

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Provide the upper bound, e.g. /:qty<max(100)> instead of /:qty<max>.
  2. Validate generated route patterns contain an argument for argument-required constraints before registering.
  3. Write a registration-time smoke test that hits the route to catch the silent no-match early.

Example fix

// before
app.Get("/:qty<max>", handler)

// after
app.Get("/:qty<max(100)>", handler)
Defensive patterns

Strategy: validation

Validate before calling

// Reject 'max' without an argument before app.Listen.
func checkMaxConstraint(pattern string) error {
    if regexp.MustCompile(`<max\s*>`).MatchString(pattern) {
        return errors.New("max constraint requires (N) argument")
    }
    return nil
}

Prevention

When it happens

Trigger: A route pattern such as app.Get("/:qty<max>", ...) missing the (N) bound, or calling maxConstraintType{}.Analyze([]string{}) directly. Custom constraints that delegate to max with an empty args slice also surface it.

Common situations: Mistakenly assuming 'max' works like 'int' (argument-free), or refactoring a route and dropping the number. Pattern strings built from configuration where the upper bound is missing are a common culprit.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/1c005ef6a063aced.json. Report an issue: GitHub.