{"record":{"id":"7b7ccc353cce8004","repo":"gofiber/fiber","slug":"fiber-keyauth-middleware-requires-a-validator-fun","errorCode":null,"errorMessage":"fiber: keyauth middleware requires a validator function","messagePattern":"fiber: keyauth middleware requires a validator function","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/keyauth/config.go","lineNumber":105,"sourceCode":"}\n\n// ConfigDefault is the default config\nvar ConfigDefault = Config{\n\tSuccessHandler: func(c fiber.Ctx) error {\n\t\treturn c.Next()\n\t},\n\tErrorHandler: func(c fiber.Ctx, _ error) error {\n\t\treturn c.Status(fiber.StatusUnauthorized).SendString(ErrMissingOrMalformedAPIKey.Error())\n\t},\n\tRealm:     \"Restricted\",\n\tExtractor: extractors.FromAuthHeader(\"Bearer\"),\n}\n\n// configDefault is a helper function to set default values\nfunc configDefault(config ...Config) Config {\n\t// Return default config if nothing provided\n\tif len(config) < 1 {\n\t\tpanic(\"fiber: keyauth middleware requires a validator function\")\n\t}\n\tcfg := config[0]\n\n\t// Require a validator function\n\tif cfg.Validator == nil {\n\t\tpanic(\"fiber: keyauth middleware requires a validator function\")\n\t}\n\n\t// Set default values\n\tif cfg.Extractor.Extract == nil {\n\t\tcfg.Extractor = ConfigDefault.Extractor\n\t}\n\tif cfg.Realm == \"\" {\n\t\tcfg.Realm = ConfigDefault.Realm\n\t}\n\tif cfg.SuccessHandler == nil {\n\t\tcfg.SuccessHandler = ConfigDefault.SuccessHandler\n\t}","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/middleware/keyauth/config.go#L87-L123","documentation":"keyauth.New() panics when called with no Config argument at all. Because Validator is the only required field and cannot have a sensible default, calling New() with zero arguments is treated as a programmer error: the middleware has no way to decide which keys are valid. The same message is reused for the nil-Validator case (error 287) but this specific line fires on len(config) < 1.","triggerScenarios":"keyauth.New() with no arguments. Also any wrapper that does var cfg keyauth.Config; keyauth.New(cfg) would NOT hit this line (it has len 1) — only a truly empty variadic call does.","commonSituations":"Adding app.Use(keyauth.New()) as a placeholder during scaffolding and forgetting to come back; refactor that moves Config construction into a function returning zero values; conditional wiring where the config-arg branch is dropped.","solutions":["Pass a Config with a Validator: keyauth.New(keyauth.Config{Validator: func(c fiber.Ctx, key string) (bool, error){ ... }}).","If you are just scaffolding, comment out the app.Use(keyauth.New()) line until the Validator is ready.","Ensure any helper that builds keyauth config always returns a non-nil Validator before the result reaches New()."],"exampleFix":"// before\napp.Use(keyauth.New())\n\n// after\napp.Use(keyauth.New(keyauth.Config{\n    Validator: func(_ fiber.Ctx, key string) (bool, error) {\n        return key == os.Getenv(\"API_KEY\"), nil\n    },\n}))","handlingStrategy":"validation","validationCode":"func mustKeyAuth(validator func(fiber.Ctx, string) (bool, error)) fiber.Handler {\n    if validator == nil {\n        log.Fatal(\"keyauth requires a validator\")\n    }\n    return keyauth.New(keyauth.Config{Validator: validator})\n}","typeGuard":"func hasKeyAuthConfig(args ...keyauth.Config) bool { return len(args) > 0 }","tryCatchPattern":null,"preventionTips":["Never call keyauth.New() without arguments in production code paths.","Wrap keyauth.New in a project helper that always supplies a Validator.","Add a smoke test that boots the app to catch missing-config panics in CI."],"tags":["keyauth","config","required-field","auth","startup-panic"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}