diff --git a/ctx.go b/ctx.go index 8a41d8ac031..e8b9d6fc14d 100644 --- a/ctx.go +++ b/ctx.go @@ -246,6 +246,9 @@ func (c *DefaultCtx) BindWithValidate(v any) error { } func (c *DefaultCtx) Validate(v any) error { + if c.app.config.Validator == nil { + return NilValidatorError{} + } return c.app.config.Validator.Validate(v) } diff --git a/error.go b/error.go index 9b93d007f0f..4d7072fa53d 100644 --- a/error.go +++ b/error.go @@ -11,6 +11,14 @@ var ( ErrRangeUnsatisfiable = errors.New("range: unsatisfiable range") ) +// NilValidatorError is the validate error when context.Validate is called but no validator is set in config. +type NilValidatorError struct { +} + +func (n NilValidatorError) Error() string { + return "fiber: ctx.Validate(v any) is called without validator" +} + // InvalidBinderError is the error when try to bind unsupported type. type InvalidBinderError struct { Type reflect.Type