Documentation
¶
Overview ¶
Package validators provides a catalog of ready-made validation directives for the valex engine.
Directives are opt-in: importing this package registers nothing on its own. Register the ones you need against valex's "val" tag with valex.MustRegisterDirective, then validate with valex.ValidateStruct (or the valex/forms helpers):
valex.MustRegisterDirective(&validators.EmailValidator{})
valex.MustRegisterDirective(&validators.IntRangeValidator{})
type User struct {
Email string `val:"email"`
Age int `val:"rangeint,min=0,max=120"`
}
err := valex.ValidateStruct(&User{Email: "a@b.com", Age: 30})
Parameters are supplied in the tag after the directive name (for example "rangeint,min=0,max=120"). Where a directive takes a list, the values are pipe-separated (for example "oneof,values=a|b|c").
Catalog ¶
The "val" tag directives, grouped by the Go type of the field they validate. The Registers column names the type to pass to valex.RegisterDirective.
Tag Registers Params Description -------------- ----------------------------- ------------ ------------------------------------ -- int -- rangeint IntRangeValidator min, max inclusive range minint MinIntValidator min value >= min maxint MaxIntValidator max value <= max posint NonNegativeIntValidator - non-negative negint NonPositiveIntValidator - non-positive !zeroint NonZeroIntValidator - not zero oneofint OneOfIntValidator values one of a pipe-separated list -- float64 -- rangefloat Float64RangeValidator min, max inclusive range minfloat MinFloat64Validator min value >= min maxfloat MaxFloat64Validator max value <= max posfloat NonNegativeFloat64Validator - non-negative negfloat NonPositiveFloat64Validator - non-positive !zerofloat NonZeroFloat64Validator - not zero oneoffloat OneOfFloat64Validator values one of a pipe-separated list -- string -- url UrlValidator - valid absolute URL email EmailValidator - valid email address !empty NonEmptyStringValidator - non-empty min MinLengthValidator size length >= size max MaxLengthValidator size length <= size len LengthRangeValidator min, max length within [min, max] regex RegexValidator pattern matches regular expression prefix PrefixValidator value has prefix suffix SuffixValidator value has suffix contains ContainsValidator value contains substring oneof OneOfStringValidator values one of a pipe-separated list alphanum AlphaNumericValidator - alphanumeric mac MACAddressValidator - valid MAC address ip IpValidator - valid IP address ipv4 IPv4Validator - valid IPv4 address ipv6 IPv6Validator - valid IPv6 address hostname HostnameValidator - valid hostname cidr IPCIDRValidator - valid CIDR notation uuid UUIDValidator version (4) RFC 4122 UUID, optional version base64 Base64Validator - valid base64 (standard or raw) hex HexValidator - valid hex (optional 0x prefix) xml XMLValidator - well-formed XML json JSONValidator - valid JSON time TimeValidator format valid time for the layout (default RFC3339) -- time.Time -- !zerotime NonZeroTimeValidator - not zero beforetime TimeBeforeValidator before before the given RFC3339 time aftertime TimeAfterValidator after after the given RFC3339 time betweentime TimeBetweenValidator start, end within [start, end] (RFC3339) -- time.Duration -- posduration PositiveDurationValidator - positive !zeroduration NonZeroDurationValidator - not zero -- net.IP -- !zeroip NonZeroIPValidator - not zero/unspecified iprange IPRangeValidator start, end within [start, end] -- url.URL -- !zerourl NonZeroURLValidator - not the zero value
Alongside the tag directives, the package also offers generic programmatic validators that are not registered with the "val" tag: CmpRangeValidator and NonZeroValidator implement valex.Validator directly, and CompositeValidator chains several valex.Validator values into one.
Index ¶
- type AlphaNumericValidator
- type Base64Validator
- type CmpRangeValidator
- type CompositeValidator
- type ContainsValidator
- type EmailValidator
- type Float64RangeValidator
- type HexValidator
- type HostnameValidator
- type IPCIDRValidator
- type IPRangeValidator
- func (v *IPRangeValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *IPRangeValidator) Handle(val net.IP) (net.IP, error)
- func (v *IPRangeValidator) Mode() tagex.DirectiveMode
- func (v *IPRangeValidator) Name() string
- func (v *IPRangeValidator) Validate(val net.IP) error
- type IPv4Validator
- type IPv6Validator
- type IntRangeValidator
- type IpValidator
- type JSONValidator
- type LengthRangeValidator
- type MACAddressValidator
- type MaxFloat64Validator
- type MaxIntValidator
- type MaxLengthValidator
- type MinFloat64Validator
- type MinIntValidator
- type MinLengthValidator
- type NonEmptyStringValidator
- type NonNegativeFloat64Validator
- type NonNegativeIntValidator
- type NonPositiveFloat64Validator
- type NonPositiveIntValidator
- type NonZeroDurationValidator
- type NonZeroFloat64Validator
- type NonZeroIPValidator
- type NonZeroIntValidator
- type NonZeroTimeValidator
- type NonZeroURLValidator
- type NonZeroValidator
- type OneOfFloat64Validator
- func (v *OneOfFloat64Validator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *OneOfFloat64Validator) Handle(val float64) (float64, error)
- func (v *OneOfFloat64Validator) Mode() tagex.DirectiveMode
- func (v *OneOfFloat64Validator) Name() string
- func (v *OneOfFloat64Validator) Validate(val float64) error
- type OneOfIntValidator
- func (v *OneOfIntValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *OneOfIntValidator) Handle(val int) (int, error)
- func (v *OneOfIntValidator) Mode() tagex.DirectiveMode
- func (v *OneOfIntValidator) Name() string
- func (v *OneOfIntValidator) Validate(val int) error
- type OneOfStringValidator
- func (v *OneOfStringValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *OneOfStringValidator) Handle(val string) (string, error)
- func (v *OneOfStringValidator) Mode() tagex.DirectiveMode
- func (v *OneOfStringValidator) Name() string
- func (v *OneOfStringValidator) Validate(val string) error
- type PositiveDurationValidator
- type PrefixValidator
- type RegexValidator
- func (v *RegexValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *RegexValidator) Handle(val string) (string, error)
- func (v *RegexValidator) Mode() tagex.DirectiveMode
- func (v *RegexValidator) Name() string
- func (v *RegexValidator) Validate(val string) error
- type SuffixValidator
- type TimeAfterValidator
- func (v *TimeAfterValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *TimeAfterValidator) Handle(val time.Time) (time.Time, error)
- func (v *TimeAfterValidator) Mode() tagex.DirectiveMode
- func (v *TimeAfterValidator) Name() string
- func (v *TimeAfterValidator) Validate(val time.Time) error
- type TimeBeforeValidator
- func (v *TimeBeforeValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *TimeBeforeValidator) Handle(val time.Time) (time.Time, error)
- func (v *TimeBeforeValidator) Mode() tagex.DirectiveMode
- func (v *TimeBeforeValidator) Name() string
- func (v *TimeBeforeValidator) Validate(val time.Time) error
- type TimeBetweenValidator
- func (v *TimeBetweenValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *TimeBetweenValidator) Handle(val time.Time) (time.Time, error)
- func (v *TimeBetweenValidator) Mode() tagex.DirectiveMode
- func (v *TimeBetweenValidator) Name() string
- func (v *TimeBetweenValidator) Validate(val time.Time) error
- type TimeValidator
- func (v *TimeValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *TimeValidator) Handle(val string) (string, error)
- func (v *TimeValidator) Mode() tagex.DirectiveMode
- func (v *TimeValidator) Name() string
- func (v *TimeValidator) Validate(val string) error
- type UUIDValidator
- func (v *UUIDValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
- func (v *UUIDValidator) Handle(val string) (string, error)
- func (v *UUIDValidator) Mode() tagex.DirectiveMode
- func (v *UUIDValidator) Name() string
- func (v *UUIDValidator) Validate(val string) error
- type UrlValidator
- type XMLValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AlphaNumericValidator ¶
type AlphaNumericValidator struct{}
AlphaNumericValidator validates that a string contains only alphanumeric characters.
func (*AlphaNumericValidator) Handle ¶
func (v *AlphaNumericValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*AlphaNumericValidator) Mode ¶
func (v *AlphaNumericValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*AlphaNumericValidator) Name ¶
func (v *AlphaNumericValidator) Name() string
Name returns the directive identifier.
func (*AlphaNumericValidator) Validate ¶
func (v *AlphaNumericValidator) Validate(val string) error
Validate checks whether the value is alphanumeric.
type Base64Validator ¶
type Base64Validator struct{}
Base64Validator validates that a string is valid base64.
func (*Base64Validator) Handle ¶
func (v *Base64Validator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*Base64Validator) Mode ¶
func (v *Base64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*Base64Validator) Name ¶
func (v *Base64Validator) Name() string
Name returns the directive identifier.
func (*Base64Validator) Validate ¶
func (v *Base64Validator) Validate(val string) error
Validate checks whether the value is base64 encoded.
type CmpRangeValidator ¶
CmpRangeValidator validates that a value is within an inclusive range.
func (*CmpRangeValidator[T]) Validate ¶
func (v *CmpRangeValidator[T]) Validate(val T) error
Validate checks whether the value is within the configured range.
type CompositeValidator ¶
CompositeValidator validates a value by running multiple validators in order.
func (*CompositeValidator[T]) Validate ¶
func (cv *CompositeValidator[T]) Validate(val T) error
Validate checks the value against each validator in order.
type ContainsValidator ¶
type ContainsValidator struct {
Value string `param:"value"`
}
ContainsValidator validates that a string contains a substring.
func (*ContainsValidator) Handle ¶
func (v *ContainsValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*ContainsValidator) Mode ¶
func (v *ContainsValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*ContainsValidator) Name ¶
func (v *ContainsValidator) Name() string
Name returns the directive identifier.
func (*ContainsValidator) Validate ¶
func (v *ContainsValidator) Validate(val string) error
Validate checks whether the value contains the configured substring.
type EmailValidator ¶
type EmailValidator struct{}
EmailValidator validates that a string is a valid email address.
func (*EmailValidator) Handle ¶
func (v *EmailValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*EmailValidator) Mode ¶
func (v *EmailValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*EmailValidator) Name ¶
func (v *EmailValidator) Name() string
Name returns the directive identifier.
func (*EmailValidator) Validate ¶
func (v *EmailValidator) Validate(val string) error
Validate checks whether the value is a valid email address.
type Float64RangeValidator ¶
Float64RangeValidator validates that a float64 is within an inclusive range.
func (*Float64RangeValidator) Handle ¶
func (v *Float64RangeValidator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*Float64RangeValidator) Mode ¶
func (v *Float64RangeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*Float64RangeValidator) Name ¶
func (v *Float64RangeValidator) Name() string
Name returns the directive identifier.
func (*Float64RangeValidator) Validate ¶
func (v *Float64RangeValidator) Validate(val float64) error
Validate checks whether the value is within the configured range.
type HexValidator ¶
type HexValidator struct{}
HexValidator validates that a string is valid hex.
func (*HexValidator) Handle ¶
func (v *HexValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*HexValidator) Mode ¶
func (v *HexValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*HexValidator) Name ¶
func (v *HexValidator) Name() string
Name returns the directive identifier.
func (*HexValidator) Validate ¶
func (v *HexValidator) Validate(val string) error
Validate checks whether the value is a hex string.
type HostnameValidator ¶
type HostnameValidator struct{}
HostnameValidator validates that a string is a valid hostname.
func (*HostnameValidator) Handle ¶
func (v *HostnameValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*HostnameValidator) Mode ¶
func (v *HostnameValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*HostnameValidator) Name ¶
func (v *HostnameValidator) Name() string
Name returns the directive identifier.
func (*HostnameValidator) Validate ¶
func (v *HostnameValidator) Validate(val string) error
Validate checks whether the value is a valid hostname.
type IPCIDRValidator ¶
type IPCIDRValidator struct{}
IPCIDRValidator validates that a string is a valid CIDR notation.
func (*IPCIDRValidator) Handle ¶
func (v *IPCIDRValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*IPCIDRValidator) Mode ¶
func (v *IPCIDRValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*IPCIDRValidator) Name ¶
func (v *IPCIDRValidator) Name() string
Name returns the directive identifier.
func (*IPCIDRValidator) Validate ¶
func (v *IPCIDRValidator) Validate(val string) error
Validate checks whether the value is a valid CIDR.
type IPRangeValidator ¶
IPRangeValidator validates that a net.IP is within an inclusive range.
func (*IPRangeValidator) ConvertParam ¶
func (v *IPRangeValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the start/end parameters.
func (*IPRangeValidator) Mode ¶
func (v *IPRangeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*IPRangeValidator) Name ¶
func (v *IPRangeValidator) Name() string
Name returns the directive identifier.
type IPv4Validator ¶
type IPv4Validator struct{}
IPv4Validator validates that a string is a valid IPv4 address.
func (*IPv4Validator) Handle ¶
func (v *IPv4Validator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*IPv4Validator) Mode ¶
func (v *IPv4Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*IPv4Validator) Name ¶
func (v *IPv4Validator) Name() string
Name returns the directive identifier.
func (*IPv4Validator) Validate ¶
func (v *IPv4Validator) Validate(val string) error
Validate checks whether the value is a valid IPv4 address.
type IPv6Validator ¶
type IPv6Validator struct{}
IPv6Validator validates that a string is a valid IPv6 address.
func (*IPv6Validator) Handle ¶
func (v *IPv6Validator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*IPv6Validator) Mode ¶
func (v *IPv6Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*IPv6Validator) Name ¶
func (v *IPv6Validator) Name() string
Name returns the directive identifier.
func (*IPv6Validator) Validate ¶
func (v *IPv6Validator) Validate(val string) error
Validate checks whether the value is a valid IPv6 address.
type IntRangeValidator ¶
IntRangeValidator validates that an int is within an inclusive range.
func (*IntRangeValidator) Handle ¶
func (v *IntRangeValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*IntRangeValidator) Mode ¶
func (v *IntRangeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*IntRangeValidator) Name ¶
func (v *IntRangeValidator) Name() string
Name returns the directive identifier.
func (*IntRangeValidator) Validate ¶
func (v *IntRangeValidator) Validate(val int) error
Validate checks whether the value is within the configured range.
type IpValidator ¶
type IpValidator struct{}
IpValidator validates that a string is a valid IP address.
func (*IpValidator) Handle ¶
func (v *IpValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*IpValidator) Mode ¶
func (v *IpValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*IpValidator) Name ¶
func (v *IpValidator) Name() string
Name returns the directive identifier.
func (*IpValidator) Validate ¶
func (v *IpValidator) Validate(val string) error
Validate checks whether the value is a valid IP address.
type JSONValidator ¶
type JSONValidator struct{}
JSONValidator validates that a string is valid JSON.
func (*JSONValidator) Handle ¶
func (v *JSONValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*JSONValidator) Mode ¶
func (v *JSONValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*JSONValidator) Name ¶
func (v *JSONValidator) Name() string
Name returns the directive identifier.
func (*JSONValidator) Validate ¶
func (v *JSONValidator) Validate(val string) error
Validate checks whether the value is valid JSON.
type LengthRangeValidator ¶
LengthRangeValidator validates that a string length is within an inclusive range.
func (*LengthRangeValidator) Handle ¶
func (v *LengthRangeValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*LengthRangeValidator) Mode ¶
func (v *LengthRangeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*LengthRangeValidator) Name ¶
func (v *LengthRangeValidator) Name() string
Name returns the directive identifier.
func (*LengthRangeValidator) Validate ¶
func (v *LengthRangeValidator) Validate(val string) error
Validate checks whether the value length is within the configured range.
type MACAddressValidator ¶
type MACAddressValidator struct{}
MACAddressValidator validates that a string is a valid MAC address.
func (*MACAddressValidator) Handle ¶
func (v *MACAddressValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*MACAddressValidator) Mode ¶
func (v *MACAddressValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MACAddressValidator) Name ¶
func (v *MACAddressValidator) Name() string
Name returns the directive identifier.
func (*MACAddressValidator) Validate ¶
func (v *MACAddressValidator) Validate(val string) error
Validate checks whether the value is a valid MAC address.
type MaxFloat64Validator ¶
type MaxFloat64Validator struct {
Max float64 `param:"max"`
}
MaxFloat64Validator validates that a float64 is less than or equal to Max.
func (*MaxFloat64Validator) Handle ¶
func (v *MaxFloat64Validator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*MaxFloat64Validator) Mode ¶
func (v *MaxFloat64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MaxFloat64Validator) Name ¶
func (v *MaxFloat64Validator) Name() string
Name returns the directive identifier.
func (*MaxFloat64Validator) Validate ¶
func (v *MaxFloat64Validator) Validate(val float64) error
Validate checks whether the value meets the maximum.
type MaxIntValidator ¶
type MaxIntValidator struct {
Max int `param:"max"`
}
MaxIntValidator validates that an int is less than or equal to Max.
func (*MaxIntValidator) Handle ¶
func (v *MaxIntValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*MaxIntValidator) Mode ¶
func (v *MaxIntValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MaxIntValidator) Name ¶
func (v *MaxIntValidator) Name() string
Name returns the directive identifier.
func (*MaxIntValidator) Validate ¶
func (v *MaxIntValidator) Validate(val int) error
Validate checks whether the value meets the maximum.
type MaxLengthValidator ¶
type MaxLengthValidator struct {
Size int `param:"size"`
}
MaxLengthValidator validates that a string does not exceed a maximum length.
func (*MaxLengthValidator) Handle ¶
func (v *MaxLengthValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*MaxLengthValidator) Mode ¶
func (v *MaxLengthValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MaxLengthValidator) Name ¶
func (v *MaxLengthValidator) Name() string
Name returns the directive identifier.
func (*MaxLengthValidator) Validate ¶
func (v *MaxLengthValidator) Validate(val string) error
Validate checks whether the value does not exceed the maximum length.
type MinFloat64Validator ¶
type MinFloat64Validator struct {
Min float64 `param:"min"`
}
MinFloat64Validator validates that a float64 is greater than or equal to Min.
func (*MinFloat64Validator) Handle ¶
func (v *MinFloat64Validator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*MinFloat64Validator) Mode ¶
func (v *MinFloat64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MinFloat64Validator) Name ¶
func (v *MinFloat64Validator) Name() string
Name returns the directive identifier.
func (*MinFloat64Validator) Validate ¶
func (v *MinFloat64Validator) Validate(val float64) error
Validate checks whether the value meets the minimum.
type MinIntValidator ¶
type MinIntValidator struct {
Min int `param:"min"`
}
MinIntValidator validates that an int is greater than or equal to Min.
func (*MinIntValidator) Handle ¶
func (v *MinIntValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*MinIntValidator) Mode ¶
func (v *MinIntValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MinIntValidator) Name ¶
func (v *MinIntValidator) Name() string
Name returns the directive identifier.
func (*MinIntValidator) Validate ¶
func (v *MinIntValidator) Validate(val int) error
Validate checks whether the value meets the minimum.
type MinLengthValidator ¶
type MinLengthValidator struct {
Size int `param:"size"`
}
MinLengthValidator validates that a string meets a minimum length.
func (*MinLengthValidator) Handle ¶
func (v *MinLengthValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*MinLengthValidator) Mode ¶
func (v *MinLengthValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*MinLengthValidator) Name ¶
func (v *MinLengthValidator) Name() string
Name returns the directive identifier.
func (*MinLengthValidator) Validate ¶
func (v *MinLengthValidator) Validate(val string) error
Validate checks whether the value meets the minimum length.
type NonEmptyStringValidator ¶
type NonEmptyStringValidator struct{}
NonEmptyStringValidator validates that a string is not empty.
func (*NonEmptyStringValidator) Handle ¶
func (v *NonEmptyStringValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*NonEmptyStringValidator) Mode ¶
func (v *NonEmptyStringValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonEmptyStringValidator) Name ¶
func (v *NonEmptyStringValidator) Name() string
Name returns the directive identifier.
func (*NonEmptyStringValidator) Validate ¶
func (v *NonEmptyStringValidator) Validate(val string) error
Validate checks whether the value is non-empty.
type NonNegativeFloat64Validator ¶
type NonNegativeFloat64Validator struct{}
NonNegativeFloat64Validator validates that a float64 is not negative.
func (*NonNegativeFloat64Validator) Handle ¶
func (v *NonNegativeFloat64Validator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*NonNegativeFloat64Validator) Mode ¶
func (v *NonNegativeFloat64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonNegativeFloat64Validator) Name ¶
func (v *NonNegativeFloat64Validator) Name() string
Name returns the directive identifier.
func (*NonNegativeFloat64Validator) Validate ¶
func (v *NonNegativeFloat64Validator) Validate(val float64) error
Validate checks whether the value is non-negative.
type NonNegativeIntValidator ¶
type NonNegativeIntValidator struct{}
NonNegativeIntValidator validates that an int is not negative.
func (*NonNegativeIntValidator) Handle ¶
func (v *NonNegativeIntValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*NonNegativeIntValidator) Mode ¶
func (v *NonNegativeIntValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonNegativeIntValidator) Name ¶
func (v *NonNegativeIntValidator) Name() string
Name returns the directive identifier.
func (*NonNegativeIntValidator) Validate ¶
func (v *NonNegativeIntValidator) Validate(val int) error
Validate checks whether the value is non-negative.
type NonPositiveFloat64Validator ¶
type NonPositiveFloat64Validator struct{}
NonPositiveFloat64Validator validates that a float64 is not positive.
func (*NonPositiveFloat64Validator) Handle ¶
func (v *NonPositiveFloat64Validator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*NonPositiveFloat64Validator) Mode ¶
func (v *NonPositiveFloat64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonPositiveFloat64Validator) Name ¶
func (v *NonPositiveFloat64Validator) Name() string
Name returns the directive identifier.
func (*NonPositiveFloat64Validator) Validate ¶
func (v *NonPositiveFloat64Validator) Validate(val float64) error
Validate checks whether the value is non-positive.
type NonPositiveIntValidator ¶
type NonPositiveIntValidator struct{}
NonPositiveIntValidator validates that an int is not positive.
func (*NonPositiveIntValidator) Handle ¶
func (v *NonPositiveIntValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*NonPositiveIntValidator) Mode ¶
func (v *NonPositiveIntValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonPositiveIntValidator) Name ¶
func (v *NonPositiveIntValidator) Name() string
Name returns the directive identifier.
func (*NonPositiveIntValidator) Validate ¶
func (v *NonPositiveIntValidator) Validate(val int) error
Validate checks whether the value is non-positive.
type NonZeroDurationValidator ¶
type NonZeroDurationValidator struct{}
NonZeroDurationValidator validates that a time.Duration is not zero.
func (*NonZeroDurationValidator) Mode ¶
func (v *NonZeroDurationValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonZeroDurationValidator) Name ¶
func (v *NonZeroDurationValidator) Name() string
Name returns the directive identifier.
type NonZeroFloat64Validator ¶
type NonZeroFloat64Validator struct{}
NonZeroFloat64Validator validates that a float64 is not zero.
func (*NonZeroFloat64Validator) Handle ¶
func (v *NonZeroFloat64Validator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*NonZeroFloat64Validator) Mode ¶
func (v *NonZeroFloat64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonZeroFloat64Validator) Name ¶
func (v *NonZeroFloat64Validator) Name() string
Name returns the directive identifier.
func (*NonZeroFloat64Validator) Validate ¶
func (v *NonZeroFloat64Validator) Validate(val float64) error
Validate checks whether the value is non-zero.
type NonZeroIPValidator ¶
type NonZeroIPValidator struct{}
NonZeroIPValidator validates that a net.IP is not zero or unspecified.
func (*NonZeroIPValidator) Mode ¶
func (v *NonZeroIPValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonZeroIPValidator) Name ¶
func (v *NonZeroIPValidator) Name() string
Name returns the directive identifier.
type NonZeroIntValidator ¶
type NonZeroIntValidator struct{}
NonZeroIntValidator validates that an int is not zero.
func (*NonZeroIntValidator) Handle ¶
func (v *NonZeroIntValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*NonZeroIntValidator) Mode ¶
func (v *NonZeroIntValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonZeroIntValidator) Name ¶
func (v *NonZeroIntValidator) Name() string
Name returns the directive identifier.
func (*NonZeroIntValidator) Validate ¶
func (v *NonZeroIntValidator) Validate(val int) error
Validate checks whether the value is non-zero.
type NonZeroTimeValidator ¶
type NonZeroTimeValidator struct{}
NonZeroTimeValidator validates that a time.Time is not zero.
func (*NonZeroTimeValidator) Mode ¶
func (v *NonZeroTimeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonZeroTimeValidator) Name ¶
func (v *NonZeroTimeValidator) Name() string
Name returns the directive identifier.
type NonZeroURLValidator ¶
type NonZeroURLValidator struct{}
NonZeroURLValidator validates that a url.URL is not zero.
func (*NonZeroURLValidator) Mode ¶
func (v *NonZeroURLValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*NonZeroURLValidator) Name ¶
func (v *NonZeroURLValidator) Name() string
Name returns the directive identifier.
type NonZeroValidator ¶
type NonZeroValidator[T any] struct{}
NonZeroValidator validates that a value is not the zero value for its type.
func (*NonZeroValidator[T]) Validate ¶
func (v *NonZeroValidator[T]) Validate(val T) error
Validate checks whether the value is non-zero.
type OneOfFloat64Validator ¶
type OneOfFloat64Validator struct {
Values []float64 `param:"values"`
}
OneOfFloat64Validator validates that a float64 matches one of the configured values.
func (*OneOfFloat64Validator) ConvertParam ¶
func (v *OneOfFloat64Validator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the values parameter.
func (*OneOfFloat64Validator) Handle ¶
func (v *OneOfFloat64Validator) Handle(val float64) (float64, error)
Handle validates the value and returns it unchanged.
func (*OneOfFloat64Validator) Mode ¶
func (v *OneOfFloat64Validator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*OneOfFloat64Validator) Name ¶
func (v *OneOfFloat64Validator) Name() string
Name returns the directive identifier.
func (*OneOfFloat64Validator) Validate ¶
func (v *OneOfFloat64Validator) Validate(val float64) error
Validate checks whether the value is in the configured set.
type OneOfIntValidator ¶
type OneOfIntValidator struct {
Values []int `param:"values"`
}
OneOfIntValidator validates that an int matches one of the configured values.
func (*OneOfIntValidator) ConvertParam ¶
func (v *OneOfIntValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the values parameter.
func (*OneOfIntValidator) Handle ¶
func (v *OneOfIntValidator) Handle(val int) (int, error)
Handle validates the value and returns it unchanged.
func (*OneOfIntValidator) Mode ¶
func (v *OneOfIntValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*OneOfIntValidator) Name ¶
func (v *OneOfIntValidator) Name() string
Name returns the directive identifier.
func (*OneOfIntValidator) Validate ¶
func (v *OneOfIntValidator) Validate(val int) error
Validate checks whether the value is in the configured set.
type OneOfStringValidator ¶
type OneOfStringValidator struct {
Values []string `param:"values"`
}
OneOfStringValidator validates that a string matches one of the configured values.
func (*OneOfStringValidator) ConvertParam ¶
func (v *OneOfStringValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the values parameter.
func (*OneOfStringValidator) Handle ¶
func (v *OneOfStringValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*OneOfStringValidator) Mode ¶
func (v *OneOfStringValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*OneOfStringValidator) Name ¶
func (v *OneOfStringValidator) Name() string
Name returns the directive identifier.
func (*OneOfStringValidator) Validate ¶
func (v *OneOfStringValidator) Validate(val string) error
Validate checks whether the value is in the configured set.
type PositiveDurationValidator ¶
type PositiveDurationValidator struct{}
PositiveDurationValidator validates that a time.Duration is positive.
func (*PositiveDurationValidator) Mode ¶
func (v *PositiveDurationValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*PositiveDurationValidator) Name ¶
func (v *PositiveDurationValidator) Name() string
Name returns the directive identifier.
type PrefixValidator ¶
type PrefixValidator struct {
Value string `param:"value"`
}
PrefixValidator validates that a string has a given prefix.
func (*PrefixValidator) Handle ¶
func (v *PrefixValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*PrefixValidator) Mode ¶
func (v *PrefixValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*PrefixValidator) Name ¶
func (v *PrefixValidator) Name() string
Name returns the directive identifier.
func (*PrefixValidator) Validate ¶
func (v *PrefixValidator) Validate(val string) error
Validate checks whether the value has the configured prefix.
type RegexValidator ¶
RegexValidator validates that a string matches a regular expression.
func (*RegexValidator) ConvertParam ¶
func (v *RegexValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam compiles the regex pattern parameter.
func (*RegexValidator) Handle ¶
func (v *RegexValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*RegexValidator) Mode ¶
func (v *RegexValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*RegexValidator) Name ¶
func (v *RegexValidator) Name() string
Name returns the directive identifier.
func (*RegexValidator) Validate ¶
func (v *RegexValidator) Validate(val string) error
Validate checks whether the value matches the configured pattern.
type SuffixValidator ¶
type SuffixValidator struct {
Value string `param:"value"`
}
SuffixValidator validates that a string has a given suffix.
func (*SuffixValidator) Handle ¶
func (v *SuffixValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*SuffixValidator) Mode ¶
func (v *SuffixValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*SuffixValidator) Name ¶
func (v *SuffixValidator) Name() string
Name returns the directive identifier.
func (*SuffixValidator) Validate ¶
func (v *SuffixValidator) Validate(val string) error
Validate checks whether the value has the configured suffix.
type TimeAfterValidator ¶
TimeAfterValidator validates that a time.Time is after the configured time.
func (*TimeAfterValidator) ConvertParam ¶
func (v *TimeAfterValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the after parameter.
func (*TimeAfterValidator) Mode ¶
func (v *TimeAfterValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*TimeAfterValidator) Name ¶
func (v *TimeAfterValidator) Name() string
Name returns the directive identifier.
type TimeBeforeValidator ¶
TimeBeforeValidator validates that a time.Time is before the configured time.
func (*TimeBeforeValidator) ConvertParam ¶
func (v *TimeBeforeValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the before parameter.
func (*TimeBeforeValidator) Mode ¶
func (v *TimeBeforeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*TimeBeforeValidator) Name ¶
func (v *TimeBeforeValidator) Name() string
Name returns the directive identifier.
type TimeBetweenValidator ¶
TimeBetweenValidator validates that a time.Time is within an inclusive range.
func (*TimeBetweenValidator) ConvertParam ¶
func (v *TimeBetweenValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the start/end parameters.
func (*TimeBetweenValidator) Mode ¶
func (v *TimeBetweenValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*TimeBetweenValidator) Name ¶
func (v *TimeBetweenValidator) Name() string
Name returns the directive identifier.
type TimeValidator ¶
type TimeValidator struct {
Format string `param:"format,required=false"`
}
TimeValidator validates that a string matches a time layout. If Format is empty, time.RFC3339 is used.
func (*TimeValidator) ConvertParam ¶
func (v *TimeValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam maps well-known time layout names or accepts a raw layout string.
func (*TimeValidator) Handle ¶
func (v *TimeValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*TimeValidator) Mode ¶
func (v *TimeValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*TimeValidator) Name ¶
func (v *TimeValidator) Name() string
Name returns the directive identifier.
func (*TimeValidator) Validate ¶
func (v *TimeValidator) Validate(val string) error
Validate checks whether the value matches the configured layout.
type UUIDValidator ¶
type UUIDValidator struct {
Version int `param:"version,required=false"`
}
UUIDValidator validates that a string is a RFC 4122 UUID. If Version is 0, version 4 is assumed.
func (*UUIDValidator) ConvertParam ¶
func (v *UUIDValidator) ConvertParam(field reflect.StructField, fieldValue reflect.Value, raw string) error
ConvertParam parses the version parameter.
func (*UUIDValidator) Handle ¶
func (v *UUIDValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*UUIDValidator) Mode ¶
func (v *UUIDValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*UUIDValidator) Name ¶
func (v *UUIDValidator) Name() string
Name returns the directive identifier.
func (*UUIDValidator) Validate ¶
func (v *UUIDValidator) Validate(val string) error
Validate checks whether the value is a UUID.
type UrlValidator ¶
type UrlValidator struct{}
UrlValidator validates that a string is a valid URL.
func (*UrlValidator) Handle ¶
func (v *UrlValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*UrlValidator) Mode ¶
func (v *UrlValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*UrlValidator) Name ¶
func (v *UrlValidator) Name() string
Name returns the directive identifier.
func (*UrlValidator) Validate ¶
func (v *UrlValidator) Validate(val string) error
Validate checks whether the value is a valid URL.
type XMLValidator ¶
type XMLValidator struct{}
XMLValidator validates that a string is well-formed XML with at least one element.
func (*XMLValidator) Handle ¶
func (v *XMLValidator) Handle(val string) (string, error)
Handle validates the value and returns it unchanged.
func (*XMLValidator) Mode ¶
func (v *XMLValidator) Mode() tagex.DirectiveMode
Mode returns the directive evaluation mode.
func (*XMLValidator) Name ¶
func (v *XMLValidator) Name() string
Name returns the directive identifier.
func (*XMLValidator) Validate ¶
func (v *XMLValidator) Validate(val string) error
Validate checks whether the value is valid XML with at least one element.