Documentation
¶
Index ¶
- func Validate(v any) error
- type And
- type Any
- type Base64
- type CIDR
- type Charset
- type Charset0
- type CountryAlpha
- type CountryAlpha2
- type CountryAlpha3
- type CurrencyAlpha
- type Email
- type Even
- type HTTPURL
- type IP
- type IPV4
- type IPV6
- type InFuture
- type InPast
- type InvalidValidateError
- type JSON
- type LangAlpha
- type LangAlpha2
- type LangAlpha3
- type Latitude
- type Longitude
- type MAC
- type MIME
- type Negative
- type Negative0
- type NonEmpty
- type NonEmptySlice
- type NonZero
- type Not
- type Odd
- type Or
- type Positive
- type Positive0
- type TypeValidateable
- type URL
- type UUID
- type Unique
- type UniqueSlice
- type Validateable
- type ValidationError
- type Validator
- type Zero
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Validate ¶
Validate checks if the provided value can be validated and reports any validation errors.
The validation process follows these steps:
- If v implements both TypeValidateable and Validateable interfaces, its [TypeValidateable.TypeValidate] method is called, followed by its [Validateable.Validate] method.
- If v only implements the TypeValidateable interface, its [TypeValidateable.TypeValidate] method is called.
- Otherwise, Validate traverses all fields in the struct pointed to by v, applying the same validation logic to each field.
A ValidationError is returned if any validation fails during any step.
If v is nil or not a pointer, Validate returns an InvalidValidateError.
Types ¶
type And ¶
And is a meta validator that combines other validators with AND operator. Validators are called in the same order as specified by type parameters.
type CIDR ¶
type CIDR[T constraint.Text] struct{}
CIDR accepts CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC 4632 and RFC 4291.
type Charset ¶
type Charset[T constraint.Text, F charset.Filter] struct{}
Charset accepts non-empty text which contains only runes acceptable by filter.
See also Charset0
type Charset0 ¶
type Charset0[T constraint.Text, F charset.Filter] struct{}
Charset0 accepts (possibly empty) text which contains only runes acceptable by filter.
See Charset for a non-empty variant.
type CountryAlpha ¶
type CountryAlpha[T constraint.Text] struct { Or[T, CountryAlpha2[T], CountryAlpha3[T]] }
CountryAlpha2 accepts either CountryAlpha2 or CountryAlpha3
type CountryAlpha2 ¶
type CountryAlpha2[T constraint.Text] struct{}
CountryAlpha2 accepts case-insensitive ISO 3166 2-letter country code
type CountryAlpha3 ¶
type CountryAlpha3[T constraint.Text] struct{}
CountryAlpha2 accepts case-insensitive ISO 3166 3-letter country code
type CurrencyAlpha ¶
type CurrencyAlpha[T constraint.Text] struct{}
CurrencyAlpha accepts case-insensitive ISO 4217 alphabetic currency code
type Email ¶
type Email[T constraint.Text] struct{}
Email accepts a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>".
type HTTPURL ¶
type HTTPURL[T constraint.Text] struct{}
HTTPURL accepts a single http(s) url.
See also URL.
type IP ¶
type IP[T constraint.Text] struct{}
IP accepts an IP address. The address can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
type IPV6 ¶
type IPV6[T constraint.Text] struct{}
IP accepts an IP V6 address, including IPv4-mapped IPv6 addresses. The address can be regular IPv6 ("2001:db8::68"), or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
type InFuture ¶
type InFuture[T constraint.Time] struct{}
InFuture accepts any time after current timestamp
See also InPast
type InPast ¶
type InPast[T constraint.Time] struct{}
InPast accepts any time before current timestamp
See also InFuture
type InvalidValidateError ¶
InvalidValidateError describes an invalid argument passed to Validate. (The argument to Validate must be a non-nil pointer.)
func (InvalidValidateError) Error ¶
func (e InvalidValidateError) Error() string
type LangAlpha ¶
type LangAlpha[T constraint.Text] struct { Or[T, LangAlpha2[T], LangAlpha3[T]] }
LangAlpha accepts either LangAlpha2 or LangAlpha3
type LangAlpha2 ¶
type LangAlpha2[T constraint.Text] struct{}
LangAlpha2 accepts case-insesitive ISO 639 2-letter language code.
type LangAlpha3 ¶
type LangAlpha3[T constraint.Text] struct{}
LangAlpha3 accepts case-insesitive ISO 639 3-letter language code.
type Latitude ¶
type Latitude[T constraint.Real] struct{}
Latitude accepts any number in the range [-90; 90]
See also Longitude
type Longitude ¶
type Longitude[T constraint.Real] struct{}
Longitude accepts any number in the range [-180; 180]
See also Latitude
type MAC ¶
type MAC[T constraint.Text] struct{}
MAC accepts an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address.
type Negative ¶
type Negative[T constraint.Real] struct{}
Negative accepts all negative real numbers excluding zero.
See Negative0 for zero including variant.
type Negative0 ¶
type Negative0[T constraint.Real] struct { Or[T, Negative[T], Zero[T]] }
Negative0 accepts all negative real numbers including zero.
See Negative for zero excluding variant.
type NonEmpty ¶
type NonEmpty[S ~[]T, T any] struct{}
NonEmpty accepts a non-empty slice-like (len > 0)
See NonEmptySlice for a slice shortcut
type NonEmptySlice ¶
NonEmpty accepts a non-empty slice (len > 0)
See NonEmpty for a more generic version
type NonZero ¶
type NonZero[T comparable] struct{}
NonZero accepts all non-zero values.
The zero value is: - 0 for numeric types, - false for the boolean type, and - "" (the empty string) for strings.
See Zero
type Or ¶
And is a meta validator that combines other validators with OR operator. Validators are called in the same order as type parameters.
type Positive ¶
type Positive[T constraint.Real] struct{}
Positive accepts all positive real numbers excluding zero.
See Positive0 for zero inlcuding variant.
type Positive0 ¶
type Positive0[T constraint.Real] struct { Or[T, Positive[T], Zero[T]] }
Positive0 accepts all positive real numbers including zero.
See Positive for zero excluding variant.
type TypeValidateable ¶
type TypeValidateable interface {
TypeValidate() error
}
TypeValidateable is an interface for types that can validate their types. This is used by required and optional fields so that they can validate if contained values satisfy the schema enforced by Validator backed type.
TL;DR: do not implement nor use this method directly (codegen is exception).
See Validateable interface if you want to implement custom validation
type URL ¶
type URL[T constraint.Text] struct{}
URL accepts a single url. The url may be relative (a path, without a host) or absolute (starting with a scheme).
See also HTTPURL.
type UUID ¶
type UUID[T constraint.Text] struct{}
UUID accepts a properly formatted UUID in one of the following formats:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
type Unique ¶
type Unique[S ~[]T, T comparable] struct{}
Unique accepts a slice-like of unique values
See UniqueSlice for a slice shortcut
type UniqueSlice ¶
type UniqueSlice[T comparable] struct { Unique[[]T, T] }
Unique accepts a slice of unique values
See Unique for a more generic version
type Validateable ¶
type Validateable interface {
Validate() error
}
Validateable is an interface for types that can perform validation logic after type validation (by TypeValidateable) has been called without errors.
Primary usecase is custom cross-field validation. E.g. if X is true then Y cannot be empty
type ValidationError ¶
ValidationError describes validation error occured at validate.Validate
func (ValidationError) Error ¶
func (e ValidationError) Error() string
func (ValidationError) Path ¶
func (e ValidationError) Path() string
Path returns the path to the value which raised this error
func (ValidationError) Unwrap ¶
func (e ValidationError) Unwrap() error
func (ValidationError) WithPath ¶
func (e ValidationError) WithPath(path string) ValidationError
WithPath returns a copy of ValidationError with the given path set