validators

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 20 Imported by: 0

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

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

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

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

type CmpRangeValidator[T cmp.Ordered] struct {
	Min T
	Max T
}

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

type CompositeValidator[T cmp.Ordered] struct {
	Validators []valex.Validator[T]
}

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

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

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

type Float64RangeValidator struct {
	Min float64 `param:"min"`
	Max float64 `param:"max"`
}

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

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

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

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

type IPRangeValidator struct {
	Start net.IP `param:"start"`
	End   net.IP `param:"end"`
}

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) Handle

func (v *IPRangeValidator) Handle(val net.IP) (net.IP, error)

Handle validates the value and returns it unchanged.

func (*IPRangeValidator) Mode

Mode returns the directive evaluation mode.

func (*IPRangeValidator) Name

func (v *IPRangeValidator) Name() string

Name returns the directive identifier.

func (*IPRangeValidator) Validate

func (v *IPRangeValidator) Validate(val net.IP) error

Validate checks whether the value is within the configured range.

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

type IntRangeValidator struct {
	Min int `param:"min"`
	Max int `param:"max"`
}

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

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

type LengthRangeValidator struct {
	Min int `param:"min"`
	Max int `param:"max"`
}

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

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

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

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

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

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

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

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

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

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

Handle validates the value and returns it unchanged.

func (*NonNegativeFloat64Validator) Mode

Mode returns the directive evaluation mode.

func (*NonNegativeFloat64Validator) Name

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

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

Handle validates the value and returns it unchanged.

func (*NonPositiveFloat64Validator) Mode

Mode returns the directive evaluation mode.

func (*NonPositiveFloat64Validator) Name

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

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) Handle

Handle validates the value and returns it unchanged.

func (*NonZeroDurationValidator) Mode

Mode returns the directive evaluation mode.

func (*NonZeroDurationValidator) Name

func (v *NonZeroDurationValidator) Name() string

Name returns the directive identifier.

func (*NonZeroDurationValidator) Validate

func (v *NonZeroDurationValidator) Validate(val time.Duration) error

Validate checks whether the value is non-zero.

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

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) Handle

func (v *NonZeroIPValidator) Handle(val net.IP) (net.IP, error)

Handle validates the value and returns it unchanged.

func (*NonZeroIPValidator) Mode

Mode returns the directive evaluation mode.

func (*NonZeroIPValidator) Name

func (v *NonZeroIPValidator) Name() string

Name returns the directive identifier.

func (*NonZeroIPValidator) Validate

func (v *NonZeroIPValidator) Validate(val net.IP) error

Validate checks whether the value is non-zero.

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

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) Handle

func (v *NonZeroTimeValidator) Handle(val time.Time) (time.Time, error)

Handle validates the value and returns it unchanged.

func (*NonZeroTimeValidator) Mode

Mode returns the directive evaluation mode.

func (*NonZeroTimeValidator) Name

func (v *NonZeroTimeValidator) Name() string

Name returns the directive identifier.

func (*NonZeroTimeValidator) Validate

func (v *NonZeroTimeValidator) Validate(val time.Time) error

Validate checks whether the value is non-zero.

type NonZeroURLValidator

type NonZeroURLValidator struct{}

NonZeroURLValidator validates that a url.URL is not zero.

func (*NonZeroURLValidator) Handle

func (v *NonZeroURLValidator) Handle(val url.URL) (url.URL, error)

Handle validates the value and returns it unchanged.

func (*NonZeroURLValidator) Mode

Mode returns the directive evaluation mode.

func (*NonZeroURLValidator) Name

func (v *NonZeroURLValidator) Name() string

Name returns the directive identifier.

func (*NonZeroURLValidator) Validate

func (v *NonZeroURLValidator) Validate(val url.URL) error

Validate checks whether the value is non-zero.

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

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

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

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) Handle

Handle validates the value and returns it unchanged.

func (*PositiveDurationValidator) Mode

Mode returns the directive evaluation mode.

func (*PositiveDurationValidator) Name

Name returns the directive identifier.

func (*PositiveDurationValidator) Validate

func (v *PositiveDurationValidator) Validate(val time.Duration) error

Validate checks whether the value is positive.

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

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

type RegexValidator struct {
	Pattern *regexp.Regexp `param:"pattern"`
}

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

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

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

type TimeAfterValidator struct {
	After time.Time `param:"after"`
}

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) Handle

func (v *TimeAfterValidator) Handle(val time.Time) (time.Time, error)

Handle validates the value and returns it unchanged.

func (*TimeAfterValidator) Mode

Mode returns the directive evaluation mode.

func (*TimeAfterValidator) Name

func (v *TimeAfterValidator) Name() string

Name returns the directive identifier.

func (*TimeAfterValidator) Validate

func (v *TimeAfterValidator) Validate(val time.Time) error

Validate checks whether the value is after the configured time.

type TimeBeforeValidator

type TimeBeforeValidator struct {
	Before time.Time `param:"before"`
}

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) Handle

func (v *TimeBeforeValidator) Handle(val time.Time) (time.Time, error)

Handle validates the value and returns it unchanged.

func (*TimeBeforeValidator) Mode

Mode returns the directive evaluation mode.

func (*TimeBeforeValidator) Name

func (v *TimeBeforeValidator) Name() string

Name returns the directive identifier.

func (*TimeBeforeValidator) Validate

func (v *TimeBeforeValidator) Validate(val time.Time) error

Validate checks whether the value is before the configured time.

type TimeBetweenValidator

type TimeBetweenValidator struct {
	Start time.Time `param:"start"`
	End   time.Time `param:"end"`
}

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) Handle

func (v *TimeBetweenValidator) Handle(val time.Time) (time.Time, error)

Handle validates the value and returns it unchanged.

func (*TimeBetweenValidator) Mode

Mode returns the directive evaluation mode.

func (*TimeBetweenValidator) Name

func (v *TimeBetweenValidator) Name() string

Name returns the directive identifier.

func (*TimeBetweenValidator) Validate

func (v *TimeBetweenValidator) Validate(val time.Time) error

Validate checks whether the value is within the configured range.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL