Documentation
¶
Index ¶
- func Must[Value any](t *testing.T, value Value, validators ...Validator[Value])
- type BasicTypes
- type Errors
- type FieldDescriptor
- type FuncValidator
- type NumericTypes
- type Validator
- func DeepEqual[Value any](expected Value) Validator[Value]
- func Field[Struct any, Field any](descriptor FieldDescriptor[Struct, Field], validators ...Validator[Field]) Validator[*Struct]
- func Max[Value NumericTypes](max Value) Validator[Value]
- func MaxCount[Element any](maxLength int) Validator[[]Element]
- func MaxRunes(max int) Validator[string]
- func Min[Value NumericTypes](min Value) Validator[Value]
- func MinCount[Element any](minLength int) Validator[[]Element]
- func MinRunes(min int) Validator[string]
- func OneOf[Element comparable](values ...Element) Validator[Element]
- func Regex(regex *regexp.Regexp) Validator[string]
- func RunesExactly(length int) Validator[string]
- func Slice[Element any](validators ...Validator[Element]) Validator[[]Element]
- func Struct[Struct any](validators ...Validator[*Struct]) Validator[*Struct]
- func When[T any](condition func(T) bool, validators ...Validator[T]) Validator[T]
- func WhenNotEmpty(validators ...Validator[string]) Validator[string]
- func WhenNotNil[Struct any](validators ...Validator[*Struct]) Validator[*Struct]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BasicTypes ¶
type BasicTypes interface {
~string | ~bool | NumericTypes
}
BasicTypes represents types that are basic types
type FieldDescriptor ¶
type FuncValidator ¶
func (FuncValidator[T]) Validate ¶
func (f FuncValidator[T]) Validate(v T) Errors
type NumericTypes ¶
type NumericTypes interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~float32 | ~float64
}
NumericTypes represents types that support comparison operators
type Validator ¶
func DeepEqual ¶ added in v1.0.1
DeepEqual returns a validator that checks if the actual value is deeply equal to the expected value using reflect.DeepEqual.
func Field ¶
func Field[Struct any, Field any](descriptor FieldDescriptor[Struct, Field], validators ...Validator[Field]) Validator[*Struct]
Field returns a validator for a struct field described by `descriptor`. The returned validator extracts the field value from the parent struct and runs the provided validators; any errors are prefixed with the field name.
func Max ¶
func Max[Value NumericTypes](max Value) Validator[Value]
Max creates a condition that validates a numeric value is less than or equal to the maximum.
func MaxRunes ¶
MaxRunes returns a validator that ensures the string contains at most `max` runes (Unicode code points).
func Min ¶
func Min[Value NumericTypes](min Value) Validator[Value]
Min creates a condition that validates a numeric value is greater than or equal to the minimum.
func MinCount ¶
MinCount returns a validator that ensures a slice has at least `minLength` elements.
func MinRunes ¶
MinRunes returns a validator that ensures the string contains at least `min` runes (Unicode code points).
func OneOf ¶
func OneOf[Element comparable](values ...Element) Validator[Element]
OneOf returns a `Validator` that checks whether a value is one of the provided `values`.
func Regex ¶
Regex returns a validator that checks the given string matches the provided regular expression.
It panics if `regex` is nil.
func RunesExactly ¶
RunesExactly returns a validator that ensures the string contains exactly `length` runes (Unicode code points).
func Slice ¶
Slice returns a validator that applies the provided element validators to each element of a slice. If any element produces validation errors the returned result will contain those errors prefixed with the element index.
func Struct ¶
Struct returns a validator for pointer-to-struct values that runs the provided field validators and aggregates their errors.
func When ¶
When returns a conditional validator that runs the provided validators only when `condition` evaluates to true for the validated value.
func WhenNotEmpty ¶ added in v1.0.4
WhenNotEmpty returns a validator for string that runs the provided validators only when the string is not empty
func WhenNotNil ¶
WhenNotNil returns a validator for pointer-to-struct values that runs the provided validators only when the pointer is non-nil. This is a convenience wrapper around `When` that checks `v != nil` before executing the nested validators, preventing nil dereferences in validators that assume a non-nil receiver.