check

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Email

func Email(value any) bool

Email validates email format

Example usage: Email("foo@example.com") // returns true Email("invalid-email") // returns false

func EqualStrings

func EqualStrings(value, other string) bool

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

func Password

func Password(value any) bool

Password returns common password validation rules

func Phone

func Phone(value any) bool

Phone validates phone number format

func Required

func Required(value any) bool

Required checks if a value is non-empty

Example usage: Required("some value") // returns true Required("") // returns false Required([]int{1, 2, 3}) // returns true Required([]int{}) // returns false

func Username

func Username(value any) bool

Username returns common username validation rules

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 ValidationError struct {
	Field   string `json:"field,omitempty"`
	Message string `json:"message"`
}

type ValidationFunc

type ValidationFunc func(value any) bool

ValidationFunc is a non-generic function type for validation

func After

func After(t time.Time) ValidationFunc

After checks if a time is after another

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 Before

func Before(t time.Time) ValidationFunc

Before checks if a time is before another

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 NewValidator

func NewValidator() *Validator

NewValidator creates a new validator instance

func (*Validator) AddError

func (v *Validator) AddError(message string)

AddError adds a standalone error

func (*Validator) AddFieldError

func (v *Validator) AddFieldError(field, message string)

AddFieldError adds an error for a specific field

func (*Validator) Check

func (v *Validator) Check(valid bool, message string) bool

Check performs a validation and adds an error if it fails

func (*Validator) CheckField

func (v *Validator) CheckField(valid bool, field, message string) bool

CheckField performs a field validation and adds an error if it fails

func (*Validator) Clear

func (v *Validator) Clear()

Clear removes all errors from the validator

func (*Validator) DetailedErrors

func (v *Validator) DetailedErrors() map[string][]ValidationError

DetailedErrors returns all validation errors with full details

func (*Validator) Error

func (v *Validator) Error() string

Error implements the error interface

func (*Validator) Field

func (v *Validator) Field(field string) string

Field returns the first error message for a field if it exists

func (*Validator) Fields

func (v *Validator) Fields() map[string]string

Fields returns a map of field names to their first error message

func (*Validator) HasErrors

func (v *Validator) HasErrors() bool

HasErrors returns true if there are any validation errors

func (*Validator) HasField

func (v *Validator) HasField(field string) bool

HasField returns true if the field has any errors

func (*Validator) MarshalJSON

func (v *Validator) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Validator) Merge

func (v *Validator) Merge(other *Validator)

Merge combines another validator's errors into this one

Jump to

Keyboard shortcuts

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