Documentation
¶
Overview ¶
Package validator provides a lightweight, composable field validator.
Build a Validator, call Validator.Field for each input field with one or more Rule functions, then call Validator.Result to retrieve a *ValidationError (nil when all fields pass).
Example:
v := validator.New()
v.Field("email", req.Email, validator.Required(), validator.Email())
v.Field("age", req.Age, validator.Gte(18))
if err := v.Result(); err != nil {
// err.Fields contains per-field details
}
Index ¶
- type ErrCode
- type FieldError
- type Rule
- func Between(min, max float64) Rule
- func Email() Rule
- func Gt(n float64) Rule
- func Gte(n float64) Rule
- func HasSpecialChar() Rule
- func Lt(n float64) Rule
- func Lte(n float64) Rule
- func Matches(re *regexp.Regexp, message ...string) Rule
- func MaxLength(n int) Rule
- func MinLength(n int) Rule
- func NonNegative() Rule
- func OneOf[T comparable](values ...T) Rule
- func Optional(rules ...Rule) Rule
- func Positive() Rule
- func Required() Rule
- func URL() Rule
- func UUID() Rule
- type RuleError
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FieldError ¶
FieldError describes a validation failure for a single field.
type Rule ¶
Rule is a validation function. Returns nil when the value is valid. On failure, return a *RuleError to carry a code and message.
func Between ¶
Between fails when value is outside [min, max] (inclusive). Returns ErrMin when below min, ErrMax when above max.
func Email ¶
func Email() Rule
Email fails when the value is not a valid email address. Display-name format ("John <user@example.com>") is rejected; only bare addresses pass.
func HasSpecialChar ¶
func HasSpecialChar() Rule
HasSpecialChar fails when the value contains no non-alphanumeric character.
func Matches ¶
Matches fails when the value does not match re. Compile the regexp once at package or init level, not inside a handler. An optional message overrides the default "invalid format" error text.
func OneOf ¶
func OneOf[T comparable](values ...T) Rule
OneOf fails when value is not among the provided allowed values.
func Optional ¶
Optional wraps rules and skips them when value is nil or whitespace-only. Non-empty values are passed through all rules in order; first failure stops.
func Required ¶
func Required() Rule
Required fails when value is nil, empty, or whitespace-only. Non-nil, non-string values are considered present and pass.
type RuleError ¶
RuleError is returned by a Rule when validation fails. Callers that invoke rules directly can use errors.As to retrieve the code and params.
type ValidationError ¶
type ValidationError struct {
Message string
Fields []FieldError
}
ValidationError is returned by Validator.Result when one or more fields failed validation.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error implements the error interface.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator accumulates field errors for a request payload.
func (*Validator) Field ¶
Field runs rules against value in order, recording the first failure for fieldName. Subsequent rules are skipped once a failure is found. Rules that return a plain error (not *RuleError) are recorded with CodeInvalid.
func (*Validator) Result ¶
func (v *Validator) Result() *ValidationError
Result returns nil if no errors were recorded, or a *ValidationError with all field errors.