{"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/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/constraint.go#L522-L558","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a pattern, e.g. /:slug<regex(^[a-z0-9-]+$)>.","When constructing patterns from config, fall back to a sensible default pattern instead of emitting an empty one.","Unit-test route registration with a request that must match, so an empty regex is caught immediately."],"exampleFix":"// before\napp.Get(\"/:slug<regex>\", handler)\n\n// after\napp.Get(\"/:slug<regex(^[a-z0-9-]+$)>\", handler)","handlingStrategy":"validation","validationCode":"// Reject empty regex constraints.\nif regexp.MustCompile(`<regex\\s*>`).MatchString(pattern) {\n    return errors.New(\"regex constraint requires a pattern\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always provide a concrete pattern inside regex(...).","Default to a sensible pattern when building patterns from user/config input.","Test that a representative request matches each regex-constrained route."],"tags":["routing","constraint","regex","config","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}