Documentation
¶
Overview ¶
Valgo is a type-safe, expressive, and extensible validator library for Golang. Valgo is built with generics, so Go 1.18 or higher is required.
Valgo differs from other Golang validation libraries in that the rules are written in functions and not in struct tags. This allows greater flexibility and freedom when it comes to where and how data is validated.
Additionally, Valgo supports customizing and localizing validation messages.
Example ¶
val := Is(String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20)).
Is(Number(17, "age").GreaterThan(18))
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "age": [ "Age must be greater than \"18\"" ], "full_name": [ "Full name must have a length between \"4\" and \"20\"" ] }
Index ¶
- Constants
- type Error
- type FactoryOptions
- type Locale
- type Options
- type TypeNumber
- type Validation
- func AddErrorMessage(name string, message string) *Validation
- func Check(validators ...Validator) *Validation
- func Do(function func(val *Validation)) *Validation
- func If(condition bool, _validation *Validation) *Validation
- func In(name string, v *Validation) *Validation
- func InCell(name string, index int, v *Validation) *Validation
- func InRow(name string, index int, v *Validation) *Validation
- func Is(validators ...Validator) *Validation
- func New(options ...Options) *Validation
- func When(condition bool, function func(val *Validation)) *Validation
- func (v *Validation) AddErrorMessage(name string, message string) *Validation
- func (validation *Validation) Check(validators ...Validator) *Validation
- func (validation *Validation) Do(function func(val *Validation)) *Validation
- func (validation *Validation) Error(marshalJsonFun ...func(e *Error) ([]byte, error)) error
- func (session *Validation) Errors() map[string]*valueError
- func (validation *Validation) If(condition bool, _validation *Validation) *Validation
- func (validation *Validation) In(name string, _validation *Validation) *Validation
- func (validation *Validation) InCell(name string, index int, _validation *Validation) *Validation
- func (validation *Validation) InRow(name string, index int, _validation *Validation) *Validation
- func (validation *Validation) Is(validators ...Validator) *Validation
- func (validation *Validation) IsValid(name string) bool
- func (validation *Validation) Merge(_validation *Validation) *Validation
- func (v *Validation) MergeError(err *Error) *Validation
- func (v *Validation) MergeErrorIn(name string, err *Error) *Validation
- func (v *Validation) MergeErrorInIndex(name string, index int, err *Error) *Validation
- func (v *Validation) MergeErrorInRow(name string, index int, err *Error) *Validation
- func (validation *Validation) ToError(marshalJsonFun ...func(e *Error) ([]byte, error)) error
- func (validation *Validation) ToValgoError(marshalJsonFun ...func(e *Error) ([]byte, error)) *Error
- func (validation *Validation) Valid() bool
- func (validation *Validation) When(condition bool, function func(val *Validation)) *Validation
- type ValidationFactory
- func (_factory *ValidationFactory) AddErrorMessage(name string, message string) *Validation
- func (_factory *ValidationFactory) Check(v Validator) *Validation
- func (_factory *ValidationFactory) Do(function func(val *Validation)) *Validation
- func (_factory *ValidationFactory) If(condition bool, _validation *Validation) *Validation
- func (_factory *ValidationFactory) In(name string, v *Validation) *Validation
- func (_factory *ValidationFactory) InCell(name string, index int, v *Validation) *Validation
- func (_factory *ValidationFactory) InRow(name string, index int, v *Validation) *Validation
- func (_factory *ValidationFactory) Is(v Validator) *Validation
- func (_factory *ValidationFactory) New(options ...Options) *Validation
- func (_factory *ValidationFactory) When(condition bool, function func(val *Validation)) *Validation
- type Validator
- type ValidatorAny
- func (validator *ValidatorAny) Context() *ValidatorContext
- func (validator *ValidatorAny) EqualTo(value any, template ...string) *ValidatorAny
- func (validator *ValidatorAny) Nil(template ...string) *ValidatorAny
- func (validator *ValidatorAny) Not() *ValidatorAny
- func (validator *ValidatorAny) Or() *ValidatorAny
- func (validator *ValidatorAny) Passing(function func(v any) bool, template ...string) *ValidatorAny
- type ValidatorBool
- func (validator *ValidatorBool[T]) Context() *ValidatorContext
- func (validator *ValidatorBool[T]) EqualTo(value T, template ...string) *ValidatorBool[T]
- func (validator *ValidatorBool[T]) False(template ...string) *ValidatorBool[T]
- func (validator *ValidatorBool[T]) InSlice(slice []T, template ...string) *ValidatorBool[T]
- func (validator *ValidatorBool[T]) Not() *ValidatorBool[T]
- func (validator *ValidatorBool[T]) Or() *ValidatorBool[T]
- func (validator *ValidatorBool[T]) Passing(function func(v T) bool, template ...string) *ValidatorBool[T]
- func (validator *ValidatorBool[T]) True(template ...string) *ValidatorBool[T]
- type ValidatorBoolP
- func (validator *ValidatorBoolP[T]) Context() *ValidatorContext
- func (validator *ValidatorBoolP[T]) EqualTo(value T, template ...string) *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) False(template ...string) *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) FalseOrNil(template ...string) *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) InSlice(slice []T, template ...string) *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) Nil(template ...string) *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) Not() *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) Or() *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorBoolP[T]
- func (validator *ValidatorBoolP[T]) True(template ...string) *ValidatorBoolP[T]
- type ValidatorComparable
- func (validator *ValidatorComparable[T]) Context() *ValidatorContext
- func (validator *ValidatorComparable[T]) EqualTo(value T, template ...string) *ValidatorComparable[T]
- func (validator *ValidatorComparable[T]) InSlice(slice []T, template ...string) *ValidatorComparable[T]
- func (validator *ValidatorComparable[T]) Not() *ValidatorComparable[T]
- func (validator *ValidatorComparable[T]) Or() *ValidatorComparable[T]
- func (validator *ValidatorComparable[T]) Passing(function func(v T) bool, template ...string) *ValidatorComparable[T]
- type ValidatorComparableP
- func (validator *ValidatorComparableP[T]) Context() *ValidatorContext
- func (validator *ValidatorComparableP[T]) EqualTo(value T, template ...string) *ValidatorComparableP[T]
- func (validator *ValidatorComparableP[T]) InSlice(slice []T, template ...string) *ValidatorComparableP[T]
- func (validator *ValidatorComparableP[T]) Nil(template ...string) *ValidatorComparableP[T]
- func (validator *ValidatorComparableP[T]) Not() *ValidatorComparableP[T]
- func (validator *ValidatorComparableP[T]) Or() *ValidatorComparableP[T]
- func (validator *ValidatorComparableP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorComparableP[T]
- type ValidatorContext
- func (ctx *ValidatorContext) Add(function func() bool, errorKey string, template ...string) *ValidatorContext
- func (ctx *ValidatorContext) AddWithParams(function func() bool, errorKey string, templateParams map[string]any, ...) *ValidatorContext
- func (ctx *ValidatorContext) AddWithValue(function func() bool, errorKey string, value any, template ...string) *ValidatorContext
- func (ctx *ValidatorContext) Not() *ValidatorContext
- func (ctx *ValidatorContext) Or() *ValidatorContext
- func (ctx *ValidatorContext) Value() any
- type ValidatorFloat
- func (validator *ValidatorFloat[T]) Between(min T, max T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Context() *ValidatorContext
- func (validator *ValidatorFloat[T]) EqualTo(value T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Finite(template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) GreaterThan(value T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) InSlice(slice []T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Infinite(template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) LessOrEqualTo(value T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) LessThan(value T, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) NaN(template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Negative(template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Not() *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Or() *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Passing(function func(v T) bool, template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Positive(template ...string) *ValidatorFloat[T]
- func (validator *ValidatorFloat[T]) Zero(template ...string) *ValidatorFloat[T]
- type ValidatorFloatP
- func (validator *ValidatorFloatP[T]) Between(min T, max T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Context() *ValidatorContext
- func (validator *ValidatorFloatP[T]) EqualTo(value T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Finite(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) GreaterThan(value T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) InSlice(slice []T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Infinite(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) LessOrEqualTo(value T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) LessThan(value T, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) NaN(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Negative(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Nil(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Not() *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Or() *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Positive(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) Zero(template ...string) *ValidatorFloatP[T]
- func (validator *ValidatorFloatP[T]) ZeroOrNil(template ...string) *ValidatorFloatP[T]
- type ValidatorInt
- func Int[T ~int](value T, nameAndTitle ...string) *ValidatorInt[T]
- func Int8[T ~int8](value T, nameAndTitle ...string) *ValidatorInt[T]
- func Int16[T ~int16](value T, nameAndTitle ...string) *ValidatorInt[T]
- func Int32[T ~int32](value T, nameAndTitle ...string) *ValidatorInt[T]
- func Int64[T ~int64](value T, nameAndTitle ...string) *ValidatorInt[T]
- func Rune[T ~rune](value T, nameAndTitle ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Between(min T, max T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Context() *ValidatorContext
- func (validator *ValidatorInt[T]) EqualTo(value T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) GreaterThan(value T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) InSlice(slice []T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) LessOrEqualTo(value T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) LessThan(value T, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Negative(template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Not() *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Or() *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Passing(function func(v T) bool, template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Positive(template ...string) *ValidatorInt[T]
- func (validator *ValidatorInt[T]) Zero(template ...string) *ValidatorInt[T]
- type ValidatorIntP
- func Int8P[T ~int8](value *T, nameAndTitle ...string) *ValidatorIntP[T]
- func Int16P[T ~int16](value *T, nameAndTitle ...string) *ValidatorIntP[T]
- func Int32P[T ~int32](value *T, nameAndTitle ...string) *ValidatorIntP[T]
- func Int64P[T ~int64](value *T, nameAndTitle ...string) *ValidatorIntP[T]
- func IntP[T ~int](value *T, nameAndTitle ...string) *ValidatorIntP[T]
- func RuneP[T ~rune](value *T, nameAndTitle ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Between(min T, max T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Context() *ValidatorContext
- func (validator *ValidatorIntP[T]) EqualTo(value T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) GreaterThan(value T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) InSlice(slice []T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) LessOrEqualTo(value T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) LessThan(value T, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Negative(template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Nil(template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Not() *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Or() *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Positive(template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) Zero(template ...string) *ValidatorIntP[T]
- func (validator *ValidatorIntP[T]) ZeroOrNil(template ...string) *ValidatorIntP[T]
- type ValidatorNumber
- func (validator *ValidatorNumber[T]) Between(min T, max T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) Context() *ValidatorContext
- func (validator *ValidatorNumber[T]) EqualTo(value T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) GreaterThan(value T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) InSlice(slice []T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) LessOrEqualTo(value T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) LessThan(value T, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) Not() *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) Or() *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) Passing(function func(v T) bool, template ...string) *ValidatorNumber[T]
- func (validator *ValidatorNumber[T]) Zero(template ...string) *ValidatorNumber[T]
- type ValidatorNumberP
- func (validator *ValidatorNumberP[T]) Between(min T, max T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) Context() *ValidatorContext
- func (validator *ValidatorNumberP[T]) EqualTo(value T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) GreaterThan(value T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) InSlice(slice []T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) LessOrEqualTo(value T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) LessThan(value T, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) Nil(template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) Not() *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) Or() *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) Zero(template ...string) *ValidatorNumberP[T]
- func (validator *ValidatorNumberP[T]) ZeroOrNil(template ...string) *ValidatorNumberP[T]
- type ValidatorString
- func (validator *ValidatorString[T]) Between(min T, max T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) Blank(template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) Context() *ValidatorContext
- func (validator *ValidatorString[T]) Empty(template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) EqualTo(value T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) GreaterThan(value T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) InSlice(slice []T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) LessOrEqualTo(value T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) LessThan(value T, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) MatchingTo(regex *regexp.Regexp, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) MaxBytes(length int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) MaxLength(length int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) MinBytes(length int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) MinLength(length int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) Not() *ValidatorString[T]
- func (validator *ValidatorString[T]) OfByteLength(length int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) OfByteLengthBetween(min int, max int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) OfLength(length int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) OfLengthBetween(min int, max int, template ...string) *ValidatorString[T]
- func (validator *ValidatorString[T]) Or() *ValidatorString[T]
- func (validator *ValidatorString[T]) Passing(function func(v0 T) bool, template ...string) *ValidatorString[T]
- type ValidatorStringP
- func (validator *ValidatorStringP[T]) Between(min T, max T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) Blank(template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) BlankOrNil(template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) Context() *ValidatorContext
- func (validator *ValidatorStringP[T]) Empty(template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) EmptyOrNil(template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) EqualTo(value T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) GreaterThan(value T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) InSlice(slice []T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) LessOrEqualTo(value T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) LessThan(value T, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) MatchingTo(regex *regexp.Regexp, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) MaxBytes(length int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) MaxLength(length int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) MinBytes(length int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) MinLength(length int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) Nil(template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) Not() *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) OfByteLength(length int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) OfByteLengthBetween(min int, max int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) OfLength(length int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) OfLengthBetween(min int, max int, template ...string) *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) Or() *ValidatorStringP[T]
- func (validator *ValidatorStringP[T]) Passing(function func(v0 *T) bool, template ...string) *ValidatorStringP[T]
- type ValidatorTime
- func (validator *ValidatorTime) After(value time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) AfterOrEqualTo(value time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) Before(value time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) BeforeOrEqualTo(value time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) Between(min time.Time, max time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) Context() *ValidatorContext
- func (validator *ValidatorTime) EqualTo(value time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) InSlice(slice []time.Time, template ...string) *ValidatorTime
- func (validator *ValidatorTime) Not() *ValidatorTime
- func (validator *ValidatorTime) Or() *ValidatorTime
- func (validator *ValidatorTime) Passing(function func(v0 time.Time) bool, template ...string) *ValidatorTime
- func (validator *ValidatorTime) Zero(template ...string) *ValidatorTime
- type ValidatorTimeP
- func (validator *ValidatorTimeP) After(value time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) AfterOrEqualTo(value time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) Before(value time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) BeforeOrEqualTo(value time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) Between(min time.Time, max time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) Context() *ValidatorContext
- func (validator *ValidatorTimeP) EqualTo(value time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) InSlice(slice []time.Time, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) Nil(template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) NilOrZero(template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) Not() *ValidatorTimeP
- func (validator *ValidatorTimeP) Or() *ValidatorTimeP
- func (validator *ValidatorTimeP) Passing(function func(v0 *time.Time) bool, template ...string) *ValidatorTimeP
- func (validator *ValidatorTimeP) Zero(template ...string) *ValidatorTimeP
- type ValidatorTyped
- func (validator *ValidatorTyped[T]) Context() *ValidatorContext
- func (validator *ValidatorTyped[T]) Nil(template ...string) *ValidatorTyped[T]
- func (validator *ValidatorTyped[T]) Not() *ValidatorTyped[T]
- func (validator *ValidatorTyped[T]) Or() *ValidatorTyped[T]
- func (validator *ValidatorTyped[T]) Passing(function func(v T) bool, template ...string) *ValidatorTyped[T]
- type ValidatorUint
- func Byte[T ~byte](value T, nameAndTitle ...string) *ValidatorUint[T]
- func Uint[T ~uint](value T, nameAndTitle ...string) *ValidatorUint[T]
- func Uint8[T ~uint8](value T, nameAndTitle ...string) *ValidatorUint[T]
- func Uint16[T ~uint16](value T, nameAndTitle ...string) *ValidatorUint[T]
- func Uint32[T ~uint32](value T, nameAndTitle ...string) *ValidatorUint[T]
- func Uint64[T ~uint64](value T, nameAndTitle ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) Between(min T, max T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) Context() *ValidatorContext
- func (validator *ValidatorUint[T]) EqualTo(value T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) GreaterThan(value T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) InSlice(slice []T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) LessOrEqualTo(value T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) LessThan(value T, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) Not() *ValidatorUint[T]
- func (validator *ValidatorUint[T]) Or() *ValidatorUint[T]
- func (validator *ValidatorUint[T]) Passing(function func(v T) bool, template ...string) *ValidatorUint[T]
- func (validator *ValidatorUint[T]) Zero(template ...string) *ValidatorUint[T]
- type ValidatorUintP
- func ByteP[T ~byte](value *T, nameAndTitle ...string) *ValidatorUintP[T]
- func Uint8P[T ~uint8](value *T, nameAndTitle ...string) *ValidatorUintP[T]
- func Uint16P[T ~uint16](value *T, nameAndTitle ...string) *ValidatorUintP[T]
- func Uint32P[T ~uint32](value *T, nameAndTitle ...string) *ValidatorUintP[T]
- func Uint64P[T ~uint64](value *T, nameAndTitle ...string) *ValidatorUintP[T]
- func UintP[T ~uint](value *T, nameAndTitle ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Between(min T, max T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Context() *ValidatorContext
- func (validator *ValidatorUintP[T]) EqualTo(value T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) GreaterThan(value T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) InSlice(slice []T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) LessOrEqualTo(value T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) LessThan(value T, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Nil(template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Not() *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Or() *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) Zero(template ...string) *ValidatorUintP[T]
- func (validator *ValidatorUintP[T]) ZeroOrNil(template ...string) *ValidatorUintP[T]
Examples ¶
Constants ¶
const ( ErrorKeyAfter = "after" ErrorKeyNotAfter = "not_after" ErrorKeyAfterOrEqualTo = "after_equal_to" ErrorKeyNotAfterOrEqualTo = "not_after_equal_to" ErrorKeyBefore = "before" ErrorKeyNotBefore = "not_before" ErrorKeyBeforeOrEqualTo = "before_equal_to" ErrorKeyNotBeforeOrEqualTo = "not_before_equal_to" ErrorKeyBetween = "between" ErrorKeyNotBetween = "not_between" ErrorKeyBlank = "blank" ErrorKeyNotBlank = "not_blank" ErrorKeyEmpty = "empty" ErrorKeyNotEmpty = "not_empty" ErrorKeyEqualTo = "equal_to" ErrorKeyNotEqualTo = "not_equal_to" ErrorKeyFalse = "false" ErrorKeyNotFalse = "not_false" ErrorKeyGreaterOrEqualTo = "greater_equal_to" ErrorKeyNotGreaterOrEqualTo = "not_greater_equal_to" ErrorKeyGreaterThan = "greater_than" ErrorKeyNotGreaterThan = "not_greater_than" ErrorKeyInSlice = "in_slice" ErrorKeyNotInSlice = "not_in_slice" ErrorKeyLength = "length" ErrorKeyNotLength = "not_length" ErrorKeyLengthBetween = "length_between" ErrorKeyNotLengthBetween = "not_length_between" ErrorKeyLessOrEqualTo = "less_or_equal_to" ErrorKeyNotLessOrEqualTo = "not_less_or_equal_to" ErrorKeyLessThan = "less_than" ErrorKeyNotLessThan = "not_less_than" ErrorKeyMatchingTo = "matching_to" ErrorKeyNotMatchingTo = "not_matching_to" ErrorKeyMaxLength = "max_length" ErrorKeyNotMaxLength = "not_max_length" ErrorKeyMinLength = "min_length" ErrorKeyNotMinLength = "not_min_length" ErrorKeyNil = "nil" ErrorKeyNotNil = "not_nil" ErrorKeyPassing = "passing" ErrorKeyNotPassing = "not_passing" ErrorKeyTrue = "true" ErrorKeyNotTrue = "not_true" ErrorKeyZero = "zero" ErrorKeyNotZero = "not_zero" ErrorKeyPositive = "positive" ErrorKeyNotPositive = "not_positive" ErrorKeyNegative = "negative" ErrorKeyNotNegative = "not_negative" ErrorKeyZeroOrNil = "zero_or_nil" ErrorKeyNotZeroOrNil = "not_zero_or_nil" ErrorKeyPositiveOrNil = "positive_or_nil" ErrorKeyNotPositiveOrNil = "not_positive_or_nil" ErrorKeyNegativeOrNil = "negative_or_nil" ErrorKeyNotNegativeOrNil = "not_negative_or_nil" ErrorKeyNaN = "nan" ErrorKeyNotNaN = "not_nan" ErrorKeyInfinite = "infinite" ErrorKeyNotInfinite = "not_infinite" ErrorKeyFinite = "finite" ErrorKeyNotFinite = "not_finite" )
const ( LocaleCodeEn = "en" LocaleCodeEs = "es" LocaleCodeDe = "de" LocaleCodeHu = "hu" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Implementation of the Go error interface in Valgo. The [Validation.Error()] method returns a value of this type.
There is a function in this type, [Errors()], that returns a list of errors in a Validation session.
func (*Error) MarshalJSON ¶
Returns the JSON encoding of the validation error messages.
A custom function can be set either by passing it as a parameter to [validation.Error()] or through FactoryOptions.
func (*Error) MarshalJSONIndent ¶ added in v0.7.0
Returns the JSON encoding of the validation error messages with the given prefix and indent.
This function does not call a custom marshalJsonFunc function if it is set.
func (*Error) MarshalJSONPretty ¶ added in v0.7.0
Returns the JSON encoding of the validation error messages with the pretty format.
This is a shortcut for MarshalJSONIndent("", " "). It does not call a custom marshalJSONFunc function if it is set.
type FactoryOptions ¶ added in v0.2.0
type FactoryOptions struct {
// A string field that represents the default locale code to use by the
// factory if a specific locale code is not provided when a Validation is
// created
LocaleCodeDefault string
// A map field that allows to modify the current or add new locales
Locales map[string]*Locale
// A function field that allows to set a custom JSON marshaler for [Error]
MarshalJsonFunc func(e *Error) ([]byte, error)
}
FactoryOptions is a struct in Go that is used to pass options to a [Factory()]
type Locale ¶ added in v0.2.0
Locale is a type alias that represents a map of locale entries. The keys in the map are strings that represent the entry's identifier, and the values are strings that contain the corresponding localized text for that entry
type Options ¶ added in v0.2.0
type Options struct {
// A string field that represents the locale code to use by the [Validation]
// session
LocaleCode string
// A map field that allows to modify or add a new [Locale]
Locale *Locale
// A function field that allows to set a custom JSON marshaler for [Error]
MarshalJsonFunc func(e *Error) ([]byte, error)
// contains filtered or unexported fields
}
type TypeNumber ¶
type TypeNumber interface {
~int |
~int8 |
~int16 |
~int32 |
~int64 |
~uint |
~uint8 |
~uint16 |
~uint32 |
~uint64 |
~float32 |
~float64
}
Custom generic type covering all numeric types. This type is used as the value type in ValidatorNumber and ValidatorNumberP.
type Validation ¶
type Validation struct {
// contains filtered or unexported fields
}
The Validation session in Valgo is the main structure for validating one or more values. It is called Validation in code.
A Validation session will contain one or more Validators, where each Validator will have the responsibility to validate a value with one or more rules.
There are multiple functions to create a Validation session, depending on the requirements:
- New()
- Is(...)
- In(...)
- Check(...)
- InRow(...)
- InCell(...)
- If(...)
- Do(...)
- When(...)
- [Merge](...)
- AddErrorMessage(...)
the function Is(...) is likely to be the most frequently used function in your validations. When Is(...) is called, the function creates a validation and receives a validator at the same time.
func AddErrorMessage ¶
func AddErrorMessage(name string, message string) *Validation
Create a new Validation session and add an error message to it without executing a field validator. By adding this error message, the Validation session will be marked as invalid.
func Check ¶
func Check(validators ...Validator) *Validation
The Check(...) function is similar to the Is(...) function, however with Check(...)` the Rules of the Validator parameter are not short-circuited, which means that regardless of whether a previous rule was valid, all rules are checked.
This example shows two rules that fail due to the empty value in the full_name Validator, and since the Validator is not short-circuited, both error messages are added to the error result.
Example ¶
val := Check(String("", "full_name").Not().Blank().OfLengthBetween(4, 20))
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "full_name": [ "Full name can't be blank", "Full name must have a length between \"4\" and \"20\"" ] }
func Do ¶ added in v0.7.0
func Do(function func(val *Validation)) *Validation
The Do(...) function executes the given function with the current Validation instance and returns the same instance.
See Validation.Do(...) for more information.
Example ¶
mustBeAdmin := true
val := Is(String("", "username").Not().Blank()).
Do(func(val *Validation) {
if mustBeAdmin {
val.Is(String("staff", "role").EqualTo("admin"))
}
})
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "role": [ "Role must be equal to \"admin\"" ], "username": [ "Username can't be blank" ] }
func If ¶ added in v0.7.0
func If(condition bool, _validation *Validation) *Validation
The If(...) function is similar to [Merge](...), but merge the Validation session only when the condition is true, and returns the same Validation instance. When the condition is false, no operation is performed and the original instance is returned unchanged.
See [Merge](...) for more information.
Example ¶
mustBeAdmin := true
val := Is(String("", "username").Not().Blank()).
If(mustBeAdmin, Is(String("staff", "role").EqualTo("admin")))
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "role": [ "Role must be equal to \"admin\"" ], "username": [ "Username can't be blank" ] }
func In ¶
func In(name string, v *Validation) *Validation
The In(...) function executes one or more validators in a namespace, so the value names in the error result are prefixed with this namespace. This is useful for validating nested structures.
In the following example we are validating the Person and the nested Address structure. We can distinguish the errors of the nested Address structure in the error results.
Example ¶
type Address struct {
Name string
Street string
}
type Person struct {
Name string
Address Address
}
p := Person{"Bob", Address{"", "1600 Amphitheatre Pkwy"}}
val := Is(String(p.Name, "name").OfLengthBetween(4, 20)).
In("address", Is(
String(p.Address.Name, "name").Not().Blank()).Is(
String(p.Address.Street, "street").Not().Blank()))
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "address.name": [ "Name can't be blank" ], "name": [ "Name must have a length between \"4\" and \"20\"" ] }
func InCell ¶ added in v0.7.0
func InCell(name string, index int, v *Validation) *Validation
The InCell(...) function executes one or more validators in an indexed namespace where the target is a scalar value (e.g., entries of a primitive slice). The value names in the error result are prefixed with this indexed namespace.
Example ¶
// Example: Validating a slice of primitive values (strings)
tags := []string{"", "important", "urgent", ""}
val := Is(String("Project", "name").Not().Blank())
// Validate each tag in the slice using InCell
for i, tag := range tags {
val.InCell("tags", i, Is(String(tag, "tag").Not().Blank()))
}
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "tags[0]": [ "Tag can't be blank" ], "tags[3]": [ "Tag can't be blank" ] }
func InRow ¶
func InRow(name string, index int, v *Validation) *Validation
The InRow(...) function executes one or more validators in a namespace similar to the In(...) function, but with indexed namespace. So, the value names in the error result are prefixed with this indexed namespace. It is useful for validating nested lists in structures.
In the following example we validate the Person and the nested list Addresses. The error results can distinguish the errors of the nested list Addresses.
Example ¶
type Address struct {
Name string
Street string
}
type Person struct {
Name string
Addresses []Address
}
p := Person{
"Bob",
[]Address{
{"", "1600 Amphitheatre Pkwy"},
{"Home", ""},
},
}
val := Is(String(p.Name, "name").OfLengthBetween(4, 20))
for i, a := range p.Addresses {
val.InRow("addresses", i, Is(
String(a.Name, "name").Not().Blank()).Is(
String(a.Street, "street").Not().Blank()))
}
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "addresses[0].name": [ "Name can't be blank" ], "addresses[1].street": [ "Street can't be blank" ], "name": [ "Name must have a length between \"4\" and \"20\"" ] }
func Is ¶
func Is(validators ...Validator) *Validation
The Is(...) function allows you to pass a Validator with the value and the rules for validating it. At the same time, create a Validation session, which lets you add more Validators in order to verify more values.
As shown in the following example, we are passing to the function Is(...) the Validator for the full_name value. The function returns a Validation session that allows us to add more Validators to validate more values; in the example case the values age and status:
Example ¶
val := Is(String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20)).
Is(Number(17, "age").GreaterThan(18)).
Is(String("singl", "status").InSlice([]string{"married", "single"}))
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "age": [ "Age must be greater than \"18\"" ], "full_name": [ "Full name must have a length between \"4\" and \"20\"" ], "status": [ "Status is not valid" ] }
func New ¶
func New(options ...Options) *Validation
This function allows you to create a new Validation session without a Validator. This is useful for conditional validation, reusing validation logic or just to pass optional parameters to the Validation session.
The function accepts an optional parameter of type Options struct, which allows you to specify options such as the specific locale code and locale to use, and a custom JSON marshaler for errors.
The following example conditionally adds a validator rule for the month_day value.
Example ¶
month := 5
monthDay := 11
val := New()
if month == 6 {
val.Is(Number(monthDay, "month_day").LessOrEqualTo(10))
}
if val.Valid() {
fmt.Println("The validation passes")
}
Output: The validation passes
func When ¶ added in v0.7.0
func When(condition bool, function func(val *Validation)) *Validation
When(...) executes the given function passing the Validation instance only if the condition is true. When the condition is false, no operation is performed.
See Validation.When(...) for more information.
Example ¶
mustBeAdmin := true
val := Is(String("", "username").Not().Blank()).
When(mustBeAdmin, func(val *Validation) {
val.Is(String("staff", "role").EqualTo("admin"))
})
if !val.Valid() {
// NOTE: sortedErrorMarshalForDocs is an optional parameter used here for
// documentation purposes to ensure the order of keys in the JSON output.
out, _ := json.MarshalIndent(val.Error(sortedErrorMarshalForDocs), "", " ")
fmt.Println(string(out))
}
Output: { "role": [ "Role must be equal to \"admin\"" ], "username": [ "Username can't be blank" ] }
func (*Validation) AddErrorMessage ¶
func (v *Validation) AddErrorMessage(name string, message string) *Validation
Add an error message to the Validation session without executing a field validator. By adding this error message, the Validation session will be marked as invalid.
func (*Validation) Check ¶
func (validation *Validation) Check(validators ...Validator) *Validation
Check(...) adds one or more validators to a Validation session. But unlike [Is()], the validators are not short-circuited.
func (*Validation) Do ¶ added in v0.7.0
func (validation *Validation) Do(function func(val *Validation)) *Validation
The Do(...) function executes the given function with the current Validation instance and returns the same instance.
This allows you to extend a validation chain with additional or conditional rules in a concise way:
v.Is(v.String(username, "username").Not().Blank()).Do(func(val *v.Validation) {
if isAdmin {
val.Is(v.String(role, "role").Equal("admin"))
}
})
func (*Validation) Error ¶
func (validation *Validation) Error(marshalJsonFun ...func(e *Error) ([]byte, error)) error
Error returns the validation errors as a standard Go error interface.
DEPRECATED: This method is deprecated in favor of ToError() or ToValgoError(). The Error() method name conflicts with Go's error interface implementation convention, where Error() typically implements the error interface for a type.
Use ToError() for standard error handling or ToValgoError() for detailed validation error information.
func (*Validation) Errors ¶
func (session *Validation) Errors() map[string]*valueError
Return a map with the information for each invalid field validator in the Validation session.
func (*Validation) If ¶ added in v0.7.0
func (validation *Validation) If(condition bool, _validation *Validation) *Validation
If(...) is similar to [Merge](...), but merge the Validation session only when the condition is true, and returns the same Validation instance. When the condition is false, no operation is performed and the original instance is returned unchanged.
See [Merge](...) for more information.
v.If(isAdmin, v.Is(v.String(username, "username").Not().Blank()) )
func (*Validation) In ¶
func (validation *Validation) In(name string, _validation *Validation) *Validation
Add a map namespace to a Validation session.
func (*Validation) InCell ¶ added in v0.7.0
func (validation *Validation) InCell(name string, index int, _validation *Validation) *Validation
Add an indexed namespace to a Validation session where the target is a single, scalar value (e.g., entries of a primitive slice). This is useful for validating arrays or slices of primitives. Example:
validation := valgo.InCell("tag_priority", 0,
valgo.Is(valgo.String("", "tag_priority", "Tag priority").Not().Blank()),
)
The example above validates the value at tag_priority[0].
func (*Validation) InRow ¶
func (validation *Validation) InRow(name string, index int, _validation *Validation) *Validation
Add an indexed namespace to a Validation session.
func (*Validation) Is ¶
func (validation *Validation) Is(validators ...Validator) *Validation
Add one or more validators to a Validation session.
func (*Validation) IsValid ¶
func (validation *Validation) IsValid(name string) bool
Return true if a specific field validator is valid.
Example ¶
val := Is(Number(16, "age").GreaterThan(18)).
Is(String("single", "status").InSlice([]string{"married", "single"}))
if !val.IsValid("age") {
fmt.Println("Warning: someone underage is trying to sign up")
}
Output: Warning: someone underage is trying to sign up
func (*Validation) Merge ¶
func (validation *Validation) Merge(_validation *Validation) *Validation
Using [Merge](...) you can merge two Validation sessions. When two validations are merged, errors with the same value name will be merged. It is useful for reusing validation logic.
The following example merges the Validation session returned by the validatePreStatus function. Since both Validation sessions validate a value with the name status, the error returned will return two error messages, and without duplicate the Not().Blank() error message rule.
Example ¶
type Record struct {
Name string
Status string
}
validatePreStatus := func(status string) *Validation {
regex, _ := regexp.Compile("pre-.+")
return Check(String(status, "status").Not().Blank().MatchingTo(regex))
}
r := Record{"Classified", ""}
val := Is(
String(r.Name, "name").Not().Blank()).Is(
String(r.Status, "status").Not().Blank())
val.Merge(validatePreStatus(r.Status))
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
Output: { "status": [ "Status can't be blank", "Status must match to \"pre-.+\"" ] }
func (*Validation) MergeError ¶ added in v0.3.0
func (v *Validation) MergeError(err *Error) *Validation
MergeError allows merging Valgo errors from an already validated Validation session. The function takes an Valgo Error pointer as an argument and returns a Validation pointer.
func (*Validation) MergeErrorIn ¶ added in v0.3.0
func (v *Validation) MergeErrorIn(name string, err *Error) *Validation
MergeErrorIn allows merging Valgo errors from already validated Validation sessions within a map namespace. The function takes a namespace name and an Error pointer as arguments and returns a Validation pointer.
func (*Validation) MergeErrorInIndex ¶ added in v0.7.0
func (v *Validation) MergeErrorInIndex(name string, index int, err *Error) *Validation
MergeErrorInRow allows merging Valgo errors from already validated Validation sessions within an indexed namespace. These are errors added by InRow() and InCell() validations. The function takes a namespace name, an index, and an Error pointer as arguments and returns a Validation pointer.
func (*Validation) MergeErrorInRow ¶ added in v0.3.0
func (v *Validation) MergeErrorInRow(name string, index int, err *Error) *Validation
MergeErrorInRow allows merging Valgo errors from already validated Validation sessions within an indexed namespace. The function takes a namespace name, an index, and an Error pointer as arguments and returns a Validation pointer.
DEPRECATED: This method is deprecated in favor of MergeErrorInIndex(). The MergeErrorInIndex() method is a generic name to cover errors added by InRow() and InCell() validations.
func (*Validation) ToError ¶ added in v0.5.0
func (validation *Validation) ToError(marshalJsonFun ...func(e *Error) ([]byte, error)) error
ToError returns the validation errors as a standard Go error interface.
This method is useful for idiomatic error handling and integration with Go's native error system. It returns the same underlying error value as ToValgoError() but typed as the error interface.
Example:
val := Is(String("", "name").Not().Blank())
if err := val.ToError(); err != nil {
log.Printf("Validation failed: %v", err)
return err
}
An optional JSON marshaling function can be passed to customize how the validation errors are serialized into JSON. If no function is provided, a default marshaling behavior is used.
func (*Validation) ToValgoError ¶ added in v0.5.0
func (validation *Validation) ToValgoError(marshalJsonFun ...func(e *Error) ([]byte, error)) *Error
ToValgoError returns the validation errors as a *valgo.Error type, providing access to rich, structured error details. It's essentially a shortcut to `ToError().(*valgo.Error)`.
This method returns the underlying *valgo.Error type directly, exposing detailed validation information such as per-field messages, templates, and localized titles. It's the single source of truth for validation errors.
Example:
val := Is(String("", "name").Not().Blank())
if errInfo := val.ToValgoError(); errInfo != nil {
for field, valueError := range errInfo.Errors() {
fmt.Printf("Field '%s': %v\n", field, valueError.Messages())
}
}
An optional JSON marshaling function can be passed to customize how the validation errors are serialized into JSON. If no function is provided, a default marshaling behavior is used.
func (*Validation) Valid ¶
func (validation *Validation) Valid() bool
A Validation session provides this function which returns either true if all their validators are valid or false if any one of them is invalid.
In the following example, even though the Validator for age is valid, the Validator for status is invalid, making the entire Validator session invalid.
Example ¶
val := Is(Number(21, "age").GreaterThan(18)).
Is(String("singl", "status").InSlice([]string{"married", "single"}))
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
Output: { "status": [ "Status is not valid" ] }
func (*Validation) When ¶ added in v0.7.0
func (validation *Validation) When(condition bool, function func(val *Validation)) *Validation
When(...) is similar to Do(...), but executes the given function only when the condition is true, and returns the same Validation instance. When the condition is false, no operation is performed and the original instance is returned unchanged.
See Do(...) for the unconditional variant.
v.Is(v.String(username, "username").Not().Blank()).When(isAdmin, func(val *v.Validation) {
val.Is(v.String(role, "role").Equal("admin"))
})
type ValidationFactory ¶ added in v0.2.4
type ValidationFactory struct {
// contains filtered or unexported fields
}
func Factory ¶ added in v0.2.0
func Factory(options FactoryOptions) *ValidationFactory
Factory is a function used to create a Valgo factory.
With a Valgo factory, you can create Validation sessions with preset options, avoiding having to pass options each time when a Validation session is created.
This allows for more flexibility and easier management of options.
The Factory function accepts an options parameter of type FactoryOptions struct, which allows you to specify options such as the default locale code, available locales and a custom JSON marshaler for errors.
func (*ValidationFactory) AddErrorMessage ¶ added in v0.2.4
func (_factory *ValidationFactory) AddErrorMessage(name string, message string) *Validation
Create a new Validation session, through a factory, and add an error message to it without executing a field validator. By adding this error message, the Validation session will be marked as invalid.
func (*ValidationFactory) Check ¶ added in v0.2.4
func (_factory *ValidationFactory) Check(v Validator) *Validation
The Check function, through a factory, is similar to the Is function, however with Check the Rules of the Validator parameter are not short-circuited, which means that regardless of whether a previous rule was valid, all rules are checked.
The function is similar to the [Check()] function, but it uses a factory. For more information see the [Check()] function.
func (*ValidationFactory) Do ¶ added in v0.7.0
func (_factory *ValidationFactory) Do(function func(val *Validation)) *Validation
The Do function executes the given function with the current Validation instance and returns the same instance.
See Validation.Do(...) for more information.
func (*ValidationFactory) If ¶ added in v0.7.0
func (_factory *ValidationFactory) If(condition bool, _validation *Validation) *Validation
If(...) is similar to [Merge](...), but merge the Validation session only when the condition is true, and returns the same Validation instance. When the condition is false, no operation is performed and the original instance is returned unchanged.
See [Merge](...) for more information.
func (*ValidationFactory) In ¶ added in v0.2.4
func (_factory *ValidationFactory) In(name string, v *Validation) *Validation
The In function executes, through a factory, one or more validators in a namespace, so the value names in the error result are prefixed with this namespace. This is useful for validating nested structures.
The function is similar to the [In()] function, but it uses a factory. For more information see the [In()] function.
func (*ValidationFactory) InCell ¶ added in v0.7.0
func (_factory *ValidationFactory) InCell(name string, index int, v *Validation) *Validation
The InCell function executes, through a factory, one or more validators in an indexed namespace where the target is a scalar value (e.g., entries of a primitive slice). The value names in the error result are prefixed with this indexed namespace.
The function is similar to the [InCell()] function, but it uses a factory. For more information see the [InCell()] function.
func (*ValidationFactory) InRow ¶ added in v0.2.4
func (_factory *ValidationFactory) InRow(name string, index int, v *Validation) *Validation
The InRow function executes, through a factory, one or more validators in a namespace similar to the In(...) function, but with indexed namespace. So,\ the value names in the error result are prefixed with this indexed namespace. It is useful for validating nested lists in structures.
The function is similar to the [InRow()] function, but it uses a factory. For more information see the [InRow()] function.
func (*ValidationFactory) Is ¶ added in v0.2.4
func (_factory *ValidationFactory) Is(v Validator) *Validation
The Is function allows you to pass, through a factory, a Validator with the value and the rules for validating it. At the same time, create a Validation session, which lets you add more Validators in order to verify more values.
The function is similar to the [Is()] function, but it uses a factory. For more information see the [Is()] function.
func (*ValidationFactory) New ¶ added in v0.2.4
func (_factory *ValidationFactory) New(options ...Options) *Validation
This New function allows you to create, through a factory, a new Validation session without a Validator. This is useful for conditional validation or reusing validation logic.
The function is similar to the [New()] function, but it uses a factory. For more information see the [New()] function.
func (*ValidationFactory) When ¶ added in v0.7.0
func (_factory *ValidationFactory) When(condition bool, function func(val *Validation)) *Validation
The When function executes the given function passing the Validation instance only if the condition is true. When the condition is false, no operation is performed.
See Validation.When(...) for more information.
type Validator ¶
type Validator interface {
Context() *ValidatorContext
}
Interface implemented by valgo Validators and custom Validators.
type ValidatorAny ¶
type ValidatorAny struct {
// contains filtered or unexported fields
}
The Any validator's type that keeps its validator context.
func Any ¶
func Any(value any, nameAndTitle ...string) *ValidatorAny
Receive a value to validate.
The value can be any type;
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A value_%N` pattern is used as a name in the error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name phone_number will be humanized as Phone Number.
func (*ValidatorAny) Context ¶
func (validator *ValidatorAny) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorAny) EqualTo ¶
func (validator *ValidatorAny) EqualTo(value any, template ...string) *ValidatorAny
Validate if a value is equal to another. This function internally uses the golang `==` operator. For example:
status := "running"
Is(v.Any(status).Equal("running"))
DEPRECATED: 'any' is not safely comparable. Use the Comparable validator instead. This function will be removed in Valgo v1.0.0.
func (*ValidatorAny) Nil ¶
func (validator *ValidatorAny) Nil(template ...string) *ValidatorAny
Validate if a value is nil. For example:
var status *string Is(v.Any(status).Nil())
func (*ValidatorAny) Not ¶
func (validator *ValidatorAny) Not() *ValidatorAny
Invert the logical value associated with the next validator function. For example:
// It will return false because `Not()` inverts the boolean value associated with the `Equal()` function
Is(v.Any("a").Not().Equal("a")).Valid()
func (*ValidatorAny) Or ¶ added in v0.3.0
func (validator *ValidatorAny) Or() *ValidatorAny
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the string is equals "test".
input := "test"
isValid := v.Is(v.String(input).MinLength(5).Or().EqualTo("test")).Valid()
func (*ValidatorAny) Passing ¶
func (validator *ValidatorAny) Passing(function func(v any) bool, template ...string) *ValidatorAny
Validate if a value passes a custom function. For example:
status := ""
Is(v.Any(status).Passing((v any) bool {
return v == getNewStatus()
})
type ValidatorBool ¶
type ValidatorBool[T ~bool] struct { // contains filtered or unexported fields }
The Boolean validator type that keeps its validator context.
func Bool ¶
func Bool[T ~bool](value T, nameAndTitle ...string) *ValidatorBool[T]
func (*ValidatorBool[T]) Context ¶
func (validator *ValidatorBool[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorBool[T]) EqualTo ¶
func (validator *ValidatorBool[T]) EqualTo(value T, template ...string) *ValidatorBool[T]
Validate if a boolean value is equal to another. For example:
activated := true Is(v.Bool(activated).Equal(true))
func (*ValidatorBool[T]) False ¶
func (validator *ValidatorBool[T]) False(template ...string) *ValidatorBool[T]
Validate if a boolean value is false. For example:
activated := false Is(v.Bool(activated).Equal(true)).Valid()
func (*ValidatorBool[T]) InSlice ¶
func (validator *ValidatorBool[T]) InSlice(slice []T, template ...string) *ValidatorBool[T]
Validate if the value of a boolean pointer is present in a boolean slice. For example:
activated := false
elements := []bool{true, false, true}
Is(v.Bool(activated).InSlice(elements))
func (*ValidatorBool[T]) Not ¶
func (validator *ValidatorBool[T]) Not() *ValidatorBool[T]
Invert the boolean value associated with the next validator function. For example:
// It will return false because `Not()` inverts the boolean value associated with the True() function Is(v.Bool(true).Not().True()).Valid()
func (*ValidatorBool[T]) Or ¶ added in v0.3.0
func (validator *ValidatorBool[T]) Or() *ValidatorBool[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is equals false. input := true isValid := v.Is(v.Bool(input).False().Or().True()).Valid()
func (*ValidatorBool[T]) Passing ¶
func (validator *ValidatorBool[T]) Passing(function func(v T) bool, template ...string) *ValidatorBool[T]
Validate if a boolean value pass a custom function. For example:
activated := false
Is(v.Bool(activated).Passing((v bool) bool {
return v == someBoolFunction()
})
func (*ValidatorBool[T]) True ¶
func (validator *ValidatorBool[T]) True(template ...string) *ValidatorBool[T]
Validate if a boolean value is true. For example:
activated := true Is(v.Bool(activated).True())
type ValidatorBoolP ¶
type ValidatorBoolP[T ~bool] struct { // contains filtered or unexported fields }
The Boolean pointer validator type that keeps its validator context.
func BoolP ¶
func BoolP[T ~bool](value *T, nameAndTitle ...string) *ValidatorBoolP[T]
Receives a boolean pointer to validate.
The value also can be a custom boolean type such as `type Active bool;`
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorBoolP[T]) Context ¶
func (validator *ValidatorBoolP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorBoolP[T]) EqualTo ¶
func (validator *ValidatorBoolP[T]) EqualTo(value T, template ...string) *ValidatorBoolP[T]
Validate if the value of a boolean pointer is equal to another value. For example:
activated := true Is(v.BoolP(&activated).Equal(true))
func (*ValidatorBoolP[T]) False ¶
func (validator *ValidatorBoolP[T]) False(template ...string) *ValidatorBoolP[T]
Validate if the value of a boolean pointer is false. For example:
activated := false Is(v.BoolP(&activated).False())
func (*ValidatorBoolP[T]) FalseOrNil ¶
func (validator *ValidatorBoolP[T]) FalseOrNil(template ...string) *ValidatorBoolP[T]
Validate if the value of a boolean pointer is false or nil. For example:
var activated *bool Is(v.BoolP(activated).FalseOrNil()) *activated = false Is(v.BoolP(activated).FalseOrNil())
func (*ValidatorBoolP[T]) InSlice ¶
func (validator *ValidatorBoolP[T]) InSlice(slice []T, template ...string) *ValidatorBoolP[T]
Validate if the value of a boolean pointer is present in a boolean slice. For example:
activated := false
elements := []bool{true, false, true}
Is(v.BoolP(&activated).InSlice(elements))
func (*ValidatorBoolP[T]) Nil ¶
func (validator *ValidatorBoolP[T]) Nil(template ...string) *ValidatorBoolP[T]
Validate if a boolean pointer is nil. For example:
var activated *bool Is(v.BoolP(activated).Nil())
func (*ValidatorBoolP[T]) Not ¶
func (validator *ValidatorBoolP[T]) Not() *ValidatorBoolP[T]
Invert the boolean value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the True() function activated := true Is(v.BoolP(&activated).Not().True()).Valid()
func (*ValidatorBoolP[T]) Or ¶ added in v0.3.0
func (validator *ValidatorBoolP[T]) Or() *ValidatorBoolP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is equals false. input := true isValid := v.Is(v.BoolP(&input).Nil().Or().EqualTo(false)).Valid()
func (*ValidatorBoolP[T]) Passing ¶
func (validator *ValidatorBoolP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorBoolP[T]
Validate if a boolean pointer pass a custom function. For example:
activated := false
Is(v.BoolP(&activated).Passing((v *bool) bool {
return *v == someBoolFunction()
})
func (*ValidatorBoolP[T]) True ¶
func (validator *ValidatorBoolP[T]) True(template ...string) *ValidatorBoolP[T]
Validate if the value of a boolean pointer is true. For example:
activated := true Is(v.BoolP(&activated).True())
type ValidatorComparable ¶ added in v0.7.0
type ValidatorComparable[T comparable] struct { // contains filtered or unexported fields }
The Comparable validator's type that keeps its validator context. T can be any Go type (pointer, struct, etc.) that is comparable.
func Comparable ¶ added in v0.7.0
func Comparable[T comparable](value T, nameAndTitle ...string) *ValidatorComparable[T]
Receive a value of type T to validate, where T must be comparable.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A value_%N pattern is used as a name in the error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name phone_number will be humanized as Phone Number.
Example:
v.Is(v.Comparable(user).Not().Nil())
func (*ValidatorComparable[T]) Context ¶ added in v0.7.0
func (validator *ValidatorComparable[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorComparable[T]) EqualTo ¶ added in v0.7.0
func (validator *ValidatorComparable[T]) EqualTo(value T, template ...string) *ValidatorComparable[T]
Validate if a value is equal to another. This function internally uses the golang `==` operator. For example:
status := "running"
Is(v.Comparable(status).EqualTo("running"))
func (*ValidatorComparable[T]) InSlice ¶ added in v0.7.0
func (validator *ValidatorComparable[T]) InSlice(slice []T, template ...string) *ValidatorComparable[T]
Validate if a value is present in a slice. For example:
status := "idle"
validStatus := []string{"idle", "paused", "stopped"}
Is(v.Comparable(status).InSlice(validStatus))
func (*ValidatorComparable[T]) Not ¶ added in v0.7.0
func (validator *ValidatorComparable[T]) Not() *ValidatorComparable[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because `Not()` inverts the boolean value associated with `EqualTo()`
v.Is(v.Comparable("a").Not().EqualTo("a")).Valid()
func (*ValidatorComparable[T]) Or ¶ added in v0.7.0
func (validator *ValidatorComparable[T]) Or() *ValidatorComparable[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the string is equals "running".
status := "running"
isValid := v.Is(v.Comparable(status).EqualTo("paused").Or().EqualTo("running")).Valid()
func (*ValidatorComparable[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorComparable[T]) Passing(function func(v T) bool, template ...string) *ValidatorComparable[T]
Validate if a value passes a custom function. The function receives a typed T value, enabling compile-time type safety.
For example:
type Status string
status := Status("running")
isValid := v.Is(
v.Comparable(status).Passing(func(s Status) bool {
return s == "running" || s == "paused"
}),
).Valid()
type ValidatorComparableP ¶ added in v0.7.0
type ValidatorComparableP[T comparable] struct { // contains filtered or unexported fields }
The Comparable validator's type that keeps its validator context. T can be any Go type (pointer, struct, etc.) that is comparable.
func ComparableP ¶ added in v0.7.0
func ComparableP[T comparable](value *T, nameAndTitle ...string) *ValidatorComparableP[T]
Receive a value of type T to validate, where T must be comparable.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A value_%N pattern is used as a name in the error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name phone_number will be humanized as Phone Number.
Example:
v.Is(v.Comparable(user).Not().Nil())
func (*ValidatorComparableP[T]) Context ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorComparableP[T]) EqualTo ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) EqualTo(value T, template ...string) *ValidatorComparableP[T]
Validate if a value is equal to another. This function internally uses the golang `==` operator. For example:
status := "running"
Is(v.Comparable(status).EqualTo("running"))
func (*ValidatorComparableP[T]) InSlice ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) InSlice(slice []T, template ...string) *ValidatorComparableP[T]
Validate if a value is present in a slice. For example:
status := "idle"
validStatus := []string{"idle", "paused", "stopped"}
Is(v.Comparable(status).InSlice(validStatus))
func (*ValidatorComparableP[T]) Nil ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) Nil(template ...string) *ValidatorComparableP[T]
Validate if a value is nil. Works for nil-able kinds: pointers, slices, maps, chans, funcs, and interfaces. For non-nil-able types, this will return false.
For example:
var s *string v.Is(v.Comparable(s).Nil())
func (*ValidatorComparableP[T]) Not ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) Not() *ValidatorComparableP[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because `Not()` inverts the boolean value associated with `EqualTo()`
v.Is(v.Comparable("a").Not().EqualTo("a")).Valid()
func (*ValidatorComparableP[T]) Or ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) Or() *ValidatorComparableP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the string is equals "running".
status := "running"
isValid := v.Is(v.Comparable(status).EqualTo("paused").Or().EqualTo("running")).Valid()
func (*ValidatorComparableP[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorComparableP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorComparableP[T]
Validate if a value passes a custom function. The function receives a typed T value, enabling compile-time type safety.
For example:
type Status string
status := Status("running")
isValid := v.Is(
v.Comparable(status).Passing(func(s Status) bool {
return s == "running" || s == "paused"
}),
).Valid()
type ValidatorContext ¶
type ValidatorContext struct {
// contains filtered or unexported fields
}
The context keeps the state and provides the functions to control a custom validator.
func NewContext ¶
func NewContext(value any, nameAndTitle ...string) *ValidatorContext
Create a new ValidatorContext to be used by a custom validator.
func (*ValidatorContext) Add ¶
func (ctx *ValidatorContext) Add(function func() bool, errorKey string, template ...string) *ValidatorContext
Add a function to a custom validator.
func (*ValidatorContext) AddWithParams ¶
func (ctx *ValidatorContext) AddWithParams(function func() bool, errorKey string, templateParams map[string]any, template ...string) *ValidatorContext
Add a function to a custom validator and pass a map with values used for the validator function to be displayed in the error message.
func (*ValidatorContext) AddWithValue ¶
func (ctx *ValidatorContext) AddWithValue(function func() bool, errorKey string, value any, template ...string) *ValidatorContext
Add a function to a custom validator and pass a value used for the validator function to be displayed in the error message.
Use [AddWithParams()] if the error message requires more input values.
func (*ValidatorContext) Not ¶
func (ctx *ValidatorContext) Not() *ValidatorContext
Invert the boolean value associated with the next validator function in a custom validator.
func (*ValidatorContext) Or ¶ added in v0.3.0
func (ctx *ValidatorContext) Or() *ValidatorContext
Add Or operation to validation.
func (*ValidatorContext) Value ¶
func (ctx *ValidatorContext) Value() any
Return the value being validated in a custom validator.
type ValidatorFloat ¶ added in v0.7.0
The ValidatorFloat provides functions for setting validation rules for a float value types, or a custom type based on a float32 or float64.
func Float32 ¶
func Float32[T ~float32](value T, nameAndTitle ...string) *ValidatorFloat[T]
Receives a float32 value to validate.
The value can also be a custom float32 type such as type Price float32.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Float64 ¶
func Float64[T ~float64](value T, nameAndTitle ...string) *ValidatorFloat[T]
Receives a float64 value to validate.
The value can also be a custom float64 type such as type Price float64.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorFloat[T]) Between ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Between(min T, max T, template ...string) *ValidatorFloat[T]
Validate if a number is within a range (inclusive). For example:
Is(v.Float32(3).Between(2,6))
func (*ValidatorFloat[T]) Context ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorFloat[T]) EqualTo ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) EqualTo(value T, template ...string) *ValidatorFloat[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := float32(2) Is(v.Float32(quantity).EqualTo(2))
func (*ValidatorFloat[T]) Finite ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Finite(template ...string) *ValidatorFloat[T]
Validate if a numeric value is finite (not NaN and not infinite).
For example:
Is(v.Float32(3.14).Finite())
func (*ValidatorFloat[T]) GreaterOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorFloat[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := float32(3) Is(v.Float32(quantity).GreaterOrEqualTo(3))
func (*ValidatorFloat[T]) GreaterThan ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) GreaterThan(value T, template ...string) *ValidatorFloat[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := float32(3) Is(v.Float32(quantity).GreaterThan(2))
func (*ValidatorFloat[T]) InSlice ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) InSlice(slice []T, template ...string) *ValidatorFloat[T]
Validate if a number is present in a numeric slice. For example:
quantity := float32(3)
validQuantities := []float32{1,3,5}
Is(v.Float32(quantity).InSlice(validQuantities))
func (*ValidatorFloat[T]) Infinite ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Infinite(template ...string) *ValidatorFloat[T]
Validate if a numeric value is infinite (positive or negative infinity).
For example:
Is(v.Float32(math.Inf(1)).Infinite())
func (*ValidatorFloat[T]) LessOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) LessOrEqualTo(value T, template ...string) *ValidatorFloat[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := float32(2) Is(v.Float32(quantity).LessOrEqualTo(2))
func (*ValidatorFloat[T]) LessThan ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) LessThan(value T, template ...string) *ValidatorFloat[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := float32(2) Is(v.Float32(quantity).LessThan(3))
func (*ValidatorFloat[T]) NaN ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) NaN(template ...string) *ValidatorFloat[T]
Validate if a numeric value is NaN (Not a Number).
For example:
Is(v.Float32(math.NaN()).NaN())
func (*ValidatorFloat[T]) Negative ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Negative(template ...string) *ValidatorFloat[T]
Validate if a numeric value is negative (less than zero).
For example:
Is(v.Float32(-5.5).Negative())
func (*ValidatorFloat[T]) Not ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Not() *ValidatorFloat[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function Is(v.Float32(0).Not().Zero()).Valid()
func (*ValidatorFloat[T]) Or ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Or() *ValidatorFloat[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := float32(0) isValid := v.Is(v.Float32(input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorFloat[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Passing(function func(v T) bool, template ...string) *ValidatorFloat[T]
Validate if a numeric value passes a custom function. For example:
quantity := float32(2)
Is(v.Float32(quantity).Passing((v float32) bool {
return v == getAllowedQuantity()
})
func (*ValidatorFloat[T]) Positive ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Positive(template ...string) *ValidatorFloat[T]
Validate if a numeric value is positive (greater than zero).
For example:
Is(v.Float32(5.5).Positive())
func (*ValidatorFloat[T]) Zero ¶ added in v0.7.0
func (validator *ValidatorFloat[T]) Zero(template ...string) *ValidatorFloat[T]
Validate if a numeric value is zero.
For example:
Is(v.Float32(0).Zero())
type ValidatorFloatP ¶ added in v0.7.0
The ValidatorFloatP provides functions for setting validation rules for a float pointer value types, or a custom type based on a float32 or float64 pointer.
func Float32P ¶
func Float32P[T ~float32](value *T, nameAndTitle ...string) *ValidatorFloatP[T]
Receives a float32 pointer value to validate.
The value can also be a custom float32 pointer type such as type Price *float32.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Float64P ¶
func Float64P[T ~float64](value *T, nameAndTitle ...string) *ValidatorFloatP[T]
Receives a float64 pointer value to validate.
The value can also be a custom float64 pointer type such as type Price *float64.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorFloatP[T]) Between ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Between(min T, max T, template ...string) *ValidatorFloatP[T]
Validate if a number is within a range (inclusive). For example:
n := float32(3) Is(v.Float32P(&n).Between(2,6))
func (*ValidatorFloatP[T]) Context ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorFloatP[T]) EqualTo ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) EqualTo(value T, template ...string) *ValidatorFloatP[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := float32(2) Is(v.Float32P(&quantity).EqualTo(2))
func (*ValidatorFloatP[T]) Finite ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Finite(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is finite (not NaN and not infinite).
For example:
n := float32(3.14) Is(v.Float32P(&n).Finite())
func (*ValidatorFloatP[T]) GreaterOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorFloatP[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := float32(3) Is(v.Float32P(&quantity).GreaterOrEqualTo(3))
func (*ValidatorFloatP[T]) GreaterThan ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) GreaterThan(value T, template ...string) *ValidatorFloatP[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := float32(3) Is(v.Float32P(&quantity).GreaterThan(2))
func (*ValidatorFloatP[T]) InSlice ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) InSlice(slice []T, template ...string) *ValidatorFloatP[T]
Validate if a number is present in a numeric slice. For example:
quantity := float32(3)
validQuantities := []float32{1,3,5}
Is(v.Float32P(&quantity).InSlice(validQuantities))
func (*ValidatorFloatP[T]) Infinite ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Infinite(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is infinite (positive or negative infinity).
For example:
n := float32(math.Inf(1)) Is(v.Float32P(&n).Infinite())
func (*ValidatorFloatP[T]) LessOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) LessOrEqualTo(value T, template ...string) *ValidatorFloatP[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := float32(2) Is(v.Float32P(&quantity).LessOrEqualTo(2))
func (*ValidatorFloatP[T]) LessThan ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) LessThan(value T, template ...string) *ValidatorFloatP[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := float32(2) Is(v.Float32P(&quantity).LessThan(3))
func (*ValidatorFloatP[T]) NaN ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) NaN(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is NaN (Not a Number).
For example:
n := float32(math.NaN()) Is(v.Float32P(&n).NaN())
func (*ValidatorFloatP[T]) Negative ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Negative(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is negative (less than zero).
For example:
n := float32(-5.5) Is(v.Float32P(&n).Negative())
func (*ValidatorFloatP[T]) Nil ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Nil(template ...string) *ValidatorFloatP[T]
Validate if a numeric pointer value is nil.
For example:
var n *float32 Is(v.Float32P(n).Nil())
func (*ValidatorFloatP[T]) Not ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Not() *ValidatorFloatP[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function n := float32(0) Is(v.Float32P(&n).Not().Zero()).Valid()
func (*ValidatorFloatP[T]) Or ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Or() *ValidatorFloatP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := float32(0) isValid := v.Is(v.Float32P(&input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorFloatP[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorFloatP[T]
Validate if a numeric value passes a custom function. For example:
quantity := float32(2)
Is(v.Float32P(&quantity).Passing((v *float32) bool {
return *v == getAllowedQuantity()
})
func (*ValidatorFloatP[T]) Positive ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Positive(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is positive (greater than zero).
For example:
n := float32(5.5) Is(v.Float32P(&n).Positive())
func (*ValidatorFloatP[T]) Zero ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) Zero(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is zero.
For example:
n := float32(0) Is(v.Float32P(&n).Zero())
func (*ValidatorFloatP[T]) ZeroOrNil ¶ added in v0.7.0
func (validator *ValidatorFloatP[T]) ZeroOrNil(template ...string) *ValidatorFloatP[T]
Validate if a numeric value is zero or nil.
For example:
n := float32(0) Is(v.Float32P(&n).ZeroOrNil()) var n *float32 Is(v.Float32P(n).ZeroOrNil())
type ValidatorInt ¶
type ValidatorInt[T ~int | ~int8 | ~int16 | ~int32 | ~int64] struct { // contains filtered or unexported fields }
The ValidatorInt provides functions for setting validation rules for a int value types, or a custom type based on a int, int8, int16, int32, or int64.
func Int ¶
func Int[T ~int](value T, nameAndTitle ...string) *ValidatorInt[T]
Receives an int value to validate.
The value can also be a custom int type such as type Age int.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int8 ¶
func Int8[T ~int8](value T, nameAndTitle ...string) *ValidatorInt[T]
Receives an int8 value to validate.
The value can also be a custom int8 type such as type Age int8.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int16 ¶
func Int16[T ~int16](value T, nameAndTitle ...string) *ValidatorInt[T]
Receives an int16 value to validate.
The value can also be a custom int16 type such as type Age int16.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int32 ¶
func Int32[T ~int32](value T, nameAndTitle ...string) *ValidatorInt[T]
Receives an int32 value to validate.
The value can also be a custom int32 type such as type Age int32.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int64 ¶
func Int64[T ~int64](value T, nameAndTitle ...string) *ValidatorInt[T]
Receives an int64 value to validate.
The value can also be a custom int64 type such as type Age int64.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Rune ¶
func Rune[T ~rune](value T, nameAndTitle ...string) *ValidatorInt[T]
Receives a rune value to validate.
The value can also be a custom rune type such as type Age rune.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorInt[T]) Between ¶
func (validator *ValidatorInt[T]) Between(min T, max T, template ...string) *ValidatorInt[T]
Validate if a number is within a range (inclusive). For example:
Is(v.Int(3).Between(2,6))
func (*ValidatorInt[T]) Context ¶
func (validator *ValidatorInt[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorInt[T]) EqualTo ¶
func (validator *ValidatorInt[T]) EqualTo(value T, template ...string) *ValidatorInt[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := int(2) Is(v.Int(quantity).EqualTo(2))
func (*ValidatorInt[T]) GreaterOrEqualTo ¶
func (validator *ValidatorInt[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorInt[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := int(3) Is(v.Int(quantity).GreaterOrEqualTo(3))
func (*ValidatorInt[T]) GreaterThan ¶
func (validator *ValidatorInt[T]) GreaterThan(value T, template ...string) *ValidatorInt[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := int(3) Is(v.Int(quantity).GreaterThan(2))
func (*ValidatorInt[T]) InSlice ¶
func (validator *ValidatorInt[T]) InSlice(slice []T, template ...string) *ValidatorInt[T]
Validate if a number is present in a numeric slice. For example:
quantity := int(3)
validQuantities := []int{1,3,5}
Is(v.Int(quantity).InSlice(validQuantities))
func (*ValidatorInt[T]) LessOrEqualTo ¶
func (validator *ValidatorInt[T]) LessOrEqualTo(value T, template ...string) *ValidatorInt[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := int(2) Is(v.Int(quantity).LessOrEqualTo(2))
func (*ValidatorInt[T]) LessThan ¶
func (validator *ValidatorInt[T]) LessThan(value T, template ...string) *ValidatorInt[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := int(2) Is(v.Int(quantity).LessThan(3))
func (*ValidatorInt[T]) Negative ¶ added in v0.7.0
func (validator *ValidatorInt[T]) Negative(template ...string) *ValidatorInt[T]
Validate if a numeric value is negative (less than zero).
For example:
Is(v.Int(-5).Negative())
func (*ValidatorInt[T]) Not ¶
func (validator *ValidatorInt[T]) Not() *ValidatorInt[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function Is(v.Int(0).Not().Zero()).Valid()
func (*ValidatorInt[T]) Or ¶ added in v0.3.0
func (validator *ValidatorInt[T]) Or() *ValidatorInt[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := int(0) isValid := v.Is(v.Int(input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorInt[T]) Passing ¶
func (validator *ValidatorInt[T]) Passing(function func(v T) bool, template ...string) *ValidatorInt[T]
Validate if a numeric value passes a custom function. For example:
quantity := int(2)
Is(v.Int(quantity).Passing((v int) bool {
return v == getAllowedQuantity()
})
func (*ValidatorInt[T]) Positive ¶ added in v0.7.0
func (validator *ValidatorInt[T]) Positive(template ...string) *ValidatorInt[T]
Validate if a numeric value is positive (greater than zero).
For example:
Is(v.Int(5).Positive())
func (*ValidatorInt[T]) Zero ¶
func (validator *ValidatorInt[T]) Zero(template ...string) *ValidatorInt[T]
Validate if a numeric value is zero.
For example:
Is(v.Int(0).Zero())
type ValidatorIntP ¶
type ValidatorIntP[T ~int | ~int8 | ~int16 | ~int32 | ~int64] struct { // contains filtered or unexported fields }
The ValidatorIntP provides functions for setting validation rules for a int pointer value types, or a custom type based on a int, int8, int16, int32, or int64 pointer.
func Int8P ¶
func Int8P[T ~int8](value *T, nameAndTitle ...string) *ValidatorIntP[T]
Receives an int8 pointer value to validate.
The value can also be a custom int8 pointer type such as type Age *int8.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int16P ¶
func Int16P[T ~int16](value *T, nameAndTitle ...string) *ValidatorIntP[T]
Receives an int16 pointer value to validate.
The value can also be a custom int16 pointer type such as type Age *int16.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int32P ¶
func Int32P[T ~int32](value *T, nameAndTitle ...string) *ValidatorIntP[T]
Receives an int32 pointer value to validate.
The value can also be a custom int32 pointer type such as type Age *int32.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Int64P ¶
func Int64P[T ~int64](value *T, nameAndTitle ...string) *ValidatorIntP[T]
Receives an int64 pointer value to validate.
The value can also be a custom int64 pointer type such as type Age *int64.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func IntP ¶
func IntP[T ~int](value *T, nameAndTitle ...string) *ValidatorIntP[T]
Receives an int pointer value to validate.
The value can also be a custom int pointer type such as type Age *int.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func RuneP ¶
func RuneP[T ~rune](value *T, nameAndTitle ...string) *ValidatorIntP[T]
Receives a rune pointer value to validate.
The value can also be a custom rune pointer type such as type Age *rune.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorIntP[T]) Between ¶
func (validator *ValidatorIntP[T]) Between(min T, max T, template ...string) *ValidatorIntP[T]
Validate if a number is within a range (inclusive). For example:
n := int(3) Is(v.IntP(&n).Between(2,6))
func (*ValidatorIntP[T]) Context ¶
func (validator *ValidatorIntP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorIntP[T]) EqualTo ¶
func (validator *ValidatorIntP[T]) EqualTo(value T, template ...string) *ValidatorIntP[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := int(2) Is(v.IntP(&quantity).EqualTo(2))
func (*ValidatorIntP[T]) GreaterOrEqualTo ¶
func (validator *ValidatorIntP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorIntP[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := int(3) Is(v.IntP(&quantity).GreaterOrEqualTo(3))
func (*ValidatorIntP[T]) GreaterThan ¶
func (validator *ValidatorIntP[T]) GreaterThan(value T, template ...string) *ValidatorIntP[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := int(3) Is(v.IntP(&quantity).GreaterThan(2))
func (*ValidatorIntP[T]) InSlice ¶
func (validator *ValidatorIntP[T]) InSlice(slice []T, template ...string) *ValidatorIntP[T]
Validate if a number is present in a numeric slice. For example:
quantity := int(3)
validQuantities := []int{1,3,5}
Is(v.IntP(&quantity).InSlice(validQuantities))
func (*ValidatorIntP[T]) LessOrEqualTo ¶
func (validator *ValidatorIntP[T]) LessOrEqualTo(value T, template ...string) *ValidatorIntP[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := int(2) Is(v.IntP(&quantity).LessOrEqualTo(2))
func (*ValidatorIntP[T]) LessThan ¶
func (validator *ValidatorIntP[T]) LessThan(value T, template ...string) *ValidatorIntP[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := int(2) Is(v.IntP(&quantity).LessThan(3))
func (*ValidatorIntP[T]) Negative ¶ added in v0.7.0
func (validator *ValidatorIntP[T]) Negative(template ...string) *ValidatorIntP[T]
Validate if a numeric value is negative (less than zero).
For example:
n := int(-5) Is(v.IntP(&n).Negative())
func (*ValidatorIntP[T]) Nil ¶
func (validator *ValidatorIntP[T]) Nil(template ...string) *ValidatorIntP[T]
Validate if a numeric pointer value is nil.
For example:
var n *int Is(v.IntP(n).Nil())
func (*ValidatorIntP[T]) Not ¶
func (validator *ValidatorIntP[T]) Not() *ValidatorIntP[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function n := int(0) Is(v.IntP(&n).Not().Zero()).Valid()
func (*ValidatorIntP[T]) Or ¶ added in v0.3.0
func (validator *ValidatorIntP[T]) Or() *ValidatorIntP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := int(0) isValid := v.Is(v.IntP(&input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorIntP[T]) Passing ¶
func (validator *ValidatorIntP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorIntP[T]
Validate if a numeric value passes a custom function. For example:
quantity := int(2)
Is(v.IntP(&quantity).Passing((v *int) bool {
return *v == getAllowedQuantity()
})
func (*ValidatorIntP[T]) Positive ¶ added in v0.7.0
func (validator *ValidatorIntP[T]) Positive(template ...string) *ValidatorIntP[T]
Validate if a numeric value is positive (greater than zero).
For example:
n := int(5) Is(v.IntP(&n).Positive())
func (*ValidatorIntP[T]) Zero ¶
func (validator *ValidatorIntP[T]) Zero(template ...string) *ValidatorIntP[T]
Validate if a numeric value is zero.
For example:
n := int(0) Is(v.IntP(&n).Zero())
func (*ValidatorIntP[T]) ZeroOrNil ¶
func (validator *ValidatorIntP[T]) ZeroOrNil(template ...string) *ValidatorIntP[T]
Validate if a numeric value is zero or nil.
For example:
n := int(0) Is(v.IntP(&n).ZeroOrNil()) var n *int Is(v.IntP(n).ZeroOrNil())
type ValidatorNumber ¶
type ValidatorNumber[T TypeNumber] struct { // contains filtered or unexported fields }
The ValidatorNumber provides functions for setting validation rules for a TypeNumber value type, or a custom type based on a TypeNumber.
TypeNumber is a generic interface defined by Valgo that generalizes any standard Golang type.
func Number ¶
func Number[T TypeNumber](value T, nameAndTitle ...string) *ValidatorNumber[T]
func (*ValidatorNumber[T]) Between ¶
func (validator *ValidatorNumber[T]) Between(min T, max T, template ...string) *ValidatorNumber[T]
Validate if a number is within a range (inclusive). For example:
Is(v.Number(3).Between(2,6))
func (*ValidatorNumber[T]) Context ¶
func (validator *ValidatorNumber[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorNumber[T]) EqualTo ¶
func (validator *ValidatorNumber[T]) EqualTo(value T, template ...string) *ValidatorNumber[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := 2 Is(v.Number(quantity).Equal(2))
func (*ValidatorNumber[T]) GreaterOrEqualTo ¶
func (validator *ValidatorNumber[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorNumber[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := 3 Is(v.Number(quantity).GreaterOrEqualTo(3))
func (*ValidatorNumber[T]) GreaterThan ¶
func (validator *ValidatorNumber[T]) GreaterThan(value T, template ...string) *ValidatorNumber[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := 3 Is(v.Number(quantity).GreaterThan(2))
func (*ValidatorNumber[T]) InSlice ¶
func (validator *ValidatorNumber[T]) InSlice(slice []T, template ...string) *ValidatorNumber[T]
Validate if a number is present in a numeric slice. For example:
quantity := 3
validQuantities := []int{1,3,5}
Is(v.Number(quantity).InSlice(validQuantities))
func (*ValidatorNumber[T]) LessOrEqualTo ¶
func (validator *ValidatorNumber[T]) LessOrEqualTo(value T, template ...string) *ValidatorNumber[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := 2 Is(v.Number(quantity).LessOrEqualTo(2))
func (*ValidatorNumber[T]) LessThan ¶
func (validator *ValidatorNumber[T]) LessThan(value T, template ...string) *ValidatorNumber[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := 2 Is(v.Number(quantity).LessThan(3))
func (*ValidatorNumber[T]) Not ¶
func (validator *ValidatorNumber[T]) Not() *ValidatorNumber[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function Is(v.Number(0).Not().Zero()).Valid()
func (*ValidatorNumber[T]) Or ¶ added in v0.3.0
func (validator *ValidatorNumber[T]) Or() *ValidatorNumber[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := 0 isValid := v.Is(v.Number(input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorNumber[T]) Passing ¶
func (validator *ValidatorNumber[T]) Passing(function func(v T) bool, template ...string) *ValidatorNumber[T]
Validate if a numeric value passes a custom function. For example:
quantity := 2
Is(v.Number(quantity).Passing((v int) bool {
return v == getAllowedQuantity()
})
func (*ValidatorNumber[T]) Zero ¶
func (validator *ValidatorNumber[T]) Zero(template ...string) *ValidatorNumber[T]
Validate if a numeric value is zero.
For example:
Is(v.Number(0).Zero())
type ValidatorNumberP ¶
type ValidatorNumberP[T TypeNumber] struct { // contains filtered or unexported fields }
The Numeric pointer validator type that keeps its validator context.
func NumberP ¶
func NumberP[T TypeNumber](value *T, nameAndTitle ...string) *ValidatorNumberP[T]
Receives a numeric pointer to validate.
The value can be any golang numeric pointer type (*int64, *int32, *float32, *uint, etc.) or a custom numeric type such as `type Level *int32;`
Optionally, the function can receive a name and title, in that order, to be used in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorNumberP[T]) Between ¶
func (validator *ValidatorNumberP[T]) Between(min T, max T, template ...string) *ValidatorNumberP[T]
Validate if the value of a numeric pointer is within a range (inclusive). For example:
n := 3 Is(v.NumberP(&n).Between(2,6))
func (*ValidatorNumberP[T]) Context ¶
func (validator *ValidatorNumberP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorNumberP[T]) EqualTo ¶
func (validator *ValidatorNumberP[T]) EqualTo(value T, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is equal to another value. This function internally uses the golang `==` operator. For example:
quantity := 2 Is(v.NumberP(quantity).Equal(2))
func (*ValidatorNumberP[T]) GreaterOrEqualTo ¶
func (validator *ValidatorNumberP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is greater than or equal to another value. This function internally uses the golang `>=` operator. For example:
quantity := 3 Is(v.NumberP(&quantity).GreaterOrEqualTo(3))
func (*ValidatorNumberP[T]) GreaterThan ¶
func (validator *ValidatorNumberP[T]) GreaterThan(value T, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is greater than another value. This function internally uses the golang `>` operator. For example:
quantity := 3 Is(v.NumberP(&quantity).GreaterThan(2))
func (*ValidatorNumberP[T]) InSlice ¶
func (validator *ValidatorNumberP[T]) InSlice(slice []T, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is present in a numeric slice. For example:
quantity := 3
validQuantities := []int{1,3,5}
Is(v.NumberP(&quantity).InSlice(validQuantities))
func (*ValidatorNumberP[T]) LessOrEqualTo ¶
func (validator *ValidatorNumberP[T]) LessOrEqualTo(value T, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is less than or equal to another value. This function internally uses the golang `<=` operator. For example:
quantity := 2 Is(v.NumberP(&quantity).LessOrEqualTo(2))
func (*ValidatorNumberP[T]) LessThan ¶
func (validator *ValidatorNumberP[T]) LessThan(value T, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is less than another value. This function internally uses the golang `<` operator. For example:
quantity := 2 Is(v.NumberP(&quantity).LessThan(3))
func (*ValidatorNumberP[T]) Nil ¶
func (validator *ValidatorNumberP[T]) Nil(template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is nil.
For example:
var quantity *int Is(v.NumberP(quantity).Nil()) // Will be true
func (*ValidatorNumberP[T]) Not ¶
func (validator *ValidatorNumberP[T]) Not() *ValidatorNumberP[T]
Invert the boolean value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function n := 0 Is(v.NumberP(&n).Not().Zero()).Valid()
func (*ValidatorNumberP[T]) Or ¶ added in v0.3.0
func (validator *ValidatorNumberP[T]) Or() *ValidatorNumberP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := 0 isValid := v.Is(v.NumberP(&input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorNumberP[T]) Passing ¶
func (validator *ValidatorNumberP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value passes a custom function. For example:
quantity := 2
Is(v.NumberP(&quantity).Passing((v *int) bool {
return *v == getAllowedQuantity()
})
func (*ValidatorNumberP[T]) Zero ¶
func (validator *ValidatorNumberP[T]) Zero(template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is zero.
For example:
n := 0 Is(v.NumberP(&n).Zero())
func (*ValidatorNumberP[T]) ZeroOrNil ¶
func (validator *ValidatorNumberP[T]) ZeroOrNil(template ...string) *ValidatorNumberP[T]
Validate if a numeric pointer value is zero or nil.
For example:
var _quantity *int Is(v.NumberP(_quantity).ZeroOrNil()) // Will be true
type ValidatorString ¶
type ValidatorString[T ~string] struct { // contains filtered or unexported fields }
The `ValidatorString` provides functions for setting validation rules for a string value type, or a custom type based on a string.
func String ¶
func String[T ~string](value T, nameAndTitle ...string) *ValidatorString[T]
func (*ValidatorString[T]) Between ¶
func (validator *ValidatorString[T]) Between(min T, max T, template ...string) *ValidatorString[T]
Validate if the value of a string is within a range (inclusive). For example:
slug := "ab"
Is(v.String(slug).Between("ab","ac"))
func (*ValidatorString[T]) Blank ¶
func (validator *ValidatorString[T]) Blank(template ...string) *ValidatorString[T]
Validate if a string value is blank. Blank will be true if the length of the string is zero or if the string only has spaces. For example:
Is(v.String("").Empty()) // Will be true
Is(v.String(" ").Empty()) // Will be true
func (*ValidatorString[T]) Context ¶
func (validator *ValidatorString[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorString[T]) Empty ¶
func (validator *ValidatorString[T]) Empty(template ...string) *ValidatorString[T]
Validate if a string value is empty. Return false if the length of the string is greater than zero, even if the string has only spaces.
For checking if the string has only spaces, use the function `Blank()` instead. For example:
Is(v.String("").Empty()) // Will be true
Is(v.String(" ").Empty()) // Will be false
func (*ValidatorString[T]) EqualTo ¶
func (validator *ValidatorString[T]) EqualTo(value T, template ...string) *ValidatorString[T]
Validate if a string value is equal to another. This function internally uses the golang `==` operator. For example:
status := "running"
Is(v.String(status).Equal("running"))
func (*ValidatorString[T]) GreaterOrEqualTo ¶
func (validator *ValidatorString[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorString[T]
Validate if a string value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
section := "bc"
Is(v.String(section).GreaterOrEqualTo("bc"))
func (*ValidatorString[T]) GreaterThan ¶
func (validator *ValidatorString[T]) GreaterThan(value T, template ...string) *ValidatorString[T]
Validate if a string value is greater than another. This function internally uses the golang `>` operator. For example:
section := "bb"
Is(v.String(section).GreaterThan("ba"))
func (*ValidatorString[T]) InSlice ¶
func (validator *ValidatorString[T]) InSlice(slice []T, template ...string) *ValidatorString[T]
Validate if a string is present in a string slice. For example:
status := "idle"
validStatus := []string{"idle", "paused", "stopped"}
Is(v.String(status).InSlice(validStatus))
func (*ValidatorString[T]) LessOrEqualTo ¶
func (validator *ValidatorString[T]) LessOrEqualTo(value T, template ...string) *ValidatorString[T]
Validate if a string value is less than or equal to another. This function internally uses the golang `<=` operator to compare two strings. For example:
section := "bc"
Is(v.String(section).LessOrEqualTo("bc"))
func (*ValidatorString[T]) LessThan ¶
func (validator *ValidatorString[T]) LessThan(value T, template ...string) *ValidatorString[T]
Validate if a string value is less than another. This function internally uses the golang `<` operator. For example:
section := "bb"
Is(v.String(section).LessThan("bc"))
func (*ValidatorString[T]) MatchingTo ¶
func (validator *ValidatorString[T]) MatchingTo(regex *regexp.Regexp, template ...string) *ValidatorString[T]
Validate if a string matches a regular expression. For example:
status := "pre-approved"
regex, _ := regexp.Compile("pre-.+")
Is(v.String(status).MatchingTo(regex))
func (*ValidatorString[T]) MaxBytes ¶ added in v0.6.0
func (validator *ValidatorString[T]) MaxBytes(length int, template ...string) *ValidatorString[T]
Validate the maximum length (in bytes) of a string. For example:
slug := "myname" Is(v.String(slug).MaxBytes(6))
For character count, use `MaxLength` instead.
func (*ValidatorString[T]) MaxLength ¶
func (validator *ValidatorString[T]) MaxLength(length int, template ...string) *ValidatorString[T]
Validate the maximum length (in runes/characters) of a string. For example:
word := "虎視眈々" // 4 runes, len(word) = 12 bytes Is(v.String(word).MaxLength(4))
func (*ValidatorString[T]) MinBytes ¶ added in v0.6.0
func (validator *ValidatorString[T]) MinBytes(length int, template ...string) *ValidatorString[T]
Validate the minimum length (in bytes) of a string. For example:
slug := "myname" Is(v.String(slug).MinBytes(6))
For character count, use `MinLength` instead.
func (*ValidatorString[T]) MinLength ¶
func (validator *ValidatorString[T]) MinLength(length int, template ...string) *ValidatorString[T]
Validate the minimum length (in runes/characters) of a string. For example:
word := "虎視眈々" // 4 runes, len(word) = 12 bytes Is(v.String(word).MinLength(4))
func (*ValidatorString[T]) Not ¶
func (validator *ValidatorString[T]) Not() *ValidatorString[T]
Invert the boolean value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Blank() function
Is(v.String("").Not().Blank()).Valid()
func (*ValidatorString[T]) OfByteLength ¶ added in v0.6.0
func (validator *ValidatorString[T]) OfByteLength(length int, template ...string) *ValidatorString[T]
Validate the length (in bytes) of a string. For example:
slug := "myname" Is(v.String(slug).OfByteLength(6))
For character count, use `OfLength` instead.
func (*ValidatorString[T]) OfByteLengthBetween ¶ added in v0.6.0
func (validator *ValidatorString[T]) OfByteLengthBetween(min int, max int, template ...string) *ValidatorString[T]
Validate if the length (in bytes) of a string is within a range (inclusive). For example:
slug := "myname" Is(v.String(slug).OfByteLengthBetween(2,6))
For character count, use `OfLengthBetween` instead.
func (*ValidatorString[T]) OfLength ¶
func (validator *ValidatorString[T]) OfLength(length int, template ...string) *ValidatorString[T]
Validate the length (in runes/characters) of a string. For example:
word := "虎視眈々" // 4 runes, len(word) = 12 bytes Is(v.String(word).OfLength(4))
func (*ValidatorString[T]) OfLengthBetween ¶
func (validator *ValidatorString[T]) OfLengthBetween(min int, max int, template ...string) *ValidatorString[T]
Validate if the length (in runes/characters) of a string is within a range (inclusive). For example:
word := "虎視眈々" // 4 runes, len(word) = 12 bytes Is(v.String(word).OfLengthBetween(2,4))
func (*ValidatorString[T]) Or ¶ added in v0.3.0
func (validator *ValidatorString[T]) Or() *ValidatorString[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the string is equals "test".
input := "test"
isValid := v.Is(v.String(input).MinLength(5).Or().EqualTo("test")).Valid()
func (*ValidatorString[T]) Passing ¶
func (validator *ValidatorString[T]) Passing(function func(v0 T) bool, template ...string) *ValidatorString[T]
Validate if a string value passes a custom function. For example:
status := ""
Is(v.String(status).Passing((v string) bool {
return v == getNewStatus()
})
type ValidatorStringP ¶
type ValidatorStringP[T ~string] struct { // contains filtered or unexported fields }
The String pointer validator type that keeps its validator context.
func StringP ¶
func StringP[T ~string](value *T, nameAndTitle ...string) *ValidatorStringP[T]
func (*ValidatorStringP[T]) Between ¶
func (validator *ValidatorStringP[T]) Between(min T, max T, template ...string) *ValidatorStringP[T]
Validate if the value of a string pointer's value is in a range (inclusive). For example:
slug := "ab"
Is(v.StringP(&slug).Between("ab","ac"))
func (*ValidatorStringP[T]) Blank ¶
func (validator *ValidatorStringP[T]) Blank(template ...string) *ValidatorStringP[T]
Validate if a string value is blank. Blank will be true if the length of the string is zero or if the string only has spaces. For example:
status := "" Is(v.StringP(&status).Blank()) // Will be true status = " " Is(v.StringP(&status).Blank()) // Will be true
func (*ValidatorStringP[T]) BlankOrNil ¶
func (validator *ValidatorStringP[T]) BlankOrNil(template ...string) *ValidatorStringP[T]
Validate if a string value is blank or nil. Blank will be true if the length of the string is zero or if the string only has spaces. For example:
status := "" Is(v.StringP(&status).BlankOrNil()) // Will be true status = " " Is(v.StringP(&status).BlankOrNil()) // Will be true var _status *string Is(v.StringP(_status).BlankOrNil()) // Will be true
func (*ValidatorStringP[T]) Context ¶
func (validator *ValidatorStringP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorStringP[T]) Empty ¶
func (validator *ValidatorStringP[T]) Empty(template ...string) *ValidatorStringP[T]
Validate if a string value is empty. Empty will be false if the length of the string is greater than zero, even if the string has only spaces. For checking if the string has only spaces, uses the function `Blank()` instead. For example:
status := "" Is(v.StringP(&status).Empty()) // Will be true status = " " Is(v.StringP(&status).Empty()) // Will be false
func (*ValidatorStringP[T]) EmptyOrNil ¶
func (validator *ValidatorStringP[T]) EmptyOrNil(template ...string) *ValidatorStringP[T]
Validate if a string value is empty or nil. Empty will be false if the length of the string is greater than zero, even if the string has only spaces. For checking if the string has only spaces, uses the function `BlankOrNil()` instead. For example:
status := "" Is(v.StringP(&status).EmptyOrNil()) // Will be true status = " " Is(v.StringP(&status).EmptyOrNil()) // Will be false var _status *string Is(v.StringP(_status).EmptyOrNil()) // Will be true
func (*ValidatorStringP[T]) EqualTo ¶
func (validator *ValidatorStringP[T]) EqualTo(value T, template ...string) *ValidatorStringP[T]
Validate if the value of a string pointer is equal to a another value. For example:
status := "running"
Is(v.StringP(&status).Equal("running"))
func (*ValidatorStringP[T]) GreaterOrEqualTo ¶
func (validator *ValidatorStringP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorStringP[T]
func (*ValidatorStringP[T]) GreaterThan ¶
func (validator *ValidatorStringP[T]) GreaterThan(value T, template ...string) *ValidatorStringP[T]
Validate if a string value is greater than another. This function internally uses the golang `>` operator. For example:
section := "bb"
Is(v.StringP(§ion).GreaterThan("ba"))
func (*ValidatorStringP[T]) InSlice ¶
func (validator *ValidatorStringP[T]) InSlice(slice []T, template ...string) *ValidatorStringP[T]
Validate if the value of a string pointer is present in a string slice. For example:
status := "idle"
validStatus := []string{"idle", "paused", "stopped"}
Is(v.StringP(&status).InSlice(validStatus))
func (*ValidatorStringP[T]) LessOrEqualTo ¶
func (validator *ValidatorStringP[T]) LessOrEqualTo(value T, template ...string) *ValidatorStringP[T]
Validate if a string value is less or equal to another. This function internally uses the golang `<=` operator to compare two strings. For example:
section := "bc"
Is(v.StringP(§ion).LessOrEqualTo("bc"))
func (*ValidatorStringP[T]) LessThan ¶
func (validator *ValidatorStringP[T]) LessThan(value T, template ...string) *ValidatorStringP[T]
Validate if a string value is less than another. This function internally uses the golang `<` operator. For example:
section := "bb"
Is(v.StringP(§ion).LessThan("bc"))
func (*ValidatorStringP[T]) MatchingTo ¶
func (validator *ValidatorStringP[T]) MatchingTo(regex *regexp.Regexp, template ...string) *ValidatorStringP[T]
Validate if the value of a string pointer match a regular expression. For example:
status := "pre-approved"
regex, _ := regexp.Compile("pre-.+")
Is(v.StringP(&status).MatchingTo(regex))
func (*ValidatorStringP[T]) MaxBytes ¶ added in v0.6.0
func (validator *ValidatorStringP[T]) MaxBytes(length int, template ...string) *ValidatorStringP[T]
Validate the maximum length (in bytes) of a string pointer's value. For example:
slug := "myname" Is(v.StringP(&slug).MaxBytes(6))
For character count, use `MaxLength` instead.
func (*ValidatorStringP[T]) MaxLength ¶
func (validator *ValidatorStringP[T]) MaxLength(length int, template ...string) *ValidatorStringP[T]
Validate the maximum length (in runes/characters) of a string pointer's value. For example:
slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes Is(v.StringP(&slug).MaxLength(4))
func (*ValidatorStringP[T]) MinBytes ¶ added in v0.6.0
func (validator *ValidatorStringP[T]) MinBytes(length int, template ...string) *ValidatorStringP[T]
Validate the minimum length (in bytes) of a string pointer's value. For example:
slug := "myname" Is(v.StringP(&slug).MinBytes(6))
For character count, use `MinLength` instead.
func (*ValidatorStringP[T]) MinLength ¶
func (validator *ValidatorStringP[T]) MinLength(length int, template ...string) *ValidatorStringP[T]
Validate the minimum length (in runes/characters) of a string pointer's value. For example:
slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes Is(v.StringP(&slug).MinLength(4))
func (*ValidatorStringP[T]) Nil ¶
func (validator *ValidatorStringP[T]) Nil(template ...string) *ValidatorStringP[T]
Validate if a string pointer is nil. For example:
var status *string Is(v.StringP(status).Nil())
func (*ValidatorStringP[T]) Not ¶
func (validator *ValidatorStringP[T]) Not() *ValidatorStringP[T]
Invert the logical value associated to the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Blank() function status := "" Is(v.StringP(&status).Not().Blank()).Valid()
func (*ValidatorStringP[T]) OfByteLength ¶ added in v0.6.0
func (validator *ValidatorStringP[T]) OfByteLength(length int, template ...string) *ValidatorStringP[T]
Validate the length (in bytes) of a string pointer's value. For example:
slug := "myname" Is(v.StringP(&slug).OfByteLength(6))
For character count, use `OfLength` instead.
func (*ValidatorStringP[T]) OfByteLengthBetween ¶ added in v0.6.0
func (validator *ValidatorStringP[T]) OfByteLengthBetween(min int, max int, template ...string) *ValidatorStringP[T]
Validate if the length (in bytes) of a string pointer's value is in a range (inclusive). For example:
slug := "myname" Is(v.StringP(&slug).OfByteLengthBetween(2,6))
For character count, use `OfLengthBetween` instead.
func (*ValidatorStringP[T]) OfLength ¶
func (validator *ValidatorStringP[T]) OfLength(length int, template ...string) *ValidatorStringP[T]
Validate the length (in runes/characters) of a string pointer's value. For example:
slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes Is(v.StringP(&slug).OfLength(4))
func (*ValidatorStringP[T]) OfLengthBetween ¶
func (validator *ValidatorStringP[T]) OfLengthBetween(min int, max int, template ...string) *ValidatorStringP[T]
Validate if the length (in runes/characters) of a string pointer's value is in a range (inclusive). For example:
slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes Is(v.StringP(&slug).OfLengthBetween(2,4))
func (*ValidatorStringP[T]) Or ¶ added in v0.3.0
func (validator *ValidatorStringP[T]) Or() *ValidatorStringP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the string is equals "test".
input := "test"
isValid := v.Is(v.StringP(&input).MinLength(5).Or().EqualTo("test")).Valid()
func (*ValidatorStringP[T]) Passing ¶
func (validator *ValidatorStringP[T]) Passing(function func(v0 *T) bool, template ...string) *ValidatorStringP[T]
Validate if a string pointer pass a custom function. For example:
status := ""
Is(v.StringP(&status).Passing((v string) bool {
return v == getNewStatus()
})
type ValidatorTime ¶ added in v0.3.0
type ValidatorTime struct {
// contains filtered or unexported fields
}
The `ValidatorTime` structure provides a set of methods to perform validation checks on time.Time values, utilizing Go's native time package.
func Time ¶ added in v0.3.0
func Time(value time.Time, nameAndTitle ...string) *ValidatorTime
The Time function initiates a new `ValidatorTime` instance to validate a given time value. The optional name and title parameters can be used for enhanced error reporting. If a name is provided without a title, the name is humanized to be used as the title.
For example:
startTime := time.Now()
v := ValidatorTime{}
v.Time(startTime, "start_time", "Start Time")
func (*ValidatorTime) After ¶ added in v0.3.0
func (validator *ValidatorTime) After(value time.Time, template ...string) *ValidatorTime
The After method checks if the time value is after a specified time.
For example:
startTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) endTime := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC) Is(v.Time(endTime).After(startTime)).Valid()
func (*ValidatorTime) AfterOrEqualTo ¶ added in v0.3.0
func (validator *ValidatorTime) AfterOrEqualTo(value time.Time, template ...string) *ValidatorTime
The AfterOrEqualTo method checks if the time value is either after or equal to a specified time.
For example:
timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) timeB := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) Is(v.Time(timeA).AfterOrEqualTo(timeB)).Valid()
func (*ValidatorTime) Before ¶ added in v0.3.0
func (validator *ValidatorTime) Before(value time.Time, template ...string) *ValidatorTime
The Before method checks if the time value is before a specified time.
For example:
startTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) endTime := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC) Is(v.Time(startTime).Before(endTime)).Valid()
func (*ValidatorTime) BeforeOrEqualTo ¶ added in v0.3.0
func (validator *ValidatorTime) BeforeOrEqualTo(value time.Time, template ...string) *ValidatorTime
The BeforeOrEqualTo method checks if the time value is either before or equal to a specified time.
For example:
timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) timeB := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) Is(v.Time(timeA).BeforeOrEqualTo(timeB)).Valid()
func (*ValidatorTime) Between ¶ added in v0.3.0
func (validator *ValidatorTime) Between(min time.Time, max time.Time, template ...string) *ValidatorTime
The Between method verifies if the time value falls within a given time range, inclusive.
For example:
minTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) maxTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC) checkTime := time.Date(2023, 1, 1, 6, 0, 0, 0, time.UTC) Is(v.Time(checkTime).Between(minTime, maxTime)).Valid()
func (*ValidatorTime) Context ¶ added in v0.3.0
func (validator *ValidatorTime) Context() *ValidatorContext
The Context method returns the current context of the validator, which can be utilized to create custom validations by extending this validator.
func (*ValidatorTime) EqualTo ¶ added in v0.3.0
func (validator *ValidatorTime) EqualTo(value time.Time, template ...string) *ValidatorTime
The EqualTo method validates if the time value is equal to another given time value. It uses the equality (`==`) operator from Go for the comparison.
For example:
timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) timeB := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) Is(v.Time(timeA).EqualTo(timeB)).Valid()
func (*ValidatorTime) InSlice ¶ added in v0.3.0
func (validator *ValidatorTime) InSlice(slice []time.Time, template ...string) *ValidatorTime
The InSlice method validates if the time value is found within a provided slice of time values.
For example:
timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
timeB := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)
timeSlice := []time.Time{timeA, timeB}
checkTime := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)
Is(v.Time(checkTime).InSlice(timeSlice)).Valid()
func (*ValidatorTime) Not ¶ added in v0.3.0
func (validator *ValidatorTime) Not() *ValidatorTime
The Not method inverts the boolean value associated with the next validator method. This can be used to negate the check performed by the next validation method in the chain.
For example:
// Will return false because Not() inverts the boolean value of the Zero() function startTime := time.Now() Is(v.Time(startTime).Not().Zero()).Valid()
func (*ValidatorTime) Or ¶ added in v0.3.0
func (validator *ValidatorTime) Or() *ValidatorTime
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the time is before or equal to time.Now(). t := time.Now() isValid := v.Is(v.Time(t).Zero().Or().BeforeOrEqualTo(time.Now())).Valid()
func (*ValidatorTime) Passing ¶ added in v0.3.0
func (validator *ValidatorTime) Passing(function func(v0 time.Time) bool, template ...string) *ValidatorTime
The Passing method allows for custom validation logic by accepting a function that returns a boolean indicating whether the validation passed or failed.
For example:
checkTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
Is(v.Time(checkTime).Passing(func(t time.Time) bool {
return t.Year() == 2023
})).Valid()
func (*ValidatorTime) Zero ¶ added in v0.3.0
func (validator *ValidatorTime) Zero(template ...string) *ValidatorTime
The Zero method verifies if the time value is a zero time, which means it hasn't been initialized yet.
For example:
zeroTime := time.Time{}
Is(v.Time(zeroTime).Zero()).Valid()
type ValidatorTimeP ¶ added in v0.3.0
type ValidatorTimeP struct {
// contains filtered or unexported fields
}
ValidatorTimeP is a type that facilitates validation for time pointer variables. It retains a context that records details about the validation process.
func TimeP ¶ added in v0.3.0
func TimeP(value *time.Time, nameAndTitle ...string) *ValidatorTimeP
TimeP initializes a new ValidatorTimeP instance with the provided time pointer and optional name and title arguments for detailed error messages.
Usage example:
var myTime *time.Time v.TimeP(myTime, "start_time", "Start Time")
func (*ValidatorTimeP) After ¶ added in v0.3.0
func (validator *ValidatorTimeP) After(value time.Time, template ...string) *ValidatorTimeP
After validates that the time pointer is after the specified time value.
Usage example:
t1 := time.Now() t2 := t1.Add(-time.Hour) Is(v.TimeP(&t1).After(t2)).Valid() // Will return true.
func (*ValidatorTimeP) AfterOrEqualTo ¶ added in v0.3.0
func (validator *ValidatorTimeP) AfterOrEqualTo(value time.Time, template ...string) *ValidatorTimeP
AfterOrEqualTo validates that the time pointer is after or equal to the specified time value.
Usage example:
t1 := time.Now() t2 := t1 Is(v.TimeP(&t1).AfterOrEqualTo(t2)).Valid() // Will return true.
func (*ValidatorTimeP) Before ¶ added in v0.3.0
func (validator *ValidatorTimeP) Before(value time.Time, template ...string) *ValidatorTimeP
Before validates that the time pointer is before the specified time value.
Usage example:
t1 := time.Now() t2 := t1.Add(time.Hour) Is(v.TimeP(&t1).Before(t2)).Valid() // Will return true.
func (*ValidatorTimeP) BeforeOrEqualTo ¶ added in v0.3.0
func (validator *ValidatorTimeP) BeforeOrEqualTo(value time.Time, template ...string) *ValidatorTimeP
BeforeOrEqualTo validates that the time pointer is before or equal to the specified time value.
Usage example:
t1 := time.Now() t2 := t1 Is(v.TimeP(&t1).BeforeOrEqualTo(t2)).Valid() // Will return true.
func (*ValidatorTimeP) Between ¶ added in v0.3.0
func (validator *ValidatorTimeP) Between(min time.Time, max time.Time, template ...string) *ValidatorTimeP
Between validates that the time pointer is between the specified minimum and maximum time values (inclusive).
Usage example:
t1 := time.Now() min := t1.Add(-time.Hour) max := t1.Add(time.Hour) Is(v.TimeP(&t1).Between(min, max)).Valid() // Will return true.
func (*ValidatorTimeP) Context ¶ added in v0.3.0
func (validator *ValidatorTimeP) Context() *ValidatorContext
Context retrieves the context associated with the validator.
func (*ValidatorTimeP) EqualTo ¶ added in v0.3.0
func (validator *ValidatorTimeP) EqualTo(value time.Time, template ...string) *ValidatorTimeP
EqualTo validates that the time pointer is equal to the specified time value.
Usage example:
t1 := time.Now() t2 := t1 Is(v.TimeP(&t1).EqualTo(t2)).Valid() // Will return true.
func (*ValidatorTimeP) InSlice ¶ added in v0.3.0
func (validator *ValidatorTimeP) InSlice(slice []time.Time, template ...string) *ValidatorTimeP
InSlice validates that the time pointer is pointing to a time value present in the specified slice.
Usage example:
t := time.Now()
validTimes := []time.Time{t, time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)}
Is(v.TimeP(&t).InSlice(validTimes)).Valid() // Will return true.
func (*ValidatorTimeP) Nil ¶ added in v0.3.0
func (validator *ValidatorTimeP) Nil(template ...string) *ValidatorTimeP
Nil validates that the time pointer is nil.
Usage example:
var t *time.Time Is(v.TimeP(t).Nil()).Valid() // Will return true as t is nil.
func (*ValidatorTimeP) NilOrZero ¶ added in v0.3.0
func (validator *ValidatorTimeP) NilOrZero(template ...string) *ValidatorTimeP
NilOrZero validates that the time pointer is either nil or pointing to a zero time value.
Usage example:
var t *time.Time Is(v.TimeP(t).NilOrZero()).Valid() // Will return true as t is nil.
func (*ValidatorTimeP) Not ¶ added in v0.3.0
func (validator *ValidatorTimeP) Not() *ValidatorTimeP
Not negates the result of the next validator function in the chain.
Usage example:
t := time.Now() Is(v.TimeP(&t).Not().Zero()).Valid() // Will return false since t is not a zero time.
func (*ValidatorTimeP) Or ¶ added in v0.3.0
func (validator *ValidatorTimeP) Or() *ValidatorTimeP
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the time is before or equal to time.Now(). t := time.Now() isValid := v.Is(v.TimeP(&t).Nil().Or().BeforeOrEqualTo(time.Now())).Valid()
func (*ValidatorTimeP) Passing ¶ added in v0.3.0
func (validator *ValidatorTimeP) Passing(function func(v0 *time.Time) bool, template ...string) *ValidatorTimeP
Passing allows for custom validation function to be applied on the time pointer.
Usage example:
t := time.Now()
Is(v.TimeP(&t).Passing(func(v0 *time.Time) bool { return v0.After(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)) })).Valid() // Custom validation.
func (*ValidatorTimeP) Zero ¶ added in v0.3.0
func (validator *ValidatorTimeP) Zero(template ...string) *ValidatorTimeP
Zero validates that the time pointer is pointing to a zero time value.
Usage example:
var t *time.Time Is(v.TimeP(t).Zero()).Valid() // Will return true as t is nil and thus pointing to a zero time.
type ValidatorTyped ¶ added in v0.7.0
type ValidatorTyped[T any] struct { // contains filtered or unexported fields }
The Typed validator's type that keeps its validator context. T can be any Go type (pointer, struct, slice, map, etc.).
func Typed ¶ added in v0.7.0
func Typed[T any](value T, nameAndTitle ...string) *ValidatorTyped[T]
Receive a value of type T to validate.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A value_%N pattern is used as a name in the error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name phone_number will be humanized as Phone Number.
Example:
v.Is(v.Typed(user).Not().Nil())
func (*ValidatorTyped[T]) Context ¶ added in v0.7.0
func (validator *ValidatorTyped[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorTyped[T]) Nil ¶ added in v0.7.0
func (validator *ValidatorTyped[T]) Nil(template ...string) *ValidatorTyped[T]
Validate if a value is nil. Works for nil-able kinds: pointers, slices, maps, chans, funcs, and interfaces. For non-nil-able types, this will return false.
For example:
var s *string v.Is(v.Typed(s).Nil())
func (*ValidatorTyped[T]) Not ¶ added in v0.7.0
func (validator *ValidatorTyped[T]) Not() *ValidatorTyped[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because `Not()` inverts the boolean value associated with `EqualTo()`
v.Is(v.Typed("a").Not().EqualTo("a")).Valid()
func (*ValidatorTyped[T]) Or ¶ added in v0.7.0
func (validator *ValidatorTyped[T]) Or() *ValidatorTyped[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the string is equals "test".
input := "test"
isValid := v.Is(v.String(input).MinLength(5).Or().EqualTo("test")).Valid()
func (*ValidatorTyped[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorTyped[T]) Passing(function func(v T) bool, template ...string) *ValidatorTyped[T]
Validate if a value passes a custom function. The function receives a typed T value, enabling compile-time type safety.
For example:
type Status string
status := Status("running")
isValid := v.Is(
v.Typed(status).Passing(func(s Status) bool {
return s == "running" || s == "paused"
}),
).Valid()
type ValidatorUint ¶ added in v0.7.0
type ValidatorUint[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64] struct { // contains filtered or unexported fields }
The ValidatorUint provides functions for setting validation rules for a uint value types, or a custom type based on a uint, uint8, uint16, uint32, or uint64.
func Byte ¶
func Byte[T ~byte](value T, nameAndTitle ...string) *ValidatorUint[T]
Receives an byte value to validate.
The value can also be a custom byte type such as type Age byte.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint ¶ added in v0.7.0
func Uint[T ~uint](value T, nameAndTitle ...string) *ValidatorUint[T]
Receives an uint value to validate.
The value can also be a custom uint type such as type Age uint.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint8 ¶
func Uint8[T ~uint8](value T, nameAndTitle ...string) *ValidatorUint[T]
Receives an uint8 value to validate.
The value can also be a custom uint8 type such as type Age uint8.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint16 ¶
func Uint16[T ~uint16](value T, nameAndTitle ...string) *ValidatorUint[T]
Receives an uint16 value to validate.
The value can also be a custom uint16 type such as type Age uint16.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint32 ¶
func Uint32[T ~uint32](value T, nameAndTitle ...string) *ValidatorUint[T]
Receives an uint32 value to validate.
The value can also be a custom uint32 type such as type Age uint32.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint64 ¶
func Uint64[T ~uint64](value T, nameAndTitle ...string) *ValidatorUint[T]
Receives an uint64 value to validate.
The value can also be a custom uint64 type such as type Age uint64.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N“ pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorUint[T]) Between ¶ added in v0.7.0
func (validator *ValidatorUint[T]) Between(min T, max T, template ...string) *ValidatorUint[T]
Validate if a number is within a range (inclusive). For example:
Is(v.Uint(uint(3)).Between(2,6))
func (*ValidatorUint[T]) Context ¶ added in v0.7.0
func (validator *ValidatorUint[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorUint[T]) EqualTo ¶ added in v0.7.0
func (validator *ValidatorUint[T]) EqualTo(value T, template ...string) *ValidatorUint[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := uint(2) Is(v.Uint(quantity).EqualTo(2))
func (*ValidatorUint[T]) GreaterOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorUint[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorUint[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := uint(3) Is(v.Uint(quantity).GreaterOrEqualTo(3))
func (*ValidatorUint[T]) GreaterThan ¶ added in v0.7.0
func (validator *ValidatorUint[T]) GreaterThan(value T, template ...string) *ValidatorUint[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := uint(3) Is(v.Uint(quantity).GreaterThan(2))
func (*ValidatorUint[T]) InSlice ¶ added in v0.7.0
func (validator *ValidatorUint[T]) InSlice(slice []T, template ...string) *ValidatorUint[T]
Validate if a number is present in a numeric slice. For example:
quantity := uint(3)
validQuantities := []uint{1,3,5}
Is(v.Uint(quantity).InSlice(validQuantities))
func (*ValidatorUint[T]) LessOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorUint[T]) LessOrEqualTo(value T, template ...string) *ValidatorUint[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := uint(2) Is(v.Uint(quantity).LessOrEqualTo(2))
func (*ValidatorUint[T]) LessThan ¶ added in v0.7.0
func (validator *ValidatorUint[T]) LessThan(value T, template ...string) *ValidatorUint[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := uint(2) Is(v.Uint(quantity).LessThan(3))
func (*ValidatorUint[T]) Not ¶ added in v0.7.0
func (validator *ValidatorUint[T]) Not() *ValidatorUint[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function Is(v.Uint(0).Not().Zero()).Valid()
func (*ValidatorUint[T]) Or ¶ added in v0.7.0
func (validator *ValidatorUint[T]) Or() *ValidatorUint[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := uint(0) isValid := v.Is(v.Uint(input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorUint[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorUint[T]) Passing(function func(v T) bool, template ...string) *ValidatorUint[T]
Validate if a numeric value passes a custom function. For example:
quantity := uint(2)
Is(v.Uint(quantity).Passing((v uint) bool {
return v == getAllowedQuantity()
})
func (*ValidatorUint[T]) Zero ¶ added in v0.7.0
func (validator *ValidatorUint[T]) Zero(template ...string) *ValidatorUint[T]
Validate if a numeric value is zero.
For example:
Is(v.Uint(uint(0)).Zero())
type ValidatorUintP ¶ added in v0.7.0
type ValidatorUintP[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64] struct { // contains filtered or unexported fields }
The ValidatorUintP provides functions for setting validation rules for a uint pointer value types, or a custom type based on a uint, uint8, uint16, uint32, or uint64 pointer.
func ByteP ¶
func ByteP[T ~byte](value *T, nameAndTitle ...string) *ValidatorUintP[T]
Receives a byte pointer value to validate.
The value can also be a custom byte pointer type such as type Age *byte.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint8P ¶
func Uint8P[T ~uint8](value *T, nameAndTitle ...string) *ValidatorUintP[T]
Receives an uint8 pointer value to validate.
The value can also be a custom uint8 pointer type such as type Age *uint8.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint16P ¶
func Uint16P[T ~uint16](value *T, nameAndTitle ...string) *ValidatorUintP[T]
Receives an uint16 pointer value to validate.
The value can also be a custom uint16 pointer type such as type Age *uint16.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint32P ¶
func Uint32P[T ~uint32](value *T, nameAndTitle ...string) *ValidatorUintP[T]
Receives an uint32 pointer value to validate.
The value can also be a custom uint32 pointer type such as type Age *uint32.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func Uint64P ¶
func Uint64P[T ~uint64](value *T, nameAndTitle ...string) *ValidatorUintP[T]
Receives an uint64 pointer value to validate.
The value can also be a custom uint64 pointer type such as type Age *uint64.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func UintP ¶ added in v0.7.0
func UintP[T ~uint](value *T, nameAndTitle ...string) *ValidatorUintP[T]
Receives an uint pointer value to validate.
The value can also be a custom uint pointer type such as type Age *uint.
Optionally, the function can receive a name and title, in that order, to be displayed in the error messages. A `value_%N" pattern is used as a name in error messages if a name and title are not supplied; for example: value_0. When the name is provided but not the title, then the name is humanized to be used as the title as well; for example the name `phone_number` will be humanized as `Phone Number`
func (*ValidatorUintP[T]) Between ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Between(min T, max T, template ...string) *ValidatorUintP[T]
Validate if a number is within a range (inclusive). For example:
n := uint(3) Is(v.UintP(&n).Between(2,6))
func (*ValidatorUintP[T]) Context ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Context() *ValidatorContext
Return the context of the validator. The context is useful to create a custom validator by extending this validator.
func (*ValidatorUintP[T]) EqualTo ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) EqualTo(value T, template ...string) *ValidatorUintP[T]
Validate if a numeric value is equal to another. This function internally uses the golang `==` operator. For example:
quantity := uint(2) Is(v.UintP(&quantity).EqualTo(2))
func (*ValidatorUintP[T]) GreaterOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorUintP[T]
Validate if a numeric value is greater than or equal to another. This function internally uses the golang `>=` operator. For example:
quantity := uint(3) Is(v.UintP(&quantity).GreaterOrEqualTo(3))
func (*ValidatorUintP[T]) GreaterThan ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) GreaterThan(value T, template ...string) *ValidatorUintP[T]
Validate if a numeric value is greater than another. This function internally uses the golang `>` operator. For example:
quantity := uint(3) Is(v.UintP(&quantity).GreaterThan(2))
func (*ValidatorUintP[T]) InSlice ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) InSlice(slice []T, template ...string) *ValidatorUintP[T]
Validate if a number is present in a numeric slice. For example:
quantity := uint(3)
validQuantities := []uint{1,3,5}
Is(v.UintP(&quantity).InSlice(validQuantities))
func (*ValidatorUintP[T]) LessOrEqualTo ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) LessOrEqualTo(value T, template ...string) *ValidatorUintP[T]
Validate if a numeric value is less than or equal to another. This function internally uses the golang `<=` operator. For example:
quantity := uint(2) Is(v.UintP(&quantity).LessOrEqualTo(2))
func (*ValidatorUintP[T]) LessThan ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) LessThan(value T, template ...string) *ValidatorUintP[T]
Validate if a numeric value is less than another. This function internally uses the golang `<` operator. For example:
quantity := uint(2) Is(v.UintP(&quantity).LessThan(3))
func (*ValidatorUintP[T]) Nil ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Nil(template ...string) *ValidatorUintP[T]
Validate if a numeric pointer value is nil.
For example:
var n *uint Is(v.UintP(n).Nil())
func (*ValidatorUintP[T]) Not ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Not() *ValidatorUintP[T]
Invert the logical value associated with the next validator function. For example:
// It will return false because Not() inverts the boolean value associated with the Zero() function n := uint(0) Is(v.UintP(&n).Not().Zero()).Valid()
func (*ValidatorUintP[T]) Or ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Or() *ValidatorUintP[T]
Introduces a logical OR in the chain of validation conditions, affecting the evaluation order and priority of subsequent validators. A value passes the validation if it meets any one condition following the Or() call, adhering to a left-to-right evaluation. This mechanism allows for validating against multiple criteria where satisfying any single criterion is sufficient. Example:
// This validator will pass because the input is Zero. input := uint(0) isValid := v.Is(v.UintP(&input).GreaterThan(5).Or().Zero()).Valid()
func (*ValidatorUintP[T]) Passing ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Passing(function func(v *T) bool, template ...string) *ValidatorUintP[T]
Validate if a numeric value passes a custom function. For example:
quantity := uint(2)
Is(v.UintP(&quantity).Passing((v *uint) bool {
return *v == getAllowedQuantity()
})
func (*ValidatorUintP[T]) Zero ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) Zero(template ...string) *ValidatorUintP[T]
Validate if a numeric value is zero.
For example:
n := uint(0) Is(v.UintP(&n).Zero())
func (*ValidatorUintP[T]) ZeroOrNil ¶ added in v0.7.0
func (validator *ValidatorUintP[T]) ZeroOrNil(template ...string) *ValidatorUintP[T]
Validate if a numeric value is zero or nil.
For example:
n := uint(0) Is(v.UintP(&n).ZeroOrNil()) var n *uint Is(v.UintP(n).ZeroOrNil())
Source Files
¶
- constants.go
- error.go
- factory.go
- locale.go
- locale_de.go
- locale_en.go
- locale_es.go
- locale_hu.go
- util.go
- valgo.go
- validation.go
- validator.go
- validator_any.go
- validator_boolean.go
- validator_boolean_p.go
- validator_comparable.go
- validator_comparable_p.go
- validator_context.go
- validator_float.go
- validator_float_p.go
- validator_int.go
- validator_int_p.go
- validator_number.go
- validator_number_p.go
- validator_string.go
- validator_string_p.go
- validator_time.go
- validator_time_p.go
- validator_typed.go
- validator_uint.go
- validator_uint_p.go