Documentation ¶
Index ¶
- func Email(value any) bool
- func EqualStrings(value, other string) bool
- func NotZero[T comparable](value T) bool
- func Password(value any) bool
- func Phone(value any) bool
- func Required(value any) bool
- func Username(value any) bool
- type Numeric
- type ValidationError
- type ValidationFunc
- func After(t time.Time) ValidationFunc
- func AllIn[T comparable](allowed ...T) ValidationFunc
- func Before(t time.Time) ValidationFunc
- func Between[T constraints.Ordered](min, max T) ValidationFunc
- func BetweenTime(start, end time.Time) ValidationFunc
- func Equal(other any) ValidationFunc
- func GreaterOrEqual[T Numeric](n T) ValidationFunc
- func GreaterThan[T Numeric](n T) ValidationFunc
- func In[T comparable](allowed ...T) ValidationFunc
- func LessOrEqual[T Numeric](n T) ValidationFunc
- func LessThan[T Numeric](n T) ValidationFunc
- func Match(pattern string) ValidationFunc
- func Max[T ~int | ~float64](max T) ValidationFunc
- func MaxLength(max int) ValidationFunc
- func Min[T ~int | ~float64](min T) ValidationFunc
- func MinLength(min int) ValidationFunc
- func NoDuplicates[T comparable]() ValidationFunc
- type Validator
- func (v *Validator) AddError(message string)
- func (v *Validator) AddFieldError(field, message string)
- func (v *Validator) Check(valid bool, message string) bool
- func (v *Validator) CheckField(valid bool, field, message string) bool
- func (v *Validator) Clear()
- func (v *Validator) DetailedErrors() map[string][]ValidationError
- func (v *Validator) Error() string
- func (v *Validator) Field(field string) string
- func (v *Validator) Fields() map[string]string
- func (v *Validator) HasErrors() bool
- func (v *Validator) HasField(field string) bool
- func (v *Validator) MarshalJSON() ([]byte, error)
- func (v *Validator) Merge(other *Validator)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Email ¶
Email validates email format
Example usage: Email("foo@example.com") // returns true Email("invalid-email") // returns false
func EqualStrings ¶
EqualStrings checks if two strings are equal
Example usage: EqualStrings("test", "test") // returns true EqualStrings("test", "TEST") // returns false
func NotZero ¶
func NotZero[T comparable](value T) bool
NotZero checks if a numeric value is not a zero value
Types ¶
type Numeric ¶
type Numeric interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 }
Numeric represents types that can be compared numerically
type ValidationError ¶
type ValidationFunc ¶
ValidationFunc is a non-generic function type for validation
func AllIn ¶
func AllIn[T comparable](allowed ...T) ValidationFunc
AllIn checks if all values in a slice are in a set of allowed values
Example usage: AllIn(1, 2, 3)([]int{1, 2}) // returns true AllIn(1, 2, 3)([]int{1, 4}) // returns false AllIn("a", "b", "c")([]string{"a", "b"}) // returns true AllIn("a", "b", "c")([]string{"a", "d"}) // returns false
func Between ¶
func Between[T constraints.Ordered](min, max T) ValidationFunc
Between checks if a value is between a minimum and maximum value Example usage:
Between(10, 20)(15) // returns true Between(10, 20)(25) // returns false
func BetweenTime ¶
func BetweenTime(start, end time.Time) ValidationFunc
BetweenTime checks if a value is between two other values
Example usage: BetweenTime(time.Now().Add(-1*time.Hour), time.Now().Add(1*time.Hour))(time.Now()) // returns true BetweenTime(time.Now().Add(-1*time.Hour), time.Now().Add(-30*time.Minute))(time.Now()) // returns false
func Equal ¶
func Equal(other any) ValidationFunc
Equal checks if values are equal Example usage: Equal(10)(10) // returns true Equal(10)(5) // returns false
func GreaterOrEqual ¶
func GreaterOrEqual[T Numeric](n T) ValidationFunc
GreaterOrEqual returns a validation function that checks if a value is greater than or equal to a specified value
Example usage: GreaterOrEqual(10)(15) // returns true GreaterOrEqual(10)(10) // returns true GreaterOrEqual(10)(5) // returns false
func GreaterThan ¶
func GreaterThan[T Numeric](n T) ValidationFunc
GreaterThan returns a validation function that checks if a value is greater than a specified value
Example usage:
GreaterThan(10)(15) // returns true GreaterThan(10)(5) // returns false
func In ¶
func In[T comparable](allowed ...T) ValidationFunc
In checks if a value is in a set of allowed values
Example usage: In(1, 2, 3)(2) // returns true In(1, 2, 3)(4) // returns false In("a", "b", "c")("b") // returns true In("a", "b", "c")("d") // returns false
func LessOrEqual ¶
func LessOrEqual[T Numeric](n T) ValidationFunc
LessOrEqual returns a validation function that checks if a value is less than or equal to a specified value
Example usage: LessOrEqual(10)(5) // returns true LessOrEqual(10)(10) // returns true LessOrEqual(10)(15) // returns false
func LessThan ¶
func LessThan[T Numeric](n T) ValidationFunc
LessThan returns a validation function that checks if a value is less than a specified value
Example usage: LessThan(10)(5) // returns true LessThan(10)(15) // returns false
func Match ¶
func Match(pattern string) ValidationFunc
Match returns a validation function that checks if a string matches a pattern
Example usage: Match(`^[a-zA-Z0-9]+$`)(username) // returns true if username is alphanumeric Match(`^[a-zA-Z0-9]+$`)(email) // returns false if email is not alphanumeric
func Max ¶
func Max[T ~int | ~float64](max T) ValidationFunc
Max returns a validation function that checks maximum value
Example usage: Max(10)(5) // returns true Max(10)(15) // returns false
func MaxLength ¶
func MaxLength(max int) ValidationFunc
MaxLength returns a validation function that checks maximum string length
Example usage: MaxLength(5)("hello") // returns false MaxLength(5)("hi") // returns true
func Min ¶
func Min[T ~int | ~float64](min T) ValidationFunc
Min returns a validation function that checks minimum value
Example usage: Min(10)(15) // returns true Min(10)(5) // returns false
func MinLength ¶
func MinLength(min int) ValidationFunc
MinLength returns a validation function that checks minimum string length
Example usage: MinLength(5)("hello") // returns true MinLength(5)("hi") // returns false
func NoDuplicates ¶
func NoDuplicates[T comparable]() ValidationFunc
NoDuplicates checks if a slice contains any duplicate values
Example usage: NoDuplicates()([]int{1, 2, 3}) // returns true NoDuplicates()([]int{1, 2, 2}) // returns false
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func (*Validator) AddFieldError ¶
AddFieldError adds an error for a specific field
func (*Validator) CheckField ¶
CheckField performs a field validation and adds an error if it fails
func (*Validator) DetailedErrors ¶
func (v *Validator) DetailedErrors() map[string][]ValidationError
DetailedErrors returns all validation errors with full details
func (*Validator) MarshalJSON ¶
MarshalJSON implements json.Marshaler