gofiber/fiber · error

max constraint requires an argument

Error message

max constraint requires an argument

What it means

The max route-parameter constraint enforces that a matched path segment, parsed as an integer, is less than or equal to an upper bound. It is declared inline as :param<max(n)>. Its Analyze method requires exactly one argument; with zero arguments it returns this error. As with sibling constraints, fiber's internal newConstraint silently swallows the Analyze error, so this surfaces only when Analyze is called directly or via a delegating custom constraint.

Solutions

  1. Supply one integer argument: :param<max(100)>.
  2. For a string-length upper bound, use :param<maxLen(n)> instead.
  3. Confirm the argument parses as an integer (constraint.go:480).

Example fix

// before
app.Get("/n/:id<max>", h)
// after
app.Get("/n/:id<max(100)>", h)
Defensive patterns

Strategy: validation

Validate before calling

var maxNoArg = regexp.MustCompile(`:([^<]+)<max\(\s*\)>`)
for _, p := range routePatterns {
    if loc := maxNoArg.FindStringIndex(p); loc != nil {
        log.Fatalf("route %q: max constraint missing argument", p)
    }
}

Try / catch

typed, err := maxConstraint.Analyze(args)
if err != nil {
    return fmt.Errorf("route max constraint: %w", err)
}

Prevention

When it happens

Trigger: Calling Analyze on a max constraint with no arguments, derived from a route pattern such as :id<max()> or :id<max>. parseConstraintArgs returns the empty slice and len(args)==0 trips the guard at constraint.go:477.

Common situations: Omitting the bound when authoring a route; mixing up max (numeric) with maxLen (string-length); copy-paste error from another constraint.

Related errors


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

Appendix: 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 a105acad6c)