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 CIDR(value any, version int) 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 Hex(value any) bool
- func Hostname(value 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 MD5Hex(value any) 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 SHA1Hex(value any) bool
- func SHA256Hex(value any) bool
- func SHA384Hex(value any) bool
- func SHA512Hex(value any) 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 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 reports whether the string is a valid ISO datetime format.
func ISODateTimeWithOptions ¶ added in v0.3.0
func ISODateTimeWithOptions(value any, options ISODateTimeOptions) bool
ISODateTimeWithOptions reports whether the string is a valid ISO datetime with the given options.
func ISODuration ¶
ISODuration reports whether the string is a valid ISO 8601 duration format.
func ISOTimeWithOptions ¶ added in v0.3.0
func ISOTimeWithOptions(value any, options ISOTimeOptions) bool
ISOTimeWithOptions reports whether the string is a valid ISO time with the given options.
func JWTWithOptions ¶ added in v0.3.0
func JWTWithOptions(value any, options JWTOptions) bool
JWTWithOptions reports whether the string is a valid JWT format with an algorithm constraint.
func MAC ¶ added in v0.5.3
MAC reports whether the string is a valid MAC address using the default delimiter.
func MACWithOptions ¶ added in v0.5.3
func MACWithOptions(value any, opts MACOptions) bool
MACWithOptions reports whether the string is a valid MAC address with a custom delimiter.
func MD5Hex ¶ added in v0.5.4
MD5Hex reports whether the string is a valid MD5 hash in hex format (32 chars).
func MultipleOf ¶
MultipleOf reports whether value is a multiple of the given divisor.
func NonNegative ¶
NonNegative reports whether the numeric value is non-negative (>= 0).
func NonPositive ¶
NonPositive reports whether the numeric value is non-positive (<= 0).
func Property ¶
Property reports whether the object has a property at key that passes the validator.
func SHA1Hex ¶ added in v0.5.4
SHA1Hex reports whether the string is a valid SHA-1 hash in hex format (40 chars).
func SHA256Hex ¶ added in v0.5.4
SHA256Hex reports whether the string is a valid SHA-256 hash in hex format (64 chars).
func SHA384Hex ¶ added in v0.5.4
SHA384Hex reports whether the string is a valid SHA-384 hash in hex format (96 chars).
func SHA512Hex ¶ added in v0.5.4
SHA512Hex reports whether the string is a valid SHA-512 hash in hex format (128 chars).
func StartsWith ¶
StartsWith reports whether the string starts with the prefix.
func URLWithOptions ¶ added in v0.3.0
func URLWithOptions(value any, options URLOptions) bool
URLWithOptions reports whether the string is a valid URL with the given constraints.
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 allows timezone offsets like +01:00 when true.
Offset bool
// Local makes the 'Z' timezone marker optional when true.
Local bool
}
ISODateTimeOptions configures 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 configures 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 configures JWT validation.
type MACOptions ¶ added in v0.5.3
type MACOptions struct {
// Delimiter specifies the separator between hex pairs.
// Common delimiters: ":", "-", ".". Default is ":".
Delimiter string
}
MACOptions configures MAC address validation.