Documentation
¶
Overview ¶
Package valex provides generic validators and tag-based validation directives.
It supports two primary workflows:
- Programmatic validation using Validator or ValidatorFunc along with ValidatedValue for guarded assignment.
- Struct tag validation using the "val" tag and ValidateStruct. You can pass additional tagex.Tag values to ValidateStruct to process multiple tags in a single pass.
For HTTP form binding, FormValidator parses requests and binds "field" tags before running validation, and ValidateForm provides a convenience wrapper with HTTP status mapping.
The built-in directives cover common validations (ranges, lengths, URLs, emails, IPs, JSON/XML, and regex). You can extend tag validation by registering custom directives with RegisterDirective.
Index ¶
- Variables
- func BindFormValues(dst any, values url.Values) error
- func FormStatus(err error) int
- func MustValidate[T any](val T, v Validator[T]) T
- func RegisterDirective[T any](d tagex.Directive[T])
- func ValidateForm(r *http.Request, dst any) (bool, error)
- func ValidateStruct(data interface{}, tags ...*tagex.Tag) (bool, error)
- type AlphaNumericValidator
- type Base64Validator
- type CmpRangeValidator
- type CompositeValidator
- type ContainsValidator
- type EmailValidator
- type Float64RangeValidator
- type FormError
- type FormValidator
- 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) (ok bool, err 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 NonEmptyStringAliasValidator
- type NonEmptyStringValidator
- type NonNegativeFloat64Validator
- type NonNegativeIntValidator
- type NonPositiveFloat64Validator
- type NonPositiveIntValidator
- type NonZeroDurationAliasValidator
- type NonZeroDurationValidator
- type NonZeroFloat64AliasValidator
- type NonZeroFloat64Validator
- type NonZeroIPAliasValidator
- type NonZeroIPValidator
- type NonZeroIntAliasValidator
- type NonZeroIntValidator
- type NonZeroTimeAliasValidator
- type NonZeroTimeValidator
- type NonZeroURLAliasValidator
- 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err 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) (ok bool, err error)
- type UrlValidator
- type ValidatedValue
- type Validator
- type ValidatorFunc
- type XMLValidator
Constants ¶
This section is empty.
Variables ¶
var ErrFieldRequired = errors.New("field is required")
Functions ¶
func BindFormValues ¶
BindFormValues binds url.Values into a struct pointer using "field" tags.
func FormStatus ¶
FormStatus maps validation errors to HTTP status codes.
func MustValidate ¶
MustValidate validates a value or panics if validation fails.
func RegisterDirective ¶
RegisterDirective registers a directive for use with the "val" struct tag.
func ValidateForm ¶
ValidateForm wraps NewFormValidator + Validate and returns a FormError on failure.
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.
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.
type CmpRangeValidator ¶
CmpRangeValidator validates that a value is within an inclusive range.
func (*CmpRangeValidator[T]) Validate ¶
func (v *CmpRangeValidator[T]) Validate(val T) (ok bool, err 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) (ok bool, err 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.
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.
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.
type FormError ¶
FormError wraps a validation error with an HTTP status code.
func (*FormError) StatusCode ¶
StatusCode returns the associated HTTP status code.
type FormValidator ¶
type FormValidator struct {
// contains filtered or unexported fields
}
func NewFormValidator ¶
func NewFormValidator(r *http.Request) (*FormValidator, error)
NewFormValidator parses the request and prepares a validator. ParseForm handles both POST bodies and URL query parameters, so GET requests with query values are supported.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
type NonEmptyStringAliasValidator ¶
type NonEmptyStringAliasValidator struct {
NonEmptyStringValidator
}
NonEmptyStringAliasValidator provides the legacy "nonempty" tag.
func (*NonEmptyStringAliasValidator) Name ¶
func (v *NonEmptyStringAliasValidator) Name() string
Name returns the directive identifier.
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.
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.
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.
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.
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.
type NonZeroDurationAliasValidator ¶
type NonZeroDurationAliasValidator struct {
NonZeroDurationValidator
}
NonZeroDurationAliasValidator provides the "nonzeroduration" tag.
func (*NonZeroDurationAliasValidator) Name ¶
func (v *NonZeroDurationAliasValidator) Name() string
Name returns the directive identifier.
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 NonZeroFloat64AliasValidator ¶
type NonZeroFloat64AliasValidator struct {
NonZeroFloat64Validator
}
NonZeroFloat64AliasValidator provides the "nonzerofloat" tag.
func (*NonZeroFloat64AliasValidator) Name ¶
func (v *NonZeroFloat64AliasValidator) 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.
type NonZeroIPAliasValidator ¶
type NonZeroIPAliasValidator struct {
NonZeroIPValidator
}
NonZeroIPAliasValidator provides the "nonzeroip" tag.
func (*NonZeroIPAliasValidator) Name ¶
func (v *NonZeroIPAliasValidator) Name() string
Name returns the directive identifier.
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 NonZeroIntAliasValidator ¶
type NonZeroIntAliasValidator struct {
NonZeroIntValidator
}
NonZeroIntAliasValidator provides the "nonzeroint" tag.
func (*NonZeroIntAliasValidator) Name ¶
func (v *NonZeroIntAliasValidator) 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.
type NonZeroTimeAliasValidator ¶
type NonZeroTimeAliasValidator struct {
NonZeroTimeValidator
}
NonZeroTimeAliasValidator provides the legacy "nonzerotime" tag.
func (*NonZeroTimeAliasValidator) Name ¶
func (v *NonZeroTimeAliasValidator) Name() string
Name returns the directive identifier.
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 NonZeroURLAliasValidator ¶
type NonZeroURLAliasValidator struct {
NonZeroURLValidator
}
NonZeroURLAliasValidator provides the "nonzerourl" tag.
func (*NonZeroURLAliasValidator) Name ¶
func (v *NonZeroURLAliasValidator) 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) (ok bool, err 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.
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.
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.
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.
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.
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.
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.
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.
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.
type ValidatedValue ¶
type ValidatedValue[T cmp.Ordered] struct { Validator Validator[T] // contains filtered or unexported fields }
ValidatedValue stores a value and validates updates with the provided Validator.
func (*ValidatedValue[T]) Set ¶
func (v *ValidatedValue[T]) Set(val T) error
Set validates and stores the value.
func (*ValidatedValue[T]) String ¶
func (v *ValidatedValue[T]) String() string
String returns the string representation of the stored value.
type ValidatorFunc ¶
ValidatorFunc adapts a function to the Validator interface.
func (ValidatorFunc[T]) Validate ¶
func (p ValidatorFunc[T]) Validate(val T) (ok bool, err error)
Validate calls the underlying function.
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.