gofiber/fiber · error

range constraint requires two arguments

Error message

range constraint requires two arguments

What it means

Thrown by rangeConstraintType.Analyze (constraint.go:504) when the 'range' constraint is given fewer than two arguments. 'range(lo,hi)' validates that the integer parameter falls within an inclusive [lo,hi] interval, so Analyze needs both bounds (parsed with strconv.Atoi). A pattern like /:n<range> or /:n<range(5)> has an incomplete interval and cannot enforce a bounded check.

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 9a4c7e57fe)

Solutions

  1. Supply both bounds as a comma-separated pair, e.g. /:n<range(1,100)>.
  2. Confirm you used the comma separator and not a semicolon (semicolons separate distinct constraints, not range bounds).
  3. If generating patterns programmatically, assert len(args) >= 2 before emitting a range constraint.

Example fix

// before
app.Get("/:page<range(1)>", handler)

// after
app.Get("/:page<range(1,50)>", handler)
Defensive patterns

Strategy: validation

Validate before calling

// Ensure 'range' constraints carry two comma-separated bounds.
func checkRangeConstraint(pattern string) error {
    ranges := regexp.MustCompile(`<range\(([^)]*)\)>`).FindAllStringSubmatch(pattern, -1)
    for _, m := range ranges {
        parts := strings.Split(m[1], ",")
        if len(parts) < 2 {
            return fmt.Errorf("range(%s) needs two arguments", m[1])
        }
    }
    return nil
}

Prevention

When it happens

Trigger: Route patterns /:n<range>, /:n<range(5)>, or any range constraint where parseConstraintArgs yields fewer than two elements. Direct call rangeConstraintType{}.Analyze([]string{"1"}) returns it as well.

Common situations: Forgetting the second bound, using a wrong separator (the data separator is ','), or hand-constructing the args slice. Confusion between 'range' (numeric interval) and 'betweenLen' (length interval) also leads to wrong argument counts.

Related errors


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