is

package
v0.401.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package is provides common tests for using inside rule assertions. Most of these are wrappers around govalidator functions, but some are custom implementations for specific use cases. Heavily inspired by github.com/invopop/validation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Present checks if a value is non-nil and non-empty.
	// A value is considered not empty if
	// - integer, float: not zero
	// - bool: true
	// - string, array, slice, map: len() > 0
	// - interface, pointer: not nil and the referenced value is not empty
	// - any other types
	Present = presenceTest{"present", false}

	// NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty.
	// NilOrNotEmpty differs from Present in that it treats a nil pointer as valid.
	NilOrNotEmpty = presenceTest{"nil or not empty", true}
)
View Source
var (
	// EmailFormat tests that a string is a valid email address format.
	// Note that it does NOT check if the MX record exists or not.
	EmailFormat = StringFunc("email-format", govalidator.IsEmail)
	// URL tests that a string is a valid URL.
	URL = StringFunc("url", govalidator.IsURL)
	// RequestURL tests that a string is a valid request URL.
	RequestURL = StringFunc("request-url", govalidator.IsRequestURL)
	// RequestURI tests that a string is a valid request URI.
	RequestURI = StringFunc("request-uri", govalidator.IsRequestURI)
	// Alpha tests that a string contains English letters only (a-zA-Z).
	Alpha = StringFunc("alpha", govalidator.IsAlpha)
	// Digit tests that a string contains digits only (0-9).
	Digit = StringFunc("digit", isDigit)
	// Alphanumeric tests that a string contains English letters and digits only (a-zA-Z0-9).
	Alphanumeric = StringFunc("alphanumeric", govalidator.IsAlphanumeric)
	// UTFLetter tests that a string contains unicode letters only.
	UTFLetter = StringFunc("utf-letter", govalidator.IsUTFLetter)
	// UTFDigit tests that a string contains unicode decimal digits only.
	UTFDigit = StringFunc("utf-digit", govalidator.IsUTFDigit)
	// UTFLetterNumeric tests that a string contains unicode letters and numbers only.
	UTFLetterNumeric = StringFunc("utf-letter-numeric", govalidator.IsUTFLetterNumeric)
	// UTFNumeric tests that a string contains unicode number characters (category N) only.
	UTFNumeric = StringFunc("utf-numeric", isUTFNumeric)
	// LowerCase tests that a string contains lower case unicode letters only.
	LowerCase = StringFunc("lower-case", govalidator.IsLowerCase)
	// UpperCase tests that a string contains upper case unicode letters only.
	UpperCase = StringFunc("upper-case", govalidator.IsUpperCase)
	// Hexadecimal tests that a string is a valid hexadecimal number.
	Hexadecimal = StringFunc("hexadecimal", govalidator.IsHexadecimal)
	// HexColor tests that a string is a valid hexadecimal color code.
	HexColor = StringFunc("hex-color", govalidator.IsHexcolor)
	// RGBColor tests that a string is a valid RGB color in the form of rgb(R, G, B).
	RGBColor = StringFunc("rgb-color", govalidator.IsRGBcolor)
	// Int tests that a string is a valid integer number.
	Int = StringFunc("int", govalidator.IsInt)
	// Float tests that a string is a floating point number.
	Float = StringFunc("float", govalidator.IsFloat)
	// UUIDv3 tests that a string is a valid version 3 UUID.
	UUIDv3 = StringFunc("uuid-v3", govalidator.IsUUIDv3)
	// UUIDv4 tests that a string is a valid version 4 UUID.
	UUIDv4 = StringFunc("uuid-v4", govalidator.IsUUIDv4)
	// UUIDv5 tests that a string is a valid version 5 UUID.
	UUIDv5 = StringFunc("uuid-v5", govalidator.IsUUIDv5)
	// UUID tests that a string is a valid UUID (any version).
	UUID = StringFunc("uuid", govalidator.IsUUID)
	// CreditCard tests that a string is a valid credit card number.
	CreditCard = StringFunc("credit-card", govalidator.IsCreditCard)
	// ISBN10 tests that a string is a valid ISBN version 10.
	ISBN10 = StringFunc("isbn-10", govalidator.IsISBN10)
	// ISBN13 tests that a string is a valid ISBN version 13.
	ISBN13 = StringFunc("isbn-13", govalidator.IsISBN13)
	// ISBN tests that a string is a valid ISBN (either version 10 or 13).
	ISBN = StringFunc("isbn", isISBN)
	// JSON tests that a string is in valid JSON format.
	JSON = StringFunc("json", govalidator.IsJSON)
	// ASCII tests that a string contains ASCII characters only.
	ASCII = StringFunc("ascii", govalidator.IsASCII)
	// PrintableASCII tests that a string contains printable ASCII characters only.
	PrintableASCII = StringFunc("printable-ascii", govalidator.IsPrintableASCII)
	// Multibyte tests that a string contains multibyte characters.
	Multibyte = StringFunc("multibyte", govalidator.IsMultibyte)
	// FullWidth tests that a string contains full-width characters.
	FullWidth = StringFunc("full-width", govalidator.IsFullWidth)
	// HalfWidth tests that a string contains half-width characters.
	HalfWidth = StringFunc("half-width", govalidator.IsHalfWidth)
	// VariableWidth tests that a string contains both full-width and half-width characters.
	VariableWidth = StringFunc("variable-width", govalidator.IsVariableWidth)
	// Base64 tests that a string is encoded in Base64.
	Base64 = StringFunc("base64", govalidator.IsBase64)
	// DataURI tests that a string is a valid base64-encoded data URI.
	DataURI = StringFunc("data-uri", govalidator.IsDataURI)
	// E164 tests that a string is a valid E.164 telephone number.
	E164 = StringFunc("e164", isE164Number)
	// CountryCode2 tests that a string is a valid ISO 3166-1 alpha-2 country code.
	CountryCode2 = StringFunc("country-code-2", govalidator.IsISO3166Alpha2)
	// CountryCode3 tests that a string is a valid ISO 3166-1 alpha-3 country code.
	CountryCode3 = StringFunc("country-code-3", govalidator.IsISO3166Alpha3)
	// CurrencyCode tests that a string is a valid ISO 4217 currency code.
	CurrencyCode = StringFunc("currency-code", govalidator.IsISO4217)
	// DialString tests that a string is a valid dial string that can be passed to Dial().
	DialString = StringFunc("dial-string", govalidator.IsDialString)
	// MAC tests that a string is a valid MAC address.
	MAC = StringFunc("mac", govalidator.IsMAC)
	// IP tests that a string is a valid IP address (either version 4 or 6).
	IP = StringFunc("ip", govalidator.IsIP)
	// IPv4 tests that a string is a valid version 4 IP address.
	IPv4 = StringFunc("ipv4", govalidator.IsIPv4)
	// IPv6 tests that a string is a valid version 6 IP address.
	IPv6 = StringFunc("ipv6", govalidator.IsIPv6)
	// Subdomain tests that a string is a valid subdomain.
	Subdomain = StringFunc("subdomain", isSubdomain)
	// Domain tests that a string is a valid domain name.
	Domain = StringFunc("domain", isDomain)
	// DNSName tests that a string is a valid DNS name.
	DNSName = StringFunc("dns-name", govalidator.IsDNSName)
	// Host tests that a string is a valid IP address or DNS name.
	Host = StringFunc("host", govalidator.IsHost)
	// Port tests that a string is a valid port number.
	Port = StringFunc("port", govalidator.IsPort)
	// Latitude tests that a string is a valid latitude coordinate.
	Latitude = StringFunc("latitude", govalidator.IsLatitude)
	// Longitude tests that a string is a valid longitude coordinate.
	Longitude = StringFunc("longitude", govalidator.IsLongitude)
	// SSN tests that a string is a valid US Social Security Number.
	SSN = StringFunc("ssn", govalidator.IsSSN)
	// Semver tests that a string is a valid semantic version.
	Semver = StringFunc("semver", govalidator.IsSemver)
)
View Source
var (
	// Empty checks that a value is not present (nil or empty).
	// A value is considered empty if
	// - integer, float: zero
	// - bool: false
	// - string, array, slice, map: len() == 0
	// - interface, pointer: nil or the referenced value is empty
	// It is the inverse of Present.
	Empty = emptyTest{}
)
View Source
var (
	// Nil checks that a value is a nil pointer.
	// Unlike Empty, it does not pass for empty non-nil values such as "".
	Nil = nilTest{}
)

