Documentation ¶
Index ¶
- Constants
- func Chain[T any](name string, value T, validateFuncs ...ValidateFunc[T]) error
- func IsError(err error) bool
- func Join(errs ...error) error
- func Join2(err0, err1 error) error
- func Join3(err0, err1, err2 error) error
- func NamedCheck(checkName string, err error) error
- func Nested(name string, err error) error
- func NestedValidate[T Validatable](name string, value T) (stop bool, err error)
- func Next[T any](string, T) (stop bool, err error)
- func OmitEmpty[T comparable](_ string, value T) (stop bool, err error)
- func Required[T comparable](name string, value T) (stop bool, err error)
- func UnnamedValidate[T Validatable](_ string, value T) (stop bool, err error)
- type Error
- func (err Error) As(target any) bool
- func (err Error) Error() string
- func (err Error) Is(target error) bool
- func (err Error) ValueAsString() string
- func (err Error) WithNamedValue(name string, value any) Error
- func (err Error) WithParameter(parameter string) Error
- func (err Error) WithValue(value any) Error
- func (err Error) WithValueName(name string) Error
- type Errors
- type Validatable
- type ValidateFunc
- func NamedCheckFunc[T any](checkName string, validateFuncs ...ValidateFunc[T]) ValidateFunc[T]
- func Or[T any](validateFuncs ...ValidateFunc[T]) ValidateFunc[T]
- func Or2[T any](validateFunc0, validateFunc1 ValidateFunc[T]) ValidateFunc[T]
- func Or3[T any](validateFunc0, validateFunc1, validateFunc2 ValidateFunc[T]) ValidateFunc[T]
- type Zeroer
Constants ¶
const ( CheckNameRequired = "required" CheckNameRequiredIf = "required_if" CheckNameRequiredUnless = "required_unless" CheckNameRequiredWithAny = "required_with" CheckNameRequiredWithoutAny = "required_without" CheckNameRequiredWithAll = "required_with_all" CheckNameRequiredWithoutAll = "required_without_all" CheckNameExcludedIf = "excluded_if" CheckNameExcludedUnless = "excluded_unless" CheckNameExcludedWithAny = "excluded_with" CheckNameExcludedWithoutAny = "excluded_without" CheckNameExcludedWithAll = "excluded_with_all" CheckNameExcludedWithoutAll = "excluded_without_all" CheckNameMin = "min" CheckNameMax = "max" CheckNameGreaterThan = "gt" CheckNameGreaterThanOrEqual = "gte" CheckNameLessThan = "lt" CheckNameLessThanOrEqual = "lte" CheckNameGreaterThanNamed = "gtfield" CheckNameGreaterThanOrEqualNamed = "gtefield" CheckNameLessThanNamed = "ltfield" CheckNameLessThanOrEqualNamed = "ltefield" CheckNameEqual = "eq" CheckNameNotEqual = "ne" CheckNameOneOf = "oneof" CheckNameUnique = "unique" CheckNameEmail = "email" CheckNameE164 = "e164" CheckNameUUID = "uuid" CheckNameURI = "uri" CheckNameURL = "url" CheckNameHostname = "hostname" // RFC 952 CheckNameHostnameRFC1123 = "hostname_rfc1123" // RFC 1123, DNS name CheckNameHostnamePort = "hostname_port" // [RFC 1123]:<port> CheckNameFQDN = "fqdn" // RFC 1123, but must contain a non-numerical TLD CheckNameRegexp = "regexp" CheckNameBase64 = "base64" CheckNameBase64Raw = "base64raw" CheckNameBase64URL = "base64url" CheckNameBase64RawURL = "base64rawurl" CheckNameAlpha = "alpha" CheckNameAlphanumeric = "alphanum" CheckNameNumeric = "numeric" CheckNameLowercase = "lowercase" CheckNameUppercase = "uppercase" CheckNameContainsAlpha = "contains_alpha" CheckNameContainsLowerAlpha = "contains_lower_alpha" CheckNameContainsUpperAlpha = "contains_upper_alpha" CheckNameContainsDigit = "contains_digit" CheckNameContainsSpecialCharacter = "contains_special_character" CheckNameExcludesWhitespace = "excludes_whitespace" CheckNameStartsWithAlpha = "starts_with_alpha" CheckNameStartsWithLowerAlpha = "starts_with_lower_alpha" CheckNameStartsWithUpperAlpha = "starts_with_upper_alpha" CheckNameStartsWithDigit = "starts_with_digit" CheckNameStartsWithSpecialCharacter = "starts_with_special_character" CheckNameEndsWithAlpha = "ends_with_alpha" CheckNameEndsWithLowerAlpha = "ends_with_lower_alpha" CheckNameEndsWithUpperAlpha = "ends_with_upper_alpha" CheckNameEndsWithDigit = "ends_with_digit" CheckNameEndsWithSpecialCharacter = "ends_with_special_character" CheckNameText = "text" CheckNameTitle = "title" )
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain[T any](name string, value T, validateFuncs ...ValidateFunc[T]) error
Chain allows chaining validation funcs against a single struct field or value. If not nil, the result is always of Errors type.
func Join ¶ added in v0.10.0
Join returns combined Errors or nil. It is useful to combine Chain results, while validating multiple values.
func NamedCheck ¶ added in v0.13.7
NamedCheck processes errors of either Error or Errors type, clearing Error.Parameter and replacing Error.CheckName with the given name. Unsupported and nil errors are returned as is.
func Nested ¶ added in v0.1.1
Nested processes errors of either Error or Errors type, prepending Error.ValueName with name argument. It returns unsupported and nil errors as is.
func NestedValidate ¶ added in v0.8.5
func NestedValidate[T Validatable](name string, value T) (stop bool, err error)
NestedValidate basically equals to UnnamedValidate, but additionally calls Nested before returning the error.
func OmitEmpty ¶ added in v0.11.4
func OmitEmpty[T comparable](_ string, value T) (stop bool, err error)
OmitEmpty skips further validation, when a generic comparable value is default for its type. Most of the validation packages contain specialized versions of this function, e.g. vstring.OmitEmpty, etc.
func Required ¶ added in v0.16.3
func Required[T comparable](name string, value T) (stop bool, err error)
Required checks a generic comparable value against the default for its type. Most of the validation packages contain specialized versions of this function, e.g. vstring.Required, etc.
func UnnamedValidate ¶ added in v0.8.5
func UnnamedValidate[T Validatable](_ string, value T) (stop bool, err error)
UnnamedValidate is a ValidateFunc that simply calls Validatable.Validate of the given value. It may be useful, while validating slice items or map entries.
Types ¶
type Error ¶
func ErrRequired ¶ added in v0.13.8
func (Error) ValueAsString ¶
func (Error) WithParameter ¶
func (Error) WithValueName ¶
type Errors ¶ added in v0.10.0
type Validatable ¶ added in v0.8.0
type Validatable interface {
Validate() error
}
type ValidateFunc ¶
ValidateFunc usually represents a single validation check against the given named value.
func NamedCheckFunc ¶ added in v0.14.2
func NamedCheckFunc[T any](checkName string, validateFuncs ...ValidateFunc[T]) ValidateFunc[T]
NamedCheckFunc combines the given validation funcs into a new named one. Those functions are invoked similarly to Chain, then NamedCheck is applied to the result.
func Or ¶
func Or[T any](validateFuncs ...ValidateFunc[T]) ValidateFunc[T]
Or combines the given validation funcs into a new one, which iterates over and sequentially invokes the arguments. When any of the functions returns a nil error, its result is immediately returned. Otherwise, a non-nil error and stop flag of the last function are returned.
Constructing and dropping intermediate errors is somewhat expensive, so that use Or with care. If performance is crucial, write your own validation func returning an error if and only if all the checks fail.
func Or2 ¶ added in v0.8.1
func Or2[T any](validateFunc0, validateFunc1 ValidateFunc[T]) ValidateFunc[T]
Or2 exactly equals to Or with two arguments, but makes one less memory allocation.
func Or3 ¶ added in v0.8.1
func Or3[T any](validateFunc0, validateFunc1, validateFunc2 ValidateFunc[T]) ValidateFunc[T]
Or3 exactly equals to Or with three arguments, but makes one less memory allocation.