Documentation ¶
Index ¶
- Constants
- Variables
- func Add(err error, errs ...error) error
- func Validate(errs ...error) error
- type Error
- func Alphanumeric(key, val string) *Error
- func ErrNotUnique(key string) *Error
- func Length[T ~string | ~[]any | ~map[any]any](key string, val T, n int) *Error
- func LengthBetween[T ~string | ~[]any | ~map[any]any](key string, val T, min, max int) *Error
- func Match(key, val string, r *regexp.Regexp) *Error
- func Max[T int | int64 | float64](key string, val T, max T) *Error
- func Min[T int | int64 | float64](key string, val T, min T) *Error
- func NewError(key, rule string, params ...any) *Error
- func NotEmpty[T ~string | ~[]any | ~map[any]any](key string, val T) *Error
- type Errors
Constants ¶
const ( RuleNotEmpty = "not_empty" RuleLength = "length" RuleLengthBetween = "length_between" RuleMin = "min" RuleMax = "max" RuleMatch = "match" RuleAlphanumeric = "alphanumeric" RuleUnique = "unique" )
Variables ¶
var ( MessageNotEmpty = "cannot be empty" MessageLength = "length must be %d" MessageLengthIn = "length must be between %d and %d" MessageMin = "must be %d or more" MessageMax = "must be %d or less" MessageMatch = "must match %s" MessageAlphanumeric = "must only contain letters a to z and digits" MessageUnique = "must be unique" ReAlphanumeric = regexp.MustCompile("^[a-zA-Z0-9]+$") )
Functions ¶
func Add ¶
Add is a convenience function to add zero or more validation errors to an Errors.
If err is nil, a new Errors wil be created and returned. If the given validation error is an Errors, it's validation errors will be added to err.
If err is not an Errors or the given validation errors are not an Errors or an Error, the function will panic.
Types ¶
type Error ¶
func Alphanumeric ¶
Alphanumeric checks if a given string only contains letters a to z, letters A to Z or digits.
err := okay.Match("issn", "1940-5758", regexp.MustCompile(`^[0-9]{4}-[0-9]{3}[0-9X]$`))
func ErrNotUnique ¶
ErrNotUnique is a convenience function to signal that a given key fails a uniqueness test.
func Length ¶
Length checks if the given string, slice or map has a given length.
err := okay.Length("keywords", []string{"childcare"}, 1)
func LengthBetween ¶
LengthBetween checks if the given string, slice or map has a length that is greater than or equal to min and less than or equal to max.
err := okay.Length("keywords", []string{"childcare"}, 0, 10)
func Match ¶
Match checks if the given string matches a regular expression.
err := okay.Match("issn", "1940-5758", regexp.MustCompile(`^[0-9]{4}-[0-9]{3}[0-9X]$`))
func Max ¶
Max checks if the given string, slice or map has a length that is less than or equal to max.
err := okay.Max("keywords", []string{"childcare"}, 10)
func Min ¶
Min checks if the given string, slice or map has a length that is greater than or equal to min.
err := okay.Min("keywords", []string{"childcare", "education"}, 1)
func NewError ¶
NewError constructs a new validation error. key represents the field or value that failed validation. There are no assumptions about the nature of this key, it could be a JSON pointer or the name of a (nested) form field.
func NotEmpty ¶
NotEmpty checks if the given string, slice or map is not empty.
err := okay.NotEmpty("keywords", []string{"childcare"})
func (*Error) WithMessage ¶
WithMessage sets a custom error message if the validation error is not nil.
type Errors ¶
type Errors struct {
Errors []*Error
}
func (*Errors) ErrorOrNil ¶
ErrorOrNil returns Errors as a (nil) error interface value.