Documentation
¶
Index ¶
- Variables
- func All[T any](fs ...func(T) error) func(T) error
- func Chars(allow string) func(string) error
- func Email(value string) error
- func Equal[T comparable](cmp T) func(T) error
- func ExceptChars(disallow string) func(string) error
- func In[T comparable](allow ...T) func(T) error
- func Max(n int, exclusive bool) func(int) error
- func MaxFloat32(n float32, exclusive bool) func(float32) error
- func MaxFloat64(n float64, exclusive bool) func(float64) error
- func MaxLength(l int) func(string) error
- func MaxSize[T any](l int) func([]T) error
- func Min(n int, exclusive bool) func(int) error
- func MinFloat32(n float32, exclusive bool) func(float32) error
- func MinFloat64(n float64, exclusive bool) func(float64) error
- func MinLength(l int) func(string) error
- func MinSize[T any](l int) func([]T) error
- func NotIn[T comparable](allow ...T) func(T) error
- func URL(value string) error
- func UUID(value string) error
- type Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrMustBeLonger = NewError("must contain at least %d characters") ErrMustBeShorter = NewError("must contain no more than %d characters") )
var ( ErrMustBeGreater = NewError("must be greater than %v") ErrMustBeGreaterOrEqual = NewError("must be greater than or equal to %v") ErrMustBeLess = NewError("must be less than %v") ErrMustBeLessOrEqual = NewError("must be less than or equal to %v") )
var ( ErrMustHaveMoreItems = NewError("must have at least %d items") ErrMustHaveFewerItems = NewError("must have no more than %d items") )
var (
Err = Error{}
)
Validation error.
var (
ErrDisallowedChars = NewError("contains disallowed characters")
)
var (
ErrInvalidEmail = NewError("invalid email address")
)
var (
ErrInvalidUUID = NewError("invalid UUID")
)
var (
ErrNotEqual = NewError("must be equal to %v")
)
var (
ErrValueNotAllowed = NewError("not allowed")
)
Functions ¶
func All ¶
All validates a value using a sequence of validation functions. If any validation function returns an error, the sequence stops and the error is returned.
Example ¶
testAll := All(MinLength(4), Chars("0123456789abcdef")) fmt.Println(testAll("invalid input"))
Output: contains disallowed characters
func Chars ¶
Chars validates whether a string contains only allowed characters.
Example ¶
testChars := Chars("0123456789abcdef") fmt.Println(testChars("invalid input"))
Output: contains disallowed characters
func Email ¶
Email validates an email address.
Example ¶
fmt.Println(Email("not an email"))
Output: invalid email address
func Equal ¶
func Equal[T comparable](cmp T) func(T) error
Equal validates whether an input value is equal to a comparison value.
func ExceptChars ¶
ExceptChars validates whether a string does not contain disallowed characters.
Example ¶
testExceptChars := Chars("0123456789abcdef") fmt.Println(testExceptChars("invalid input"))
Output: contains disallowed characters
func In ¶
func In[T comparable](allow ...T) func(T) error
In validates whether a value is found in a slice of allowed values.
Example ¶
testIn := In("abc", "def", "xyz") fmt.Println(testIn("123"))
Output: not allowed
func Max ¶
Max validates whether an integer is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.
Example ¶
testMax := Max(10, true) fmt.Println(testMax(10))
Output: must be less than 10
func MaxFloat32 ¶
MaxFloat32 validates whether a float32 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.
func MaxFloat64 ¶
MaxFloat64 validates whether a float64 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.
func MaxLength ¶
MaxLength validates the length of a string as being less than or equal to a given maximum.
Example ¶
testMaxLength := MaxLength(8) fmt.Println(testMaxLength("this string is too long"))
Output: must contain no more than 8 characters
func MaxSize ¶
MaxSize validates the length of a slice as being less than or equal to a given maximum.
Example ¶
testMaxSize := MaxSize[string](3) fmt.Println(testMaxSize([]string{"abc", "def", "ghi", "jkl"}))
Output: must have no more than 3 items
func Min ¶
Min validates whether an integer is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.
Example ¶
testMin := Min(10, false) fmt.Println(testMin(5))
Output: must be greater than or equal to 10
func MinFloat32 ¶
MinFloat32 validates whether a float32 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.
func MinFloat64 ¶
MinFloat64 validates whether a float64 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.
func MinLength ¶
MinLength validates the length of a string as being greater than or equal to a given minimum.
Example ¶
testMinLength := MinLength(8) fmt.Println(testMinLength("2short"))
Output: must contain at least 8 characters
func MinSize ¶
MinSize validates the length of a slice as being greater than or equal to a given minimum.
Example ¶
testMaxSize := MinSize[string](3) fmt.Println(testMaxSize([]string{"abc", "def"}))
Output: must have at least 3 items
func NotIn ¶
func NotIn[T comparable](allow ...T) func(T) error
NotIn validates whether a value is not found in a slice of disallowed values.
Types ¶
type Error ¶
Error represents a validation error.
func (Error) Error ¶
Error retrieves the message of a validation Error. If it has Data, the message will be formatted.
func (Error) Is ¶
Is determines whether the Error is an instance of the target. https://pkg.go.dev/errors#Is
If the target is a validation error and specifies a message, this function returns true if the messages match. If the target is an empty validation error, this function always returns true.