Documentation
¶
Overview ¶
Package validator provides FluentValidation-style validation for Go structs and unstructured data: build a Validator with New, declare rules on fields, then call Validate. Every rule attaches the same way, as a free function taking the validator and a field pointer:
validator.String(v, &user.Name).NotEmpty().Min(2) validator.Number(v, &user.Age).Gte(0)
Standalone rule values for unstructured data live in the rule subpackage.
A Validator is built per value and is not safe for concurrent use. Standalone rule values are read-only once built, so they can be shared across goroutines.
Example ¶
package main
import (
"fmt"
validator "github.com/Jamess-Lucass/validator-go"
)
func main() {
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
user := User{Name: "J", Email: "bad", Age: -1}
v, err := validator.New(&user)
if err != nil {
panic(err)
}
validator.String(v, &user.Name).NotEmpty().Min(2)
validator.String(v, &user.Email).Email()
validator.Number(v, &user.Age).Gte(0)
result := v.Validate()
fmt.Println(result.IsValid())
for _, e := range result.Errors {
fmt.Printf("%s: %s\n", e.Field, e.Message)
}
}
Output: false name: must be at least 2 characters email: must be a valid email address age: must be greater than or equal to 0
Index ¶
- type ArrayRule
- func (r *ArrayRule) EachValue(rule Rule) *ArrayRule
- func (r *ArrayRule) Length(min, max int) *ArrayRule
- func (r *ArrayRule) Max(n int) *ArrayRule
- func (r *ArrayRule) Min(n int) *ArrayRule
- func (r *ArrayRule) NotEmpty() *ArrayRule
- func (r *ArrayRule) NotNil() *ArrayRule
- func (r *ArrayRule) WithMessage(msg string) *ArrayRule
- func (r *ArrayRule) WithName(name string) *ArrayRule
- type BoolRule
- func (r *BoolRule[T]) Must(fn func(T) bool) *BoolRule[T]
- func (r *BoolRule[T]) NotEmpty() *BoolRule[T]
- func (r *BoolRule[T]) NotNil() *BoolRule[T]
- func (r *BoolRule[T]) Validate(value any) []ValidationError
- func (r *BoolRule[T]) WithMessage(msg string) *BoolRule[T]
- func (r *BoolRule[T]) WithName(name string) *BoolRule[T]
- type ComplexNumber
- type ComplexRule
- func (r *ComplexRule[T]) Must(fn func(T) bool) *ComplexRule[T]
- func (r *ComplexRule[T]) NotEmpty() *ComplexRule[T]
- func (r *ComplexRule[T]) NotNil() *ComplexRule[T]
- func (r *ComplexRule[T]) Validate(value any) []ValidationError
- func (r *ComplexRule[T]) WithMessage(msg string) *ComplexRule[T]
- func (r *ComplexRule[T]) WithName(name string) *ComplexRule[T]
- type Float
- type Integer
- type MapRule
- func (r *MapRule[M, K, V]) Each(fn func(key K, value *V, sv *Validator)) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) EachKey(rule Rule) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) EachValue(rule Rule) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) HasKey(key K) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) Length(min, max int) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) Max(n int) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) Min(n int) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) Must(fn func(M) bool) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) NotEmpty() *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) NotNil() *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) Validate(value any) []ValidationError
- func (r *MapRule[M, K, V]) WithMessage(msg string) *MapRule[M, K, V]
- func (r *MapRule[M, K, V]) WithName(name string) *MapRule[M, K, V]
- type NumberRule
- func (r *NumberRule[T]) Finite() *NumberRule[T]
- func (r *NumberRule[T]) Gt(n T) *NumberRule[T]
- func (r *NumberRule[T]) Gte(n T) *NumberRule[T]
- func (r *NumberRule[T]) Lt(n T) *NumberRule[T]
- func (r *NumberRule[T]) Lte(n T) *NumberRule[T]
- func (r *NumberRule[T]) MultipleOf(n T) *NumberRule[T]
- func (r *NumberRule[T]) Must(fn func(T) bool) *NumberRule[T]
- func (r *NumberRule[T]) Negative() *NumberRule[T]
- func (r *NumberRule[T]) Nonnegative() *NumberRule[T]
- func (r *NumberRule[T]) Nonpositive() *NumberRule[T]
- func (r *NumberRule[T]) NotEmpty() *NumberRule[T]
- func (r *NumberRule[T]) NotNil() *NumberRule[T]
- func (r *NumberRule[T]) OneOf(values ...T) *NumberRule[T]
- func (r *NumberRule[T]) Positive() *NumberRule[T]
- func (r *NumberRule[T]) Validate(value any) []ValidationError
- func (r *NumberRule[T]) WithMessage(msg string) *NumberRule[T]
- func (r *NumberRule[T]) WithName(name string) *NumberRule[T]
- type ObjectRule
- type ObjectValidator
- type Real
- type Rule
- type SliceRule
- func (r *SliceRule[S, T]) Each(fn func(item *T, sv *Validator)) *SliceRule[S, T]
- func (r *SliceRule[S, T]) EachValue(rule Rule) *SliceRule[S, T]
- func (r *SliceRule[S, T]) Length(min, max int) *SliceRule[S, T]
- func (r *SliceRule[S, T]) Max(n int) *SliceRule[S, T]
- func (r *SliceRule[S, T]) Min(n int) *SliceRule[S, T]
- func (r *SliceRule[S, T]) Must(fn func(S) bool) *SliceRule[S, T]
- func (r *SliceRule[S, T]) NotEmpty() *SliceRule[S, T]
- func (r *SliceRule[S, T]) NotNil() *SliceRule[S, T]
- func (r *SliceRule[S, T]) Validate(value any) []ValidationError
- func (r *SliceRule[S, T]) WithMessage(msg string) *SliceRule[S, T]
- func (r *SliceRule[S, T]) WithName(name string) *SliceRule[S, T]
- type StringRule
- func (r *StringRule[T]) Email() *StringRule[T]
- func (r *StringRule[T]) EndsWith(suffix string) *StringRule[T]
- func (r *StringRule[T]) Includes(substr string) *StringRule[T]
- func (r *StringRule[T]) Length(min, max int) *StringRule[T]
- func (r *StringRule[T]) Matches(pattern string) *StringRule[T]
- func (r *StringRule[T]) Max(n int) *StringRule[T]
- func (r *StringRule[T]) Min(n int) *StringRule[T]
- func (r *StringRule[T]) Must(fn func(T) bool) *StringRule[T]
- func (r *StringRule[T]) NotEmpty() *StringRule[T]
- func (r *StringRule[T]) NotNil() *StringRule[T]
- func (r *StringRule[T]) OneOf(values ...T) *StringRule[T]
- func (r *StringRule[T]) StartsWith(prefix string) *StringRule[T]
- func (r *StringRule[T]) URL() *StringRule[T]
- func (r *StringRule[T]) Validate(value any) []ValidationError
- func (r *StringRule[T]) WithMessage(msg string) *StringRule[T]
- func (r *StringRule[T]) WithName(name string) *StringRule[T]
- type TimeRule
- func (r *TimeRule) After(t time.Time) *TimeRule
- func (r *TimeRule) Before(t time.Time) *TimeRule
- func (r *TimeRule) Must(fn func(time.Time) bool) *TimeRule
- func (r *TimeRule) NotEmpty() *TimeRule
- func (r *TimeRule) NotNil() *TimeRule
- func (r *TimeRule) Validate(value any) []ValidationError
- func (r *TimeRule) WithMessage(msg string) *TimeRule
- func (r *TimeRule) WithName(name string) *TimeRule
- type UUIDRule
- type ValidationError
- type ValidationResult
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArrayRule ¶ added in v1.0.0
type ArrayRule struct {
// contains filtered or unexported fields
}
ArrayRule validates a fixed-size array field. Go generics can't abstract over an array's length, so it works by reflection and takes the field as a pointer. Use Slice for slices, which keeps the element type.
func Array ¶ added in v0.3.0
Array validates an array field by reflection; it also accepts a pointer to a slice. Pass the field address: without it the value is a copy and the name falls back to "unknown".
func (*ArrayRule) EachValue ¶ added in v1.0.0
EachValue validates every element against the given rule. It panics if rule is nil.
func (*ArrayRule) NotNil ¶ added in v1.0.0
NotNil fails when the field pointer is nil (e.g. a nil *[3]int) or points at a nil slice.
func (*ArrayRule) WithMessage ¶ added in v1.0.0
WithMessage replaces the previous rule's error message. Directly after EachValue it instead replaces the message of every error it produces.
type BoolRule ¶ added in v1.0.0
type BoolRule[T ~bool] struct { // contains filtered or unexported fields }
BoolRule validates a bool field or value. Named bool types work too.
func (*BoolRule[T]) Must ¶ added in v1.0.0
Must runs a custom check against the value. It panics if fn is nil.
func (*BoolRule[T]) NotNil ¶ added in v1.0.0
NotNil fails when the field pointer or standalone value is nil.
func (*BoolRule[T]) Validate ¶ added in v1.0.0
func (r *BoolRule[T]) Validate(value any) []ValidationError
Validate implements Rule for standalone use; the value must be a bool or a named bool type.
func (*BoolRule[T]) WithMessage ¶ added in v1.0.0
WithMessage replaces the previous rule's error message.
type ComplexNumber ¶ added in v1.0.0
type ComplexNumber interface {
~complex64 | ~complex128
}
ComplexNumber matches the complex types and named types based on them. Complex values are not ordered, so only presence and custom rules apply.
type ComplexRule ¶ added in v1.0.0
type ComplexRule[T ComplexNumber] struct { // contains filtered or unexported fields }
ComplexRule validates a complex-number field (complex64 or complex128). Complex values aren't ordered, so only presence and custom rules apply.
func Complex ¶ added in v1.0.0
func Complex[T ComplexNumber](v *Validator, field *T) *ComplexRule[T]
Complex validates a complex-number field.
func (*ComplexRule[T]) Must ¶ added in v1.0.0
func (r *ComplexRule[T]) Must(fn func(T) bool) *ComplexRule[T]
Must runs a custom check against the value. It panics if fn is nil.
func (*ComplexRule[T]) NotEmpty ¶ added in v1.0.0
func (r *ComplexRule[T]) NotEmpty() *ComplexRule[T]
NotEmpty fails on zero.
func (*ComplexRule[T]) NotNil ¶ added in v1.0.0
func (r *ComplexRule[T]) NotNil() *ComplexRule[T]
NotNil fails when the field pointer or standalone value is nil.
func (*ComplexRule[T]) Validate ¶ added in v1.0.0
func (r *ComplexRule[T]) Validate(value any) []ValidationError
Validate implements Rule for standalone use; complex values, real numbers, and parseable strings are accepted.
func (*ComplexRule[T]) WithMessage ¶ added in v1.0.0
func (r *ComplexRule[T]) WithMessage(msg string) *ComplexRule[T]
WithMessage replaces the previous rule's error message.
func (*ComplexRule[T]) WithName ¶ added in v1.0.0
func (r *ComplexRule[T]) WithName(name string) *ComplexRule[T]
WithName overrides the field name used in errors.
type Integer ¶ added in v1.0.0
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
Integer matches every built-in integer type and any named type whose underlying type is one of them (e.g. `type Age int`).
type MapRule ¶ added in v1.0.0
type MapRule[M ~map[K]V, K comparable, V any] struct { // contains filtered or unexported fields }
MapRule validates a map field. Named map types (type Labels map[string]string) work too. Entries are visited in sorted key order so error output is stable from run to run.
func Map ¶ added in v1.0.0
func Map[M ~map[K]V, K comparable, V any](v *Validator, field *M) *MapRule[M, K, V]
Map validates a map field. Like Slice, it is a free function because Go methods cannot have type parameters. Pass the field address:
validator.Map(v, &d.Labels).NotEmpty().EachValue(validator.String().NotEmpty())
func MapOf ¶ added in v1.0.0
MapOf runs every value of a map through the given rule, for use with NewObject fields. It is to Map what SliceOf is to Slice.
func (*MapRule[M, K, V]) Each ¶ added in v1.0.0
Each runs the callback for every entry in sorted key order. Errors raised on sv are prefixed with the entry, like "containers[web].image". The value is a copy, since map values aren't addressable. It panics if fn is nil.
func (*MapRule[M, K, V]) EachKey ¶ added in v1.0.0
EachKey validates every key against the given rule. Error messages are prefixed with "key" to tell them apart from value errors on the same entry: "labels[e]: key must be at least 2 characters". It panics if rule is nil.
func (*MapRule[M, K, V]) EachValue ¶ added in v1.0.0
EachValue validates every value against the given rule, naming errors by entry: "labels[env]: must not be empty". It panics if rule is nil.
func (*MapRule[M, K, V]) HasKey ¶ added in v1.0.0
HasKey requires the key to be present; chain it to require several.
func (*MapRule[M, K, V]) Must ¶ added in v1.0.0
Must runs a custom check against the whole map. It panics if fn is nil.
func (*MapRule[M, K, V]) NotNil ¶ added in v1.0.0
NotNil fails on a nil map, which is distinct from an empty one.
func (*MapRule[M, K, V]) Validate ¶ added in v1.0.0
func (r *MapRule[M, K, V]) Validate(value any) []ValidationError
Validate implements Rule for standalone use. Each callbacks run here too, since entry names come from the key rather than struct reflection.
func (*MapRule[M, K, V]) WithMessage ¶ added in v1.0.0
WithMessage replaces the previous rule's error message. Directly after Each, EachValue, or EachKey it instead replaces the message of every error they produce.
type NumberRule ¶ added in v1.0.0
type NumberRule[T Real] struct { // contains filtered or unexported fields }
NumberRule validates a field of any real (integer or float) type, including named types like `type Age int`.
func Number ¶ added in v1.0.0
func Number[T Real](v *Validator, field *T) *NumberRule[T]
Number validates a numeric field of any real type, from int and float64 to named types like type Age int. The type is inferred from the field pointer:
validator.Number(v, &s.Age).Gte(0).Lte(150)
func (*NumberRule[T]) Finite ¶ added in v1.0.0
func (r *NumberRule[T]) Finite() *NumberRule[T]
Finite fails on NaN and ±Inf; integers always pass. The comparison rules don't reject NaN on their own, so chain this where that matters.
func (*NumberRule[T]) Gt ¶ added in v1.0.0
func (r *NumberRule[T]) Gt(n T) *NumberRule[T]
Gt requires a value greater than n.
func (*NumberRule[T]) Gte ¶ added in v1.0.0
func (r *NumberRule[T]) Gte(n T) *NumberRule[T]
Gte requires a value greater than or equal to n.
func (*NumberRule[T]) Lt ¶ added in v1.0.0
func (r *NumberRule[T]) Lt(n T) *NumberRule[T]
Lt requires a value less than n.
func (*NumberRule[T]) Lte ¶ added in v1.0.0
func (r *NumberRule[T]) Lte(n T) *NumberRule[T]
Lte requires a value less than or equal to n.
func (*NumberRule[T]) MultipleOf ¶ added in v1.0.0
func (r *NumberRule[T]) MultipleOf(n T) *NumberRule[T]
MultipleOf requires the value to be a multiple of n. Exact for integers; a magnitude-relative tolerance for floats.
func (*NumberRule[T]) Must ¶ added in v1.0.0
func (r *NumberRule[T]) Must(fn func(T) bool) *NumberRule[T]
Must runs a custom check against the value. It panics if fn is nil.
func (*NumberRule[T]) Negative ¶ added in v1.0.0
func (r *NumberRule[T]) Negative() *NumberRule[T]
Negative requires a value less than zero.
func (*NumberRule[T]) Nonnegative ¶ added in v1.0.0
func (r *NumberRule[T]) Nonnegative() *NumberRule[T]
Nonnegative requires a value of zero or more.
func (*NumberRule[T]) Nonpositive ¶ added in v1.0.0
func (r *NumberRule[T]) Nonpositive() *NumberRule[T]
Nonpositive requires a value of zero or less.
func (*NumberRule[T]) NotEmpty ¶ added in v1.0.0
func (r *NumberRule[T]) NotEmpty() *NumberRule[T]
NotEmpty fails on zero.
func (*NumberRule[T]) NotNil ¶ added in v1.0.0
func (r *NumberRule[T]) NotNil() *NumberRule[T]
NotNil fails when the field pointer or standalone value is nil.
func (*NumberRule[T]) OneOf ¶ added in v1.0.0
func (r *NumberRule[T]) OneOf(values ...T) *NumberRule[T]
OneOf requires the value to be one of the given values, e.g. the members of an integer enum.
func (*NumberRule[T]) Positive ¶ added in v1.0.0
func (r *NumberRule[T]) Positive() *NumberRule[T]
Positive requires a value greater than zero.
func (*NumberRule[T]) Validate ¶ added in v1.0.0
func (r *NumberRule[T]) Validate(value any) []ValidationError
Validate implements Rule for standalone use. Any numeric kind or numeric string is coerced into T; out-of-range values are rejected, not wrapped.
func (*NumberRule[T]) WithMessage ¶ added in v1.0.0
func (r *NumberRule[T]) WithMessage(msg string) *NumberRule[T]
WithMessage replaces the previous rule's error message.
func (*NumberRule[T]) WithName ¶ added in v1.0.0
func (r *NumberRule[T]) WithName(name string) *NumberRule[T]
WithName overrides the field name used in errors.
type ObjectRule ¶ added in v1.0.0
type ObjectRule struct {
// contains filtered or unexported fields
}
ObjectRule validates a nested unstructured object within NewObject data.
func Object ¶
func Object(fn func(*ObjectValidator)) *ObjectRule
Object declares rules for a nested object (map[string]any) inside Field:
v.Field("address", validator.Object(func(mv *validator.ObjectValidator) {
mv.Field("city", rule.String().NotEmpty())
}))
For a typed map field on a struct (map[string]string and friends), use Map. It panics if fn is nil.
func (*ObjectRule) NotNil ¶ added in v1.0.0
func (r *ObjectRule) NotNil() *ObjectRule
NotNil fails when the key is missing or its value is nil.
func (*ObjectRule) Validate ¶ added in v1.0.0
func (r *ObjectRule) Validate(value any) []ValidationError
Validate implements Rule for standalone use; the value must be a string-keyed map.
type ObjectValidator ¶ added in v1.0.0
type ObjectValidator struct {
// contains filtered or unexported fields
}
ObjectValidator validates unstructured map[string]any data, declaring one rule per key.
func NewObject ¶ added in v1.0.0
func NewObject(m map[string]any) *ObjectValidator
NewObject returns a validator for unstructured data such as a decoded JSON payload. Declare rules with Field, then call Validate.
func (*ObjectValidator) Field ¶ added in v1.0.0
func (mv *ObjectValidator) Field(key string, rule Rule)
Field declares a rule for one key of the map. It panics if rule is nil.
func (*ObjectValidator) Validate ¶ added in v1.0.0
func (mv *ObjectValidator) Validate() *ValidationResult
Validate runs every field rule and returns the collected errors.
type Rule ¶ added in v1.0.0
type Rule interface {
Validate(value any) []ValidationError
}
Rule is a validation check that runs against a standalone value. The constructors in the rule subpackage all return implementations.
type SliceRule ¶ added in v1.0.0
type SliceRule[S ~[]T, T any] struct { // contains filtered or unexported fields }
SliceRule validates a slice field. Named slice types (type Tags []string) work too.
func Slice ¶ added in v1.0.0
Slice validates a slice field. Like every rule it is a free function, since Go methods cannot have their own type parameters. Pass the field address:
validator.Slice(v, &s.Tags).NotEmpty().EachValue(rule.String().NotEmpty())
func SliceOf ¶ added in v1.0.0
SliceOf runs each element of a slice value through the given rule, for use with NewObject fields.
func (*SliceRule[S, T]) Each ¶ added in v1.0.0
Each runs the callback for every element with a sub-validator; errors are prefixed with the index, like "items[2].name". It panics if fn is nil.
func (*SliceRule[S, T]) EachValue ¶ added in v1.0.0
EachValue checks every element against the rule, naming errors by index. It panics if rule is nil.
func (*SliceRule[S, T]) Must ¶ added in v1.0.0
Must runs a custom check against the whole slice. It panics if fn is nil.
func (*SliceRule[S, T]) NotNil ¶ added in v1.0.0
NotNil fails on a nil slice, which is distinct from an empty one.
func (*SliceRule[S, T]) Validate ¶ added in v1.0.0
func (r *SliceRule[S, T]) Validate(value any) []ValidationError
Validate implements Rule for standalone use. Each callbacks only run in struct mode, where there are field pointers to resolve names against.
func (*SliceRule[S, T]) WithMessage ¶ added in v1.0.0
WithMessage replaces the previous rule's error message. Directly after Each or EachValue it instead replaces the message of every error they produce.
type StringRule ¶ added in v1.0.0
type StringRule[T ~string] struct { // contains filtered or unexported fields }
StringRule validates a string field or value. Named string types (type ID string) work too.
func String ¶
func String[T ~string](v *Validator, field *T) *StringRule[T]
String validates a string field, plain or named:
validator.String(v, &s.Name).NotEmpty().Min(2)
func (*StringRule[T]) Email ¶ added in v1.0.0
func (r *StringRule[T]) Email() *StringRule[T]
Email requires a valid email address.
func (*StringRule[T]) EndsWith ¶ added in v1.0.0
func (r *StringRule[T]) EndsWith(suffix string) *StringRule[T]
EndsWith requires the given suffix.
func (*StringRule[T]) Includes ¶ added in v1.0.0
func (r *StringRule[T]) Includes(substr string) *StringRule[T]
Includes requires the substring to be present.
func (*StringRule[T]) Length ¶ added in v1.0.0
func (r *StringRule[T]) Length(min, max int) *StringRule[T]
Length requires between min and max characters, counting runes.
func (*StringRule[T]) Matches ¶ added in v1.0.0
func (r *StringRule[T]) Matches(pattern string) *StringRule[T]
Matches requires the value to match the regular expression. The pattern is compiled once when the rule is declared; like regexp.MustCompile, an invalid pattern panics.
func (*StringRule[T]) Max ¶ added in v1.0.0
func (r *StringRule[T]) Max(n int) *StringRule[T]
Max allows at most n characters, counting runes rather than bytes.
func (*StringRule[T]) Min ¶ added in v1.0.0
func (r *StringRule[T]) Min(n int) *StringRule[T]
Min requires at least n characters, counting runes rather than bytes.
func (*StringRule[T]) Must ¶ added in v1.0.0
func (r *StringRule[T]) Must(fn func(T) bool) *StringRule[T]
Must runs a custom check against the value. It panics if fn is nil.
func (*StringRule[T]) NotEmpty ¶ added in v1.0.0
func (r *StringRule[T]) NotEmpty() *StringRule[T]
NotEmpty fails on "".
func (*StringRule[T]) NotNil ¶ added in v1.0.0
func (r *StringRule[T]) NotNil() *StringRule[T]
NotNil fails when the field pointer or standalone value is nil.
func (*StringRule[T]) OneOf ¶ added in v1.0.0
func (r *StringRule[T]) OneOf(values ...T) *StringRule[T]
OneOf requires the value to be one of the given values, e.g. the members of a string enum.
func (*StringRule[T]) StartsWith ¶ added in v1.0.0
func (r *StringRule[T]) StartsWith(prefix string) *StringRule[T]
StartsWith requires the given prefix.
func (*StringRule[T]) URL ¶ added in v1.0.0
func (r *StringRule[T]) URL() *StringRule[T]
URL requires an absolute URL with a scheme and host.
func (*StringRule[T]) Validate ¶ added in v1.0.0
func (r *StringRule[T]) Validate(value any) []ValidationError
Validate implements Rule for standalone use; the value must be a string or a named string type.
func (*StringRule[T]) WithMessage ¶ added in v1.0.0
func (r *StringRule[T]) WithMessage(msg string) *StringRule[T]
WithMessage replaces the previous rule's error message.
func (*StringRule[T]) WithName ¶ added in v1.0.0
func (r *StringRule[T]) WithName(name string) *StringRule[T]
WithName overrides the field name used in errors.
type TimeRule ¶ added in v1.0.0
type TimeRule struct {
// contains filtered or unexported fields
}
TimeRule validates a time.Time field or value.
func (*TimeRule) Must ¶ added in v1.0.0
Must runs a custom check against the value. It panics if fn is nil.
func (*TimeRule) NotNil ¶ added in v1.0.0
NotNil fails when the field pointer or standalone value is nil.
func (*TimeRule) Validate ¶ added in v1.0.0
func (r *TimeRule) Validate(value any) []ValidationError
Validate implements Rule for standalone use; the value must be a time.Time or an RFC3339 string.
func (*TimeRule) WithMessage ¶ added in v1.0.0
WithMessage replaces the previous rule's error message.
type UUIDRule ¶ added in v1.0.0
type UUIDRule struct {
// contains filtered or unexported fields
}
UUIDRule validates a github.com/google/uuid value.
func (*UUIDRule) Must ¶ added in v1.0.0
Must runs a custom check against the value. It panics if fn is nil.
func (*UUIDRule) NotNil ¶ added in v1.0.0
NotNil fails when the field pointer or standalone value is nil.
func (*UUIDRule) Validate ¶ added in v1.0.0
func (r *UUIDRule) Validate(value any) []ValidationError
Validate implements Rule for standalone use; the value must be a uuid.UUID, a [16]byte, or a parseable string.
func (*UUIDRule) WithMessage ¶ added in v1.0.0
WithMessage replaces the previous rule's error message.
type ValidationError ¶
ValidationError is a single failed check: which field, and why.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Error formats the error as "field: message".
type ValidationResult ¶
type ValidationResult struct {
Errors []ValidationError `json:"errors"`
}
ValidationResult holds every error collected by a Validate call.
func (*ValidationResult) IsValid ¶
func (r *ValidationResult) IsValid() bool
IsValid reports whether no errors were found.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator collects rules for one struct and runs them on Validate.
func New ¶ added in v1.0.0
New returns a Validator for structPtr, which must be a non-nil pointer to a struct.
func (*Validator) Validate ¶ added in v1.0.0
func (v *Validator) Validate() *ValidationResult
Validate runs every rule against the struct's current values.