Documentation
¶
Overview ¶
Package validation provides input validation with detailed error messages.
Package validation provides input validation with detailed error messages.
Index ¶
- func ValidateBody(params []ParamDef) func(http.Handler) http.Handler
- func ValidateQuery(params []ParamDef) func(http.Handler) http.Handler
- func ValidationMiddleware(bodyParams, queryParams []ParamDef) func(http.Handler) http.Handler
- type ParamDef
- type ValidationError
- type ValidationErrors
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateBody ¶
ValidateBody is a helper middleware that only validates request body.
func ValidateQuery ¶
ValidateQuery is a helper middleware that only validates query parameters.
func ValidationMiddleware ¶
ValidationMiddleware creates middleware that validates request query parameters and body. It validates query params first, then body params. If either fails, it returns a 400 error.
Types ¶
type ParamDef ¶
type ParamDef struct {
Name string
Type string
Required bool
Default any
Min *float64
Max *float64
MinLength *int
MaxLength *int
Pattern *regexp.Regexp
Enum []string
Custom func(any) error
}
ParamDef defines validation rules for a parameter.
type ValidationError ¶
type ValidationError struct {
Field string `json:"field"`
Value any `json:"value,omitempty"`
Rule string `json:"rule"`
Message string `json:"message"`
Suggestion string `json:"suggestion,omitempty"`
}
ValidationError represents a single validation error with details.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Error implements the error interface.
type ValidationErrors ¶
type ValidationErrors struct {
Errors []ValidationError `json:"errors"`
}
ValidationErrors aggregates multiple validation errors.
func (*ValidationErrors) Error ¶
func (e *ValidationErrors) Error() string
Error implements the error interface.
func (*ValidationErrors) HasErrors ¶
func (e *ValidationErrors) HasErrors() bool
HasErrors returns true if there are any validation errors.