{"record":{"id":"6b54145271cd565b","repo":"gofiber/fiber","slug":"regex-constraint-requires-a-pattern-argument","errorCode":null,"errorMessage":"regex constraint requires a pattern argument","messagePattern":"regex constraint requires a pattern argument","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"constraint.go","lineNumber":540,"sourceCode":"\tif !ok {\n\t\treturn false\n\t}\n\thi, ok := data[1].(int)\n\tif !ok {\n\t\treturn false\n\t}\n\tnum, err := strconv.Atoi(param)\n\treturn err == nil && num >= lo && num <= hi\n}\n\ntype regexConstraintType struct {\n\tregexHandler any\n}\n\nfunc (regexConstraintType) Name() string { return ConstraintRegex }\nfunc (r regexConstraintType) Analyze(args []string) ([]any, error) {\n\tif len(args) == 0 {\n\t\treturn nil, errors.New(\"regex constraint requires a pattern argument\")\n\t}\n\tif r.regexHandler == nil {\n\t\tre, err := regexp.Compile(args[0])\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"parse constraint arg: %w\", err)\n\t\t}\n\t\treturn []any{re}, nil\n\t}\n\tmatcher := compileRegex(r.regexHandler, args[0])\n\treturn []any{matcher}, nil\n}\n\nfunc (regexConstraintType) Execute(param string, data []any) bool {\n\tif len(data) == 0 {\n\t\treturn false\n\t}\n\tmatcher, ok := data[0].(regexMatcher)\n\tif !ok || matcher == nil {","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/constraint.go#L522-L558","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Supply a valid regexp pattern: :param<regex(p([a-z]+)ch)>.","Remember regex receives the raw args slice (no comma splitting), so a pattern containing a comma does not need escaping.","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."],"exampleFix":"// before\napp.Get(\"/x/:p<regex>\", h)\n// after\napp.Get(\"/x/:p<regex(p([a-z]+)ch)>\", h)","handlingStrategy":"validation","validationCode":"var regexEmpty = regexp.MustCompile(`:([^<]+)<regex\\(\\s*\\)>`)\nfor _, p := range routePatterns {\n    if loc := regexEmpty.FindStringIndex(p); loc != nil {\n        log.Fatalf(\"route %q: regex constraint missing pattern\", p)\n    }\n    // Also pre-compile any regex pattern to catch syntax errors at startup.\n    m := regexp.MustCompile(`:([^<]+)<regex\\(([^)]*)\\)>`).FindStringSubmatch(p)\n    if m != nil {\n        if _, err := regexp.Compile(m[2]); err != nil {\n            log.Fatalf(\"route %q: bad regex: %v\", p, err)\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"matcher, err := regexConstraint.Analyze(args)\nif err != nil {\n    return fmt.Errorf(\"route regex constraint: %w\", err)\n}","preventionTips":["Remember regex receives the raw args slice; do not escape commas inside the pattern.","Pre-compile patterns in a startup test so syntax errors surface before serve.","If using a custom RegexHandler, ensure it accepts the same pattern syntax as regexp.Compile."],"tags":["routing","constraint","route-registration","regex","validation"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}