gofr-dev/gofr · error
validate func is empty
Error message
validate func is empty
What it means
errValidateFuncEmpty is returned by NewAPIKeyAuthProviderWithValidateFunc and NewBasicAuthProviderWithValidateFunc when the provided validate callback is nil. Without a validation function the provider has no way to decide whether credentials are valid, so construction fails immediately. It is a programming/config error at setup time.
Source
Thrown at pkg/gofr/http/middleware/auth.go:30
// AuthMethod represents a custom type to define the different authentication methods supported.
type AuthMethod int
const (
JWTClaim AuthMethod = iota // JWTClaim represents the key used to store JWT claims within the request context.
Username
APIKey
// #nosec G101
headerXAPIKey = "X-Api-Key"
headerAuthorization = "Authorization"
dummyValue = "dummy"
)
var (
errContainerNil = errors.New("container is nil")
errValidateFuncEmpty = errors.New("validate func is empty")
)
// AuthHeaders returns the request header names GoFr's authentication middleware reads. It is the
// single source of truth for callers that must forward a request's identity — e.g. the MCP server
// re-dispatching a tool call through the router.
func AuthHeaders() []string {
return []string{headerAuthorization, headerXAPIKey}
}
type AuthProvider interface {
GetAuthMethod() AuthMethod
ExtractAuthHeader(r *http.Request) (any, ErrorHTTP)
}
// AuthMiddleware creates a middleware function that enforces authentication based on the method provided.
func AuthMiddleware(a AuthProvider) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {View on GitHub (pinned to 187eb24962)
Solutions
- Pass a non-nil validation function to the constructor
- If the validator is dynamic, initialize a default implementation before wiring middleware
- Assert the function is non-nil at startup; fail fast with a clear log message
- Add a test covering the constructor with your real validator wiring
Example fix
// before
var validate func(c *container.Container, user, pass string) bool
provider, _ := middleware.NewBasicAuthProviderWithValidateFunc(c, validate) // errValidateFuncEmpty
// after
validate := func(c *container.Container, user, pass string) bool {
return c != nil && checkCredentials(user, pass)
}
provider, err := middleware.NewBasicAuthProviderWithValidateFunc(c, validate) Defensive patterns
Strategy: validation
Validate before calling
if validateFunc == nil {
return errors.New("validate func must be provided to auth provider constructor")
} Type guard
func hasValidator(fn func(*container.Container, string, string) bool) bool { return fn != nil } Try / catch
provider, err := middleware.NewAPIKeyAuthProviderWithValidateFunc(c, fn)
if err != nil {
if errors.Is(err, middleware.ErrValidateFuncEmpty) {
log.Fatal("no validate function wired for auth provider")
}
return err
} Prevention
- Always pass a concrete validator implementation; never a conditionally-nil variable
- Define default validators as package constants so they can't be nil
- Review function-typed config fields for nil defaults
- Cover constructor wiring in unit tests
When it happens
Trigger: Calling NewAPIKeyAuthProviderWithValidateFunc(c, nil) or NewBasicAuthProviderWithValidateFunc(c, nil), e.g. when the callback is conditionally assigned and ends up nil.
Common situations: Function variables resolved from config/flags that were never set, refactors that removed the assignment, or test scaffolding that forgot to inject a stub validator.
Related errors
- api keys list is empty
- user list is empty
- unsupported config file format
- %w: negative offset %d
- out of range
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/a1c9c73fc07b5050.
Report an issue: GitHub.