{"record":{"id":"eaae4731ac6b1949","repo":"kataras/iris","slug":"empty-regex-expression","errorCode":null,"errorMessage":"empty regex expression","messagePattern":"empty regex expression","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"macro/macro.go","lineNumber":58,"sourceCode":"\t\t\t}\n\n\t\t\tif typOut.NumIn() == 1 && typOut.NumOut() == 1 { // if it's a type of func(paramValue [int,string...]) bool, used for param funcs.\n\t\t\t\treturn typOut.Out(0).Kind() == reflect.Bool\n\t\t\t}\n\t\t}\n\t}\n\n\treturn false\n}\n\n// Regexp accepts a regexp \"expr\" expression\n// and returns its MatchString.\n// The regexp is compiled before return.\n//\n// Returns a not-nil error on regexp compile failure.\nfunc Regexp(expr string) (func(string) bool, error) {\n\tif expr == \"\" {\n\t\treturn nil, fmt.Errorf(\"empty regex expression\")\n\t}\n\n\t// add the last $ if missing (and not wildcard(?))\n\tif i := expr[len(expr)-1]; i != '$' && i != '*' {\n\t\texpr += \"$\"\n\t}\n\n\tr, err := regexp.Compile(expr)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn r.MatchString, nil\n}\n\n// MustRegexp same as Regexp\n// but it panics on the \"expr\" parse failure.\nfunc MustRegexp(expr string) func(string) bool {","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/macro/macro.go#L40-L76","documentation":"macro.Regexp compiles a macro constraint expression into a MatchString func and returns 'empty regex expression' if expr is an empty string. MustRegexp calls Regexp and panics on error, so this guards against configuring a regexp-based param type with no expression.","triggerScenarios":"Calling macro.Regexp(\"\") directly, or defining a custom macro/param type whose regexp constraint is an empty string (which would otherwise panic via MustRegexp).","commonSituations":"Building custom param types with a regexp pulled from config/env that is unset; template-generated macros where the expression variable is empty.","solutions":["Supply a valid non-empty regexp expression to Regexp/MustRegexp.","If the expression comes from configuration, validate it is non-empty (and compiles) before calling Regexp.","Provide a sensible default expression when the configured value is empty."],"exampleFix":"// before\neval := macro.MustRegexp(cfg.Pattern) // panics if empty\n// after\nif cfg.Pattern == \"\" {\n    cfg.Pattern = \"^[a-zA-Z0-9_-]+$\"\n}\neval := macro.MustRegexp(cfg.Pattern)","handlingStrategy":"validation","validationCode":"if expr == \"\" {\n    return errors.New(\"macro regexp expression must be configured\")\n}\nif _, err := regexp.Compile(expr); err != nil {\n    return fmt.Errorf(\"invalid macro regexp %q: %w\", expr, err)\n}","typeGuard":"func validRegexExpr(expr string) bool {\n    if expr == \"\" { return false }\n    _, err := regexp.Compile(expr)\n    return err == nil\n}","tryCatchPattern":"defer func() {\n    if r := recover(); r != nil { // MustRegexp panics on error\n        log.Fatalf(\"macro regexp setup failed: %v\", r)\n    }\n}()","preventionTips":["Never pass config/env values straight into MustRegexp without defaults and compile checks.","Prefer Regexp() over MustRegexp() when the expression is dynamic.","Unit-test custom macro definitions at package init."],"tags":["go","iris","macro","regex","configuration"],"backgroundTag":"empty-regex-expression","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}