Functions

func Expr

func Expr(test string, args ...any) rules.Test

Expr returns a new expression-based test.

func InContext

func InContext(t rules.Test) rules.Test

InContext returns a Test that passes when any value in the current validation context satisfies the provided inner test t.

When used as a guard on a registered rule set, the context is built automatically from the root object's embedded tax.Regime and tax.Addons (or any type implementing rules.ContextAdder), and from explicit rules.WithContext options passed to rules.Validate.

When called via Check without a context (e.g. Set.Validate directly), it falls back to testing the validated value itself with t.

func Indirect

func Indirect(value interface{}) (interface{}, bool)

Indirect returns the value that the given interface or pointer references to. If the value implements driver.Valuer, it will deal with the value returned by the Value() method instead. A boolean value is also returned to indicate if the value is nil or not (only applicable to interface, pointer, map, and slice). If the value is neither an interface nor a pointer, it will be returned back.

func LengthOfValue

func LengthOfValue(value interface{}) (int, error)

LengthOfValue returns the length of a value that is a string, slice, map, or array. An error is returned for all other types.

func Or

func Or(tests ...rules.Test) rules.Test

Or defines a test that will pass if any of the provided tests pass.

func StringOrBytes

func StringOrBytes(value interface{}) (isString bool, str string, isBytes bool, bs []byte)

