gofiber/fiber · error
range constraint requires two arguments
Error message
range constraint requires two arguments
What it means
The range route-parameter constraint enforces that a matched path segment, parsed as an integer, lies within an inclusive [lo, hi] range. It is declared inline as :param<range(lo,hi)>. Its Analyze method requires two integer arguments; with fewer than two it returns this error. The internal newConstraint call site swallows Analyze errors, so this surfaces only when Analyze is invoked directly or through a delegating custom-constraint wrapper.
Solutions
- Provide two integer arguments: :param<range(1,100)>.
- If a single bound suffices, use :param<min(n)> or :param<max(n)>.
- Confirm the comma separator is not escaped (\, would merge arguments).
- For length-based ranges use :param<betweenLen(lo,hi)> instead.
Example fix
// before
app.Get("/n/:id<range(10)>", h)
// after
app.Get("/n/:id<range(10,100)>", h) Defensive patterns
Strategy: validation
Validate before calling
var rangeArgs = regexp.MustCompile(`:([^<]+)<range\(([^)]*)\)>`)
for _, p := range routePatterns {
m := rangeArgs.FindStringSubmatch(p)
if m != nil && len(strings.Split(m[2], ",")) != 2 {
log.Fatalf("route %q: range needs exactly two integer args", p)
}
} Try / catch
typed, err := rangeConstraint.Analyze(args)
if err != nil {
return fmt.Errorf("route range constraint: %w", err)
} Prevention
- Lint that range has exactly two comma-separated integer args.
- For length ranges use betweenLen; for one bound use min/max.
- Confirm no escaped comma (\,) is merging the two arguments.
When it happens
Trigger: Calling Analyze on a range constraint with fewer than two arguments, derived from a route pattern such as :id<range(5)>, :id<range()>, :id<range>, or :id<range(1\,10)> (escaped comma). parseConstraintArgs splits a single-element slice on the comma; without a comma it stays a one-element slice and len(args)<2 trips the guard at constraint.go:504.
Common situations: Forgetting the upper bound; an escaped comma merging two args into one; confusing range (numeric) with betweenLen (string-length).
Related errors
- betweenLen constraint requires two arguments
- max constraint requires an argument
- min constraint requires an argument
- regex constraint requires a pattern argument
- Domain pattern cannot be empty
AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11).
Data as JSON: /api/errors/0977b9b97e5991d2.
Report an issue: GitHub.
Appendix: source
Thrown at constraint.go:504
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
}
type rangeConstraintType struct{}
func (rangeConstraintType) Name() string { return ConstraintRange }
func (rangeConstraintType) Analyze(args []string) ([]any, error) {
args = parseConstraintArgs(args)
if len(args) < 2 {
return nil, errors.New("range constraint requires two arguments")
}
lo, err := strconv.Atoi(args[0])
if err != nil {
return nil, fmt.Errorf("parse constraint arg: %w", err)
}
hi, err := strconv.Atoi(args[1])
if err != nil {
return nil, fmt.Errorf("parse constraint arg: %w", err)
}
return []any{lo, hi}, nil
}
func (rangeConstraintType) Execute(param string, data []any) bool {
if len(data) < 2 {
return false
}
lo, ok := data[0].(int)
if !ok {View on GitHub (pinned to a105acad6c)