Documentation
¶
Index ¶
- Constants
- type Result
- func AgeAtLeast(birthDate string, minYears int) Result
- func BelgianNationalNumber(value string) Result
- func CIDR(value string) Result
- func CRON(value string) Result
- func CountryCode(value string) Result
- func CreditCard(value string) Result
- func CurrencyCode(value string) Result
- func Date(value string) Result
- func DateInFuture(value string, maxAhead time.Duration) Result
- func DateInPast(value string, maxAge time.Duration) Result
- func DateRange(value, minDate, maxDate string) Result
- func Domain(value string) Result
- func DutchBSN(value string) Result
- func Email(value string, checkMX bool) Result
- func HexColor(value string) Result
- func IBAN(value string) Result
- func IPv4(value string) Result
- func IPv6(value string) Result
- func ISBN(value string) Result
- func JWT(value string) Result
- func LEI(value string) Result
- func LatLon(lat, lon float64) Result
- func MAC(value string) Result
- func NumberInRange(value, min, max string) Result
- func NumberInRangeFloat(value, min, max float64) Result
- func Phone(value string) Result
- func PostalCode(value, countryCode string) Result
- func SWIFT(value string) Result
- func SemVer(value string) Result
- func StringLength(value string, min, max int) Result
- func URL(value string) Result
- func UUID(value string, version int) Result
- func VAT(value string) Result
- type ValidationError
Constants ¶
const ( ErrEmailFormatInvalid = "email.format.invalid" ErrEmailDomainNoMX = "email.domain.no_mx" )
Error codes — email
const ( ErrIBANTooShort = "iban.length.too_short" ErrIBANInvalidChars = "iban.characters.invalid" ErrIBANCountryInvalid = "iban.country.invalid" ErrIBANLengthMismatch = "iban.length.mismatch" ErrIBANChecksumInvalid = "iban.checksum.invalid" )
Error codes — IBAN
const ( ErrSWIFTLengthInvalid = "swift.length.invalid" ErrSWIFTFormatInvalid = "swift.format.invalid" )
Error codes — SWIFT/BIC
const ( ErrPhoneTooShort = "phone.length.too_short" ErrPhoneTooLong = "phone.length.too_long" ErrPhoneInvalidChars = "phone.characters.invalid" ErrPhoneFormatInvalid = "phone.format.invalid" ErrPhoneCountryInvalid = "phone.country.invalid" )
Error codes — phone
const ( ErrCreditCardTooShort = "creditcard.length.too_short" ErrCreditCardTooLong = "creditcard.length.too_long" ErrCreditCardInvalidChars = "creditcard.characters.invalid" ErrCreditCardChecksumInvalid = "creditcard.checksum.invalid" )
Error codes — credit card
const ( ErrVATTooShort = "vat.length.too_short" ErrVATCountryInvalid = "vat.country.invalid" ErrVATFormatInvalid = "vat.format.invalid" )
Error codes — VAT
const ( ErrURLFormatInvalid = "url.format.invalid" ErrURLSchemeInvalid = "url.scheme.invalid" ErrURLHostMissing = "url.host.missing" )
Error codes — URL
const ( ErrUUIDFormatInvalid = "uuid.format.invalid" ErrUUIDVersionInvalid = "uuid.version.invalid" )
Error codes — UUID
const ( ErrPostalCodeCountryInvalid = "postalcode.country.invalid" ErrPostalCodeFormatInvalid = "postalcode.format.invalid" )
Error codes — postal code
const ( ErrCountryCodeFormatInvalid = "countrycode.format.invalid" ErrCountryCodeUnknown = "countrycode.unknown" )
Error codes — country code
const ( ErrCurrencyCodeFormatInvalid = "currencycode.format.invalid" ErrCurrencyCodeUnknown = "currencycode.unknown" )
Error codes — currency code
const ( ErrDateFormatInvalid = "date.format.invalid" ErrDateInvalid = "date.value.invalid" )
Error codes — date
const ( ErrIPv4FormatInvalid = "ipv4.format.invalid" ErrIPv4OctetInvalid = "ipv4.octet.invalid" )
Error codes — IPv4
const ( ErrDomainFormatInvalid = "domain.format.invalid" ErrDomainLabelInvalid = "domain.label.invalid" ErrDomainTooLong = "domain.length.too_long" )
Error codes — domain
const ( ErrLEILengthInvalid = "lei.length.invalid" ErrLEIFormatInvalid = "lei.format.invalid" ErrLEIChecksumInvalid = "lei.checksum.invalid" )
Error codes — LEI
const ( ErrCIDRFormatInvalid = "cidr.format.invalid" ErrCIDRPrefixInvalid = "cidr.prefix.invalid" )
Error codes — CIDR
const ( ErrISBNFormatInvalid = "isbn.format.invalid" ErrISBNChecksumInvalid = "isbn.checksum.invalid" )
Error codes — ISBN
const ( ErrCRONFormatInvalid = "cron.format.invalid" ErrCRONFieldInvalid = "cron.field.invalid" )
Error codes — CRON
const ( ErrJWTFormatInvalid = "jwt.format.invalid" ErrJWTSegmentInvalid = "jwt.segment.invalid" )
Error codes — JWT
const ( ErrLatLonFormatInvalid = "latlon.format.invalid" ErrLatitudeOutOfRange = "latlon.latitude.out_of_range" ErrLongitudeOutOfRange = "latlon.longitude.out_of_range" )
Error codes — latitude/longitude
const ( ErrBNNLengthInvalid = "bnn.length.invalid" ErrBNNFormatInvalid = "bnn.format.invalid" ErrBNNDateInvalid = "bnn.date.invalid" ErrBNNChecksumInvalid = "bnn.checksum.invalid" )
Error codes — Belgian National Number
const ( ErrBSNLengthInvalid = "bsn.length.invalid" ErrBSNFormatInvalid = "bsn.format.invalid" ErrBSNChecksumInvalid = "bsn.checksum.invalid" )
Error codes — Dutch BSN
const ( ErrDateInPastNotPast = "dateinpast.range.not_past" ErrDateInPastTooFar = "dateinpast.range.too_far" )
Error codes — date in past
const ( ErrDateInFutureNotFuture = "dateinfuture.range.not_future" ErrDateInFutureTooFar = "dateinfuture.range.too_far" )
Error codes — date in future
const ( ErrDateRangeBeforeMin = "daterange.range.before_min" ErrDateRangeAfterMax = "daterange.range.after_max" )
Error codes — date range
const ( ErrNumberFormatInvalid = "number.format.invalid" ErrNumberBelowMin = "number.range.below_min" ErrNumberAboveMax = "number.range.above_max" )
Error codes — number in range
const ( ErrStringTooShort = "string.length.too_short" ErrStringTooLong = "string.length.too_long" )
Error codes — string length
const (
ErrAgeAtLeastTooYoung = "ageatleast.range.too_young"
)
Error codes — age at least
const (
ErrHexColorFormatInvalid = "hexcolor.format.invalid"
)
Error codes — hex color
const (
ErrIPv6FormatInvalid = "ipv6.format.invalid"
)
Error codes — IPv6
const (
ErrMACFormatInvalid = "mac.format.invalid"
)
Error codes — MAC address
const (
ErrSemVerFormatInvalid = "semver.format.invalid"
)
Error codes — semver
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result struct {
Valid bool `json:"valid"`
Errors []ValidationError `json:"errors,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
Result is the unified return type for all validators.
func AgeAtLeast ¶ added in v1.1.0
AgeAtLeast validates that a birth date (YYYY-MM-DD) represents an age of at least minYears. Uses calendar year calculation (not duration), handling leap years correctly.
func BelgianNationalNumber ¶
BelgianNationalNumber validates a Belgian Rijksregisternummer (11 digits). Format: YY.MM.DD-SSS.CC where SSS is a sequence number and CC is a MOD-97 check.
func CountryCode ¶
CountryCode validates an ISO 3166-1 country code (alpha-2 or alpha-3).
func CreditCard ¶
CreditCard validates a credit card number using the Luhn algorithm. Detects the card network from the IIN prefix.
func CurrencyCode ¶
CurrencyCode validates an ISO 4217 currency code.
func DateInFuture ¶ added in v1.1.0
DateInFuture validates that a date string (YYYY-MM-DD) is in the future. If maxAhead is > 0, the date must not be further ahead than maxAhead from today. A maxAhead of 0 means any future date is accepted.
func DateInPast ¶ added in v1.1.0
DateInPast validates that a date string (YYYY-MM-DD) is in the past. If maxAge is > 0, the date must not be further back than maxAge from today. A maxAge of 0 means any past date is accepted.
func DateRange ¶ added in v1.1.0
DateRange validates that a date string (YYYY-MM-DD) falls between min and max (inclusive). Either min or max may be empty to leave that bound open.
func Email ¶
Email validates an email address. If checkMX is true, it verifies the domain has MX records.
func JWT ¶
JWT validates the structure of a JSON Web Token (three base64url-encoded segments). It does NOT verify the cryptographic signature.
func LEI ¶
LEI validates a Legal Entity Identifier (ISO 17442). 20 alphanumeric characters with a MOD-97 check.
func LatLon ¶
LatLon validates a geographic coordinate pair (latitude, longitude) in decimal degrees.
func NumberInRange ¶ added in v1.1.0
NumberInRange validates that a decimal number string falls within [min, max]. Uses exact decimal arithmetic (math/big.Rat) — no floating point errors. Either min or max may be empty to leave that bound open.
func NumberInRangeFloat ¶ added in v1.1.0
NumberInRangeFloat validates that a float64 falls within [min, max]. Convenience wrapper for callers who already have numeric types. Uses floating point comparison — caller accepts precision tradeoffs.
func Phone ¶
Phone validates an international phone number in E.164-like format. Expects a leading + followed by country code and subscriber number.
func PostalCode ¶
PostalCode validates a postal/ZIP code for a given ISO 3166-1 alpha-2 country code.
func StringLength ¶ added in v1.1.0
StringLength validates that a string's length in Unicode rune count falls within [min, max]. Uses rune count, not byte count, so multi-byte characters are counted correctly. Set min to 0 to skip minimum check. Set max to 0 to skip maximum check.
type ValidationError ¶
type ValidationError struct {
Code string `json:"code"`
Message string `json:"message"`
Field string `json:"field,omitempty"`
Context map[string]any `json:"context,omitempty"`
}
ValidationError carries a machine-readable code, a human-readable message, and structured context so both humans and AI agents can act on failures.
Source Files
¶
- ageatleast.go
- bnn.go
- bsn.go
- cidr.go
- countrycode.go
- creditcard.go
- cron.go
- currencycode.go
- date.go
- dateinfuture.go
- dateinpast.go
- daterange.go
- domain.go
- email.go
- hexcolor.go
- iban.go
- ipv4.go
- ipv6.go
- isbn.go
- jwt.go
- latlon.go
- lei.go
- mac.go
- numberinrange.go
- phone.go
- postalcode.go
- result.go
- semver.go
- stringlength.go
- swift.go
- url.go
- uuid.go
- vat.go