StringOrBytes typecasts a value into a string or byte slice. Boolean flags are returned to indicate if the typecasting succeeds or not.

Types

type FuncContextTest

type FuncContextTest struct {
	// contains filtered or unexported fields
}

FuncContextTest is a Test with access to the validation context.

func FuncContext

func FuncContext(desc string, fn func(ctx rules.Context, val any) bool) FuncContextTest

FuncContext creates a Test that has access to the validation context. fn receives a rules.Context (use ctx.Value(key) to extract stored values) and the current value being tested. Use this to implement tests that need to inspect values injected via rules.WithContext options or embedded ContextAdder fields (e.g. tax.Regime).

func (FuncContextTest) Check

func (t FuncContextTest) Check(val any) bool

Check satisfies the rules.Test interface when no context is available.

func (FuncContextTest) CheckWithContext

func (t FuncContextTest) CheckWithContext(rc *rules.Context, val any) bool

CheckWithContext implements rules.ContextualTest, giving this test access to the validation context during a rules.Validate call.

func (FuncContextTest) String

func (t FuncContextTest) String() string

type FuncTest

type FuncTest struct {
	// contains filtered or unexported fields
}

FuncTest is a Test that validates a value against a custom boolean function.

func Func

func Func(desc string, test func(any) bool) FuncTest

Func creates a validation rule that checks if a value satisfies a custom condition defined by the provided boolean test function.

func FuncError

func FuncError(desc string, test func(any) error) FuncTest

FuncError creates a validation rule that checks if a value satisfies a custom condition defined by the provided test function that returns an error.

func (FuncTest) Check

func (t FuncTest) Check(value any) bool

Check returns true if the value satisfies the test function.

func (FuncTest) String

func (t FuncTest) String() string

type InTest

type InTest struct {
	// contains filtered or unexported fields
}

InTest is a Test that checks if a value is one of a set.

func In

func In(set ...any) *InTest

In provides a validation rule that checks if a value is one of the provided set. Named types (e.g. type Code string) are compared against their underlying primitive, so In("A", "B") will match both string("A") and Code("A"). Nil values are skipped; use Present to enforce presence.

func (InTest) Check

func (t InTest) Check(value any) bool

Check returns true if the value is present in the set.

func (*InTest) Compile

func (t *InTest) Compile(_ any) error

Compile normalizes the set values for comparison.

func (InTest) String

func (t InTest) String() string

type LengthTest

type LengthTest struct {
	// contains filtered or unexported fields
}

LengthTest is a Test that checks the byte or rune length of a value.

func Length

func Length(minLen, maxLen int) LengthTest

Length returns a validation rule that checks if a value's length is within the specified range. For strings, it counts bytes. If max is 0, there is no upper bound. Empty values are skipped; use Present to enforce presence.

func RuneLength

func RuneLength(minLen, maxLen int) LengthTest

RuneLength returns a validation rule that checks if a string's rune (character) count is within the specified range. Unlike Length, it correctly handles multi-byte Unicode characters. For non-string types, it behaves identically to Length. If max is 0, there is no upper bound. Empty values are skipped; use Present to enforce presence.

func (LengthTest) Check

func (t LengthTest) Check(value any) bool

Check returns true if the value's length is within the configured range.

func (LengthTest) String

func (t LengthTest) String() string

type MatchTest

type MatchTest struct {
	// contains filtered or unexported fields
}

MatchTest is a Test that checks if a string matches a regular expression.

func Matches

func Matches(pattern string) *MatchTest

Matches provides a validation rule that checks if a string value matches the specified regular expression pattern. Patterns will be compiled when used in rules.For() or rules.ForValue() and cached for future use.

func MatchesRegexp

func MatchesRegexp(re *regexp.Regexp) *MatchTest

MatchesRegexp provides a validation rule that checks if the provided Regular Expression matches the value.

func (MatchTest) Check

func (t MatchTest) Check(value any) bool

Check returns true if the value matches the regular expression.

func (*MatchTest) Compile

func (t *MatchTest) Compile(_ any) error

Compile compiles the pattern string into a regular expression.

func (MatchTest) String

func (t MatchTest) String() string

type MinMaxTest

type MinMaxTest struct {
	// contains filtered or unexported fields
}

MinMaxTest is a Test that checks if a value meets a numeric or time threshold.

func Max

func Max(threshold any) MinMaxTest

Max returns a validation rule that checks if a value is less than or equal to threshold. Supports int, uint, float, and time.Time types. Empty values are skipped.

func Min

func Min(threshold any) MinMaxTest

Min returns a validation rule that checks if a value is greater than or equal to threshold. Supports int, uint, float, and time.Time types. Empty values are skipped.

func (MinMaxTest) Check

func (t MinMaxTest) Check(value any) bool

Check returns true if the value satisfies the threshold comparison.

func (MinMaxTest) Exclusive

func (t MinMaxTest) Exclusive() MinMaxTest

Exclusive returns a copy of the rule using strict (exclusive) comparison.

func (MinMaxTest) String

func (t MinMaxTest) String() string

type NotInTest

type NotInTest struct {
	// contains filtered or unexported fields
}

NotInTest is a Test that checks if a value is absent from a set.

func NotIn

func NotIn(set ...any) *NotInTest

NotIn provides a validation rule that checks if a value is absent from the provided set. Named types (e.g. type Code string) are compared against their underlying primitive, so NotIn("A", "B") will reject both string("A") and Code("A"). Nil values are skipped; use Present to enforce presence.

func (NotInTest) Check

func (t NotInTest) Check(value any) bool

Check returns true if the value is not present in the set.

func (*NotInTest) Compile

func (t *NotInTest) Compile(_ any) error

Compile normalizes the set values for comparison.

func (NotInTest) String

func (t NotInTest) String() string

type StringTest

type StringTest struct {
	// contains filtered or unexported fields
}

StringTest is a Test that validates a string value against a custom function.

func StringFunc

func StringFunc(desc string, test func(string) bool) StringTest

StringFunc creates a StringTest with the given description and test function.

func (StringTest) Check

func (t StringTest) Check(value any) bool

Check returns true if the value is a string that satisfies the test function.

func (StringTest) String

func (t StringTest) String() string

Jump to

Keyboard shortcuts

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