Documentation
¶
Overview ¶
Package validate provides validation functions for various data types and formats.
Key features:
- Numeric validation (Lt, Gt, Lte, Gte, Positive, Negative, MultipleOf)
- String validation (Regex, Lowercase, Uppercase, Includes, StartsWith, EndsWith)
- Format validation (Email, URL, UUID, IP, CIDR, Base64, JWT)
- ISO datetime validation (ISODateTime, ISODate, ISOTime, ISODuration)
- Length and size validation (MaxLength, MinLength, MaxSize, MinSize)
Usage:
if validate.Email(email) {
// valid email
}
if validate.UUID(uuid) {
// valid UUID
}
Index ¶
- func Base64(value any) bool
- func Base64URL(value any) bool
- func CIDRv4(value any) bool
- func CIDRv6(value any) bool
- func CUID(value any) bool
- func CUID2(value any) bool
- func E164(value any) bool
- func Email(value any) bool
- func Emoji(value any) bool
- func EndsWith(value any, suffix string) bool
- func GUID(value any) bool
- func Gt(value, limit any) bool
- func Gte(value, limit any) bool
- func IPv4(value any) bool
- func IPv6(value any) bool
- func ISODate(value any) bool
- func ISODateTime(value any) bool
- func ISODateTimeWithOptions(value any, options ISODateTimeOptions) bool
- func ISODuration(value any) bool
- func ISOTime(value any) bool
- func ISOTimeWithOptions(value any, options ISOTimeOptions) bool
- func Includes(value any, substring string) bool
- func JSON(value any) bool
- func JWT(value any) bool
- func JWTWithOptions(value any, options JWTOptions) bool
- func KSUID(value any) bool
- func Length(value any, expected int) bool
- func Lowercase(value any) bool
- func Lt(value, limit any) bool
- func Lte(value, limit any) bool
- func MAC(value any) bool
- func MACWithOptions(value any, opts MACOptions) bool
- func MaxLength(value any, maximum int) bool
- func MaxSize(value any, maximum int) bool
- func Mime(value any, allowedTypes []string) bool
- func MinLength(value any, minimum int) bool
- func MinSize(value any, minimum int) bool
- func MultipleOf(value, divisor any) bool
- func NanoID(value any) bool
- func Negative(value any) bool
- func NonNegative(value any) bool
- func NonPositive(value any) bool
- func Positive(value any) bool
- func Property(obj any, key string, validator func(any) bool) bool
- func Regex(value any, pattern *regexp.Regexp) bool
- func Size(value any, expected int) bool
- func StartsWith(value any, prefix string) bool
- func ULID(value any) bool
- func URL(value any) bool
- func URLWithOptions(value any, options URLOptions) bool
- func UUID(value any) bool
- func Uppercase(value any) bool
- func ValidateCIDR(value any, version int) bool
- func XID(value any) bool
- type ISODateTimeOptions
- type ISOTimeOptions
- type JWTOptions
- type MACOptions
- type URLOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ISODateTime ¶
ISODateTime validates if string is a valid ISO datetime format
func ISODateTimeWithOptions ¶ added in v0.3.0
func ISODateTimeWithOptions(value any, options ISODateTimeOptions) bool
ISODateTimeWithOptions validates if string is a valid ISO datetime format with options
func ISODuration ¶
ISODuration validates if string is a valid ISO 8601 duration format
func ISOTimeWithOptions ¶ added in v0.3.0
func ISOTimeWithOptions(value any, options ISOTimeOptions) bool
ISOTimeWithOptions validates if string is a valid ISO time format with options
func JWT ¶
JWT validates if string is a valid JWT token format This function only validates the JWT structure and basic claims It does NOT verify the signature (use JWTWithSecret for signature verification)
func JWTWithOptions ¶ added in v0.3.0
func JWTWithOptions(value any, options JWTOptions) bool
JWTWithOptions validates if string is a valid JWT token format with algorithm constraint This function only validates the JWT structure and basic claims It does NOT verify the signature (use JWTWithSecret for signature verification)
func MAC ¶ added in v0.5.3
MAC validates if string is a valid MAC address using default colon delimiter. Accepts both uppercase and lowercase hex digits. Examples:
"00:1A:2B:3C:4D:5E" -> true "00:1a:2b:3c:4d:5e" -> true "00-1A-2B-3C-4D-5E" -> false (wrong delimiter) "00:1A:2B:3C:4D" -> false (incomplete)
func MACWithOptions ¶ added in v0.5.3
func MACWithOptions(value any, opts MACOptions) bool
MACWithOptions validates if string is a valid MAC address with custom delimiter. This implementation matches Zod's TypeScript version using case-sensitive branches.
The validation accepts MAC addresses in the format:
XX:XX:XX:XX:XX:XX (where X is a hex digit, : can be replaced by delimiter)
Both uppercase and lowercase hex digits are valid, but they should not be mixed within a single validation (though the regex allows both).
Examples:
MACWithOptions("00:1a:2b:3c:4d:5e", MACOptions{Delimiter: ":"}) -> true
MACWithOptions("00-1A-2B-3C-4D-5E", MACOptions{Delimiter: "-"}) -> true
MACWithOptions("00.1a.2b.3c.4d.5e", MACOptions{Delimiter: "."}) -> true
func MultipleOf ¶
MultipleOf validates if value is a multiple of the given divisor
func NonNegative ¶
NonNegative validates if numeric value is non-negative (>= 0)
func NonPositive ¶
NonPositive validates if numeric value is non-positive (<= 0)
func StartsWith ¶
StartsWith validates if string starts with the prefix
func URLWithOptions ¶ added in v0.3.0
func URLWithOptions(value any, options URLOptions) bool
URLWithOptions validates if string is a valid URL format with optional constraints
func ValidateCIDR ¶ added in v0.5.3
ValidateCIDR validates CIDR notation with strict format checking version: 0=both, 4=IPv4 only, 6=IPv6 only
Types ¶
type ISODateTimeOptions ¶ added in v0.3.0
type ISODateTimeOptions struct {
// Precision specifies number of decimal places for seconds
// If nil, matches any number of decimal places
// If 0 or negative, no decimal places allowed
Precision *int
// Offset if true, allows timezone offsets like +01:00
Offset bool
// Local if true, makes the 'Z' timezone marker optional
Local bool
}
ISODateTimeOptions defines parameters for ISO datetime validation
type ISOTimeOptions ¶ added in v0.3.0
type ISOTimeOptions struct {
// Precision specifies number of decimal places for seconds
// If nil, matches any number of decimal places
// If 0 or negative, no decimal places allowed
// Special case: -1 means minute precision only (no seconds)
Precision *int
}
ISOTimeOptions defines parameters for ISO time validation
type JWTOptions ¶ added in v0.3.0
type JWTOptions struct {
// Algorithm specifies the expected signing algorithm
// If nil, any algorithm is accepted (not recommended for production)
Algorithm *string
}
JWTOptions defines options for JWT validation
type MACOptions ¶ added in v0.5.3
type MACOptions struct {
// Delimiter specifies the separator between hex pairs (default: ":")
// Common delimiters: ":", "-", "."
// Examples:
// ":" -> "00:1A:2B:3C:4D:5E"
// "-" -> "00-1A-2B-3C-4D-5E"
// "." -> "00.1A.2B.3C.4D.5E"
Delimiter string
}
MACOptions defines configuration for MAC address validation