Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/jacoelho/validation"
)
func main() {
// Define a user struct with slices and maps
type User struct {
Name string
Age int
Email string
Password string
Tags []string
Settings map[string]string
}
// Create a validator for the User struct
validator := validation.Struct(
validation.Field("Name", func(u User) string { return u.Name },
validation.NotZero[string](),
validation.StringsRuneMaxLength[string](50),
),
validation.Field("Age", func(u User) int { return u.Age },
validation.NumbersMin(18),
validation.NumbersMax(120),
),
validation.Field("Email", func(u User) string { return u.Email },
validation.NotZero[string](),
validation.StringsRuneMaxLength[string](100),
),
validation.Field("Password", func(u User) string { return u.Password },
validation.NotZero[string](),
validation.StringsRuneMinLength[string](8),
),
validation.SliceField("Tags", func(u User) []string { return u.Tags },
validation.SlicesMaxLength[string](5),
validation.SlicesForEach(
validation.NotZero[string](),
validation.StringsRuneMaxLength[string](20),
),
),
validation.MapField("Settings", func(u User) map[string]string { return u.Settings },
validation.MapsMaxKeys[string, string](5),
validation.MapsForEach(
func(k, v string) *validation.Error {
if v == "" {
return &validation.Error{
Code: "empty_value",
Field: k,
Params: map[string]any{"key": k},
}
}
return nil
},
),
validation.MapsKey("lang", validation.OneOf("en", "fr")),
),
)
// Validate a valid user
validUser := User{
Name: "John Doe",
Age: 30,
Email: "[email protected]",
Password: "secure123",
Tags: []string{"user", "premium"},
Settings: map[string]string{
"theme": "dark",
"lang": "en",
},
}
err := validator.Validate(validUser)
fmt.Println("Valid user errors:", len(err))
// Validate an invalid user
invalidUser := User{
Name: "",
Age: 15,
Email: "invalid-email",
Password: "short",
Tags: []string{"", "very_long_tag_that_exceeds_maximum_length"},
Settings: map[string]string{
"theme": "",
"lang": "en",
},
}
err = validator.Validate(invalidUser)
fmt.Println("Invalid user errors:", err)
}
Output: Valid user errors: 0 Invalid user errors: zero (field: Name); min (field: Age) {actual: 15, min: 18}; min (field: Password) {actual: 5, min: 8}; zero (field: Tags.0); max (field: Tags.1) {actual: 41, max: 20}; empty_value (field: Settings.theme) {key: theme}
Index ¶
- type Error
- type Errors
- type FieldAccessor
- func Field[T, F any](name string, getter func(T) F, rules ...Rule[F]) FieldAccessor[T, F]
- func MapField[T any, K comparable, V any](name string, getter func(T) map[K]V, rules ...MapRule[K, V]) FieldAccessor[T, map[K]V]
- func SliceField[T, E any](name string, getter func(T) []E, rules ...SliceRule[E]) FieldAccessor[T, []E]
- func StructField[T, F any](name string, getter func(T) F, validator *StructValidator[F]) FieldAccessor[T, F]
- type MapEntryRule
- type MapRule
- func MapsForEach[K comparable, V any](rules ...MapEntryRule[K, V]) MapRule[K, V]
- func MapsKey[K comparable, V any](key K, rules ...Rule[V]) MapRule[K, V]
- func MapsKeysNotOneOf[K comparable, V any](disallowed ...K) MapRule[K, V]
- func MapsKeysOneOf[K comparable, V any](allowed ...K) MapRule[K, V]
- func MapsLength[K comparable, V any](length int) MapRule[K, V]
- func MapsLengthBetween[K comparable, V any](min, max int) MapRule[K, V]
- func MapsMaxKeys[K comparable, V any](max int) MapRule[K, V]
- func MapsMinKeys[K comparable, V any](min int) MapRule[K, V]
- func MapsValuesNotOneOf[K comparable, V comparable](disallowed ...V) MapRule[K, V]
- func MapsValuesOneOf[K comparable, V comparable](allowed ...V) MapRule[K, V]
- type MapValidator
- type Rule
- func NotOneOf[T comparable](disallowed ...T) Rule[T]
- func NotZero[T comparable]() Rule[T]
- func NotZeroable[T interface{ ... }]() Rule[T]
- func NumbersBetween[T cmp.Ordered](min, max T) Rule[T]
- func NumbersMax[T cmp.Ordered](max T) Rule[T]
- func NumbersMin[T cmp.Ordered](min T) Rule[T]
- func NumbersNegative[T cmp.Ordered]() Rule[T]
- func NumbersNonNegative[T cmp.Ordered]() Rule[T]
- func NumbersNonPositive[T cmp.Ordered]() Rule[T]
- func NumbersPositive[T cmp.Ordered]() Rule[T]
- func OneOf[T comparable](allowed ...T) Rule[T]
- func Or[T any](rules ...Rule[T]) Rule[T]
- func RuleNot[T any](rule Rule[T]) Rule[T]
- func RuleStopOnError[T any](rule Rule[T]) Rule[T]
- func StringsContains[T ~string](substring T) Rule[T]
- func StringsMatchesRegex[T ~string](pattern string) Rule[T]
- func StringsRuneLengthBetween[T ~string](min, max int) Rule[T]
- func StringsRuneMaxLength[T ~string](max int) Rule[T]
- func StringsRuneMinLength[T ~string](min int) Rule[T]
- func TimeAfter(other time.Time) Rule[time.Time]
- func TimeAfterOrEqual(other time.Time) Rule[time.Time]
- func TimeBefore(other time.Time) Rule[time.Time]
- func TimeBeforeOrEqual(other time.Time) Rule[time.Time]
- func TimeBetween(min, max time.Time) Rule[time.Time]
- func Unless[T any](condition func(T) bool, rule Rule[T]) Rule[T]
- func When[T any](condition func(T) bool, rule Rule[T]) Rule[T]
- type SliceRule
- func SlicesAtIndex[T any](index int, rules ...Rule[T]) SliceRule[T]
- func SlicesContains[T comparable](value T) SliceRule[T]
- func SlicesForEach[T any](rules ...Rule[T]) SliceRule[T]
- func SlicesInBetweenLength[T any](min, max int) SliceRule[T]
- func SlicesLength[T any](length int) SliceRule[T]
- func SlicesMaxLength[T any](max int) SliceRule[T]
- func SlicesMinLength[T any](min int) SliceRule[T]
- func SlicesNotOneOf[T comparable](disallowed ...T) SliceRule[T]
- func SlicesOneOf[T comparable](allowed ...T) SliceRule[T]
- func SlicesUnique[T comparable]() SliceRule[T]
- type SliceValidator
- type StructValidator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Errors ¶
type Errors []*Error
Errors is a collection of validation errors.
func SingleErrorSlice ¶ added in v1.0.5
SingleErrorSlice creates a new Errors instance with a single error.
func (Errors) HasFatalErrors ¶
HasFatalErrors reports whether any fatal errors exist.
type FieldAccessor ¶
type FieldAccessor[T, F any] struct { // contains filtered or unexported fields }
FieldAccessor is a field of a struct.
func Field ¶
func Field[T, F any](name string, getter func(T) F, rules ...Rule[F]) FieldAccessor[T, F]
Field creates a new FieldAccessor with the given name, getter and rules.
func MapField ¶
func MapField[T any, K comparable, V any](name string, getter func(T) map[K]V, rules ...MapRule[K, V]) FieldAccessor[T, map[K]V]
MapField creates a new FieldAccessor with the given name, getter and rules.
func SliceField ¶
func SliceField[T, E any](name string, getter func(T) []E, rules ...SliceRule[E]) FieldAccessor[T, []E]
SliceField creates a new FieldAccessor with the given name, getter and rules.
func StructField ¶
func StructField[T, F any](name string, getter func(T) F, validator *StructValidator[F]) FieldAccessor[T, F]
StructField creates a new FieldAccessor with the given name, getter and validator.
func (FieldAccessor[T, F]) ValidateWithPrefix ¶
func (fa FieldAccessor[T, F]) ValidateWithPrefix(parent T, prefix string) Errors
ValidateWithPrefix validates the given value with a prefix.
type MapEntryRule ¶
type MapEntryRule[K comparable, V any] func(key K, value V) *Error
MapEntryRule is a function that validates an entry in a map.
type MapRule ¶
type MapRule[K comparable, V any] func(values map[K]V) Errors
MapRule is a function that validates a map of values.
func MapsForEach ¶
func MapsForEach[K comparable, V any](rules ...MapEntryRule[K, V]) MapRule[K, V]
MapsForEach validates each entry in the map using the given rules.
func MapsKey ¶ added in v1.0.2
func MapsKey[K comparable, V any](key K, rules ...Rule[V]) MapRule[K, V]
MapsKey validates the value of the given key.
func MapsKeysNotOneOf ¶ added in v1.0.5
func MapsKeysNotOneOf[K comparable, V any](disallowed ...K) MapRule[K, V]
MapsKeysNotOneOf validates that the map does not have the given keys.
func MapsKeysOneOf ¶ added in v1.0.5
func MapsKeysOneOf[K comparable, V any](allowed ...K) MapRule[K, V]
MapsKeysOneOf validates that the map has only the given keys.
func MapsLength ¶
func MapsLength[K comparable, V any](length int) MapRule[K, V]
func MapsLengthBetween ¶
func MapsLengthBetween[K comparable, V any](min, max int) MapRule[K, V]
func MapsMaxKeys ¶
func MapsMaxKeys[K comparable, V any](max int) MapRule[K, V]
MapsMaxKeys validates that the map has at most the given number of keys.
func MapsMinKeys ¶
func MapsMinKeys[K comparable, V any](min int) MapRule[K, V]
MapsMinKeys validates that the map has at least the given number of keys.
func MapsValuesNotOneOf ¶ added in v1.0.5
func MapsValuesNotOneOf[K comparable, V comparable](disallowed ...V) MapRule[K, V]
MapsValuesNotOneOf validates that the map does not have the given values.
func MapsValuesOneOf ¶ added in v1.0.5
func MapsValuesOneOf[K comparable, V comparable](allowed ...V) MapRule[K, V]
MapsValuesOneOf validates that the map has only the given values.
type MapValidator ¶
type MapValidator[K comparable, V any] struct { // contains filtered or unexported fields }
MapValidator is a validator for maps of values.
func Maps ¶ added in v1.0.1
func Maps[K comparable, V any](rules ...MapRule[K, V]) *MapValidator[K, V]
Maps creates a new MapValidator with the given rules.
func (*MapValidator[K, V]) Validate ¶
func (v *MapValidator[K, V]) Validate(values map[K]V) Errors
Validate validates the given values.
func (*MapValidator[K, V]) ValidateWithPrefix ¶
func (v *MapValidator[K, V]) ValidateWithPrefix(values map[K]V, prefix string) Errors
ValidateWithPrefix validates the given values with a prefix.
type Rule ¶
Rule is a function that validates a value.
func NotOneOf ¶ added in v1.0.5
func NotOneOf[T comparable](disallowed ...T) Rule[T]
NotOneOf validates that the value is not one of the given disallowed values.
func NotZero ¶ added in v1.0.5
func NotZero[T comparable]() Rule[T]
NotZero ensures the value is not the zero value for its type
func NotZeroable ¶ added in v1.0.5
NotZeroable ensures the value is not the zero value. The zero value is determined by the IsZero method.
func NumbersBetween ¶
NumbersBetween validates that the value is between the given minimum and maximum (inclusive).
func NumbersMax ¶
NumbersMax validates that the value is less than or equal to the given maximum.
func NumbersMin ¶
NumbersMin validates that the value is greater than or equal to the given minimum.
func NumbersNegative ¶
NumbersNegative validates that the value is less than 0.
func NumbersNonNegative ¶ added in v1.0.5
NumbersNonNegative validates that the value is greater than or equal to 0.
func NumbersNonPositive ¶ added in v1.0.5
NumbersNonPositive validates that the value is less than or equal to 0.
func NumbersPositive ¶
NumbersPositive validates that the value is greater than 0.
func OneOf ¶ added in v1.0.5
func OneOf[T comparable](allowed ...T) Rule[T]
OneOf validates that the value is one of the given allowed values.
func Or ¶
Or combines multiple rules, at least one must pass. If all rules fail, the last error is returned.
func RuleStopOnError ¶
RuleStopOnError stops the validation process if an error occurs.
func StringsContains ¶
StringsContains validates that the string contains the given substring.
func StringsMatchesRegex ¶
StringsMatchesRegex validates string against a regex pattern
func StringsRuneLengthBetween ¶
StringsRuneLengthBetween validates string length (in runes, not bytes) between the given minimum and maximum.
func StringsRuneMaxLength ¶
StringsRuneMaxLength validates maximum string length in runes.
func StringsRuneMinLength ¶
StringsRuneMinLength validates minimum string length in runes.
func TimeAfterOrEqual ¶ added in v1.0.5
TimeAfterOrEqual validates that the time is after the given time.
func TimeBefore ¶
TimeBefore validates that the time is before the given time.
func TimeBeforeOrEqual ¶ added in v1.0.5
TimeBeforeOrEqual validates that the time is before the given time.
func TimeBetween ¶
TimeBetween validates that the time is between the given times.
type SliceRule ¶
SliceRule is a function that validates a slice of values.
func SlicesAtIndex ¶ added in v1.0.4
SlicesAtIndex validates the value at the given index.
func SlicesContains ¶
func SlicesContains[T comparable](value T) SliceRule[T]
SlicesContains validates that the slice contains the given value.
func SlicesForEach ¶
SlicesForEach validates each value in the slice using the given rules.
func SlicesInBetweenLength ¶
SlicesInBetweenLength validates that the slice has between the given lengths.
func SlicesLength ¶
SlicesLength validates that the slice has the given length.
func SlicesMaxLength ¶
SlicesMaxLength validates that the slice has at most the given length.
func SlicesMinLength ¶
SlicesMinLength validates that the slice has at least the given length.
func SlicesNotOneOf ¶ added in v1.0.5
func SlicesNotOneOf[T comparable](disallowed ...T) SliceRule[T]
SlicesNotOneOf validates that the slice does not contain the given values.
func SlicesOneOf ¶ added in v1.0.5
func SlicesOneOf[T comparable](allowed ...T) SliceRule[T]
SlicesOneOf validates that the slice contains only the given values.
func SlicesUnique ¶
func SlicesUnique[T comparable]() SliceRule[T]
SlicesUnique validates that the slice has unique values.
type SliceValidator ¶
type SliceValidator[T any] struct { // contains filtered or unexported fields }
SliceValidator is a validator for slices of values.
func Slices ¶ added in v1.0.1
func Slices[T any](rules ...SliceRule[T]) *SliceValidator[T]
Slices creates a new SliceValidator with the given rules.
func (*SliceValidator[T]) Validate ¶
func (v *SliceValidator[T]) Validate(values []T) Errors
Validate validates the given values.
func (*SliceValidator[T]) ValidateWithPrefix ¶
func (v *SliceValidator[T]) ValidateWithPrefix(values []T, prefix string) Errors
ValidateWithPrefix validates the given values with a prefix.
type StructValidator ¶
type StructValidator[T any] struct { // contains filtered or unexported fields }
StructValidator is a validator for a struct.
func Struct ¶ added in v1.0.1
func Struct[T any](fields ...fieldValidator[T]) *StructValidator[T]
Struct creates a new StructValidator with the given fields.
func (*StructValidator[T]) Validate ¶
func (v *StructValidator[T]) Validate(value T) Errors
Validate validates the given value.
func (*StructValidator[T]) ValidateWithPrefix ¶
func (v *StructValidator[T]) ValidateWithPrefix(value T, prefix string) Errors
ValidateWithPrefix validates the given value with a prefix.