gofiber/fiber · error

regex constraint requires a pattern argument

Error message

regex constraint requires a pattern argument

What it means

The regex route-parameter constraint matches a path segment against a regular expression. It is declared inline as :param<regex(pattern)> and unlike the other built-ins it does NOT call parseConstraintArgs, so the raw args slice is inspected directly. Its Analyze method requires at least one argument (the pattern); with zero it returns this error. The internal newConstraint swallows Analyze errors, so this surfaces when Analyze is invoked directly or through a delegating custom-constraint wrapper.

Solutions

  1. Supply a valid regexp pattern: :param<regex(p([a-z]+)ch)>.
  2. Remember regex receives the raw args slice (no comma splitting), so a pattern containing a comma does not need escaping.
  3. If a RegexHandler is configured on the App, compileRegex is used instead of regexp.Compile (constraint.go:554); ensure the handler accepts the same pattern syntax.

Example fix

// before
app.Get("/x/:p<regex>", h)
// after
app.Get("/x/:p<regex(p([a-z]+)ch)>", h)
Defensive patterns

Strategy: validation

Validate before calling

var regexEmpty = regexp.MustCompile(`:([^<]+)<regex\(\s*\)>`)
for _, p := range routePatterns {
    if loc := regexEmpty.FindStringIndex(p); loc != nil {
        log.Fatalf("route %q: regex constraint missing pattern", p)
    }
    // Also pre-compile any regex pattern to catch syntax errors at startup.
    m := regexp.MustCompile(`:([^<]+)<regex\(([^)]*)\)>`).FindStringSubmatch(p)
    if m != nil {
        if _, err := regexp.Compile(m[2]); err != nil {
            log.Fatalf("route %q: bad regex: %v", p, err)
        }
    }
}

Try / catch

matcher, err := regexConstraint.Analyze(args)
if err != nil {
    return fmt.Errorf("route regex constraint: %w", err)
}

Prevention

When it happens

Trigger: Calling Analyze on a regex constraint with no arguments, derived from a route pattern such as :p<regex> or :p<regex()> with nothing inside the parens. Note that an invalid (non-empty) pattern produces a different wrapped error ("parse constraint arg: %w") from regexp.Compile, not this message.

Common situations: Forgetting to put the pattern inside the constraint; leaving the parens empty while refactoring; assuming regex inherits the comma-splitting behavior of min/max (it does not).

Related errors


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

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