gofiber/fiber · error

regex constraint requires a pattern argument

Error message

regex constraint requires a pattern argument

What it means

Thrown by regexConstraintType.Analyze (constraint.go:540) when the 'regex' constraint is registered with no pattern argument. Analyze must compile the supplied pattern (via regexp.Compile or a custom RegexHandler) into a matcher; an empty argument gives nothing to compile. Unlike the numeric constraints, a regex with no pattern has no meaningful default, so this is an unambiguous misconfiguration.

Source

Thrown at constraint.go:540

	if !ok {
		return false
	}
	hi, ok := data[1].(int)
	if !ok {
		return false
	}
	num, err := strconv.Atoi(param)
	return err == nil && num >= lo && num <= hi
}

type regexConstraintType struct {
	regexHandler any
}

func (regexConstraintType) Name() string { return ConstraintRegex }
func (r regexConstraintType) Analyze(args []string) ([]any, error) {
	if len(args) == 0 {
		return nil, errors.New("regex constraint requires a pattern argument")
	}
	if r.regexHandler == nil {
		re, err := regexp.Compile(args[0])
		if err != nil {
			return nil, fmt.Errorf("parse constraint arg: %w", err)
		}
		return []any{re}, nil
	}
	matcher := compileRegex(r.regexHandler, args[0])
	return []any{matcher}, nil
}

func (regexConstraintType) Execute(param string, data []any) bool {
	if len(data) == 0 {
		return false
	}
	matcher, ok := data[0].(regexMatcher)
	if !ok || matcher == nil {

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Add a pattern, e.g. /:slug<regex(^[a-z0-9-]+$)>.
  2. When constructing patterns from config, fall back to a sensible default pattern instead of emitting an empty one.
  3. Unit-test route registration with a request that must match, so an empty regex is caught immediately.

Example fix

// before
app.Get("/:slug<regex>", handler)

// after
app.Get("/:slug<regex(^[a-z0-9-]+$)>", handler)
Defensive patterns

Strategy: validation

Validate before calling

// Reject empty regex constraints.
if regexp.MustCompile(`<regex\s*>`).MatchString(pattern) {
    return errors.New("regex constraint requires a pattern")
}

Prevention

When it happens

Trigger: A route like app.Get("/:slug<regex>", ...) with no pattern, or calling regexConstraintType{}.Analyze([]string{}). Also triggered if the pattern string is built dynamically and ends up empty.

Common situations: Treating 'regex' as argument-free (as 'alpha' or 'int' are), or stripping user input that produces an empty pattern. Copying a route and forgetting to fill in the regex are frequent.

Related errors


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