gofiber/fiber · error

parse constraint arg

Error message

parse constraint arg: %w

What it means

Thrown by the minLen route-parameter constraint's Analyze step at route-registration time. minLen expects one base-10 integer argument (the minimum character length of the param); strconv.Atoi rejects anything that is not a clean integer, and the error is wrapped as 'parse constraint arg: <Atoi error>'. Note: Fiber's built-in constraint registration swallows Analyze errors (see findConstraintHandler, constraint.go:188), so the user-visible symptom is a dead route whose constraint never matches, not a panic.

Solutions

  1. Change the minLen argument to an integer literal, e.g. "/:name<minLen(3)>".
  2. If the value is templated/computed, format it with %d or strconv.Itoa so only digits reach the pattern.
  3. Write a unit test that calls Analyze on the constraint args to catch malformed limits at build time.
  4. If the route seems to silently match nothing, suspect this: the swallowed Analyze error leaves typedData nil, Execute's data[0].(int) fails, and every request is rejected.

Example fix

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

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

Strategy: validation

Validate before calling

// validate a minLen argument is a clean integer before registering the route
func validIntArg(arg string) bool {
    _, err := strconv.Atoi(arg)
    return err == nil
}

// at startup, scan your route table's constraint args
for _, a := range minLenArgs {
    if !validIntArg(a) {
        log.Fatalf("minLen arg %q is not an integer", a)
    }
}

Type guard

null

Try / catch

// Analyze is the public seam; assert it succeeds in a test
_, err := minLenConstraintType{}.Analyze([]string{arg})
if err != nil {
    t.Fatalf("minLen arg %q: %v", arg, err)
}

Prevention

When it happens

Trigger: Registering a route whose minLen argument is non-integer, e.g. app.Get("/:name<minLen(three)>)", or "/:name<minLen(3.5)>", or "/:name<minLen(3px)>". Calling minLenConstraintType{}.Analyze([]string{"abc"}) directly (as the test suite does) returns this error explicitly.

Common situations: Typing the limit as a word instead of a digit, pasting a float or a value with units, leaving a placeholder from copied docs, or a tool generating route patterns with templated non-numeric values.

Related errors


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

Appendix: source

Thrown at constraint.go:341

	layout, ok := data[0].(string)
	if !ok || layout == "" {
		return false
	}
	_, err := time.Parse(layout, param)
	return err == nil
}

type minLenConstraintType struct{}

func (minLenConstraintType) Name() string { return ConstraintMinLen }
func (minLenConstraintType) Analyze(args []string) ([]any, error) {
	args = parseConstraintArgs(args)
	if len(args) == 0 {
		return nil, errors.New("minLen 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 (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 }

View on GitHub (pinned to a105acad6c)