Documentation ¶
Index ¶
- Constants
- Variables
- func Accepted(ctx *ValidatorCtx) (string, bool)
- func ActiveUrl(ctx *ValidatorCtx) (string, bool)
- func AfterDate(ctx *ValidatorCtx) (string, bool)
- func AfterOrEqualDate(ctx *ValidatorCtx) (string, bool)
- func Alpha(ctx *ValidatorCtx) (string, bool)
- func AlphaDash(ctx *ValidatorCtx) (string, bool)
- func AlphaNumeric(ctx *ValidatorCtx) (string, bool)
- func Ascii(ctx *ValidatorCtx) (string, bool)
- func Bail(ctx *ValidatorCtx) (string, bool)
- func BaseRegisterMessages(resolvers map[string]MessageResolver)
- func BeforeDate(ctx *ValidatorCtx) (string, bool)
- func BeforeOrEqualDate(ctx *ValidatorCtx) (string, bool)
- func Between(ctx *ValidatorCtx) (string, bool)
- func Boolean(ctx *ValidatorCtx) (string, bool)
- func Confirmed(ctx *ValidatorCtx) (string, bool)
- func CreateGoError(errors map[string]string) error
- func CreateJsonError(errors map[string]string) error
- func Date(ctx *ValidatorCtx) (string, bool)
- func DateFormat(ctx *ValidatorCtx) (string, bool)
- func Declined(ctx *ValidatorCtx) (string, bool)
- func Digits(ctx *ValidatorCtx) (string, bool)
- func DigitsBetween(ctx *ValidatorCtx) (string, bool)
- func Email(ctx *ValidatorCtx) (string, bool)
- func EndsWith(ctx *ValidatorCtx) (string, bool)
- func Extensions(ctx *ValidatorCtx) (string, bool)
- func Filled(ctx *ValidatorCtx) (string, bool)
- func GoValidate(ctx context.Context, languages []language.Tag, input any) error
- func Gt(ctx *ValidatorCtx) (string, bool)
- func Gte(ctx *ValidatorCtx) (string, bool)
- func HexColor(ctx *ValidatorCtx) (string, bool)
- func IP(ctx *ValidatorCtx) (string, bool)
- func IPV4(ctx *ValidatorCtx) (string, bool)
- func IPV6(ctx *ValidatorCtx) (string, bool)
- func In(ctx *ValidatorCtx) (string, bool)
- func JSON(ctx *ValidatorCtx) (string, bool)
- func JsonValidate(ctx context.Context, languages []language.Tag, input any) error
- func LogValidatorsWithoutMessages()
- func Lowercase(ctx *ValidatorCtx) (string, bool)
- func Lt(ctx *ValidatorCtx) (string, bool)
- func Lte(ctx *ValidatorCtx) (string, bool)
- func MacAddress(ctx *ValidatorCtx) (string, bool)
- func Max(ctx *ValidatorCtx) (string, bool)
- func MaxDigits(ctx *ValidatorCtx) (string, bool)
- func Mimes(ctx *ValidatorCtx) (string, bool)
- func Mimetypes(ctx *ValidatorCtx) (string, bool)
- func Min(ctx *ValidatorCtx) (string, bool)
- func MinDigits(ctx *ValidatorCtx) (string, bool)
- func NotIn(ctx *ValidatorCtx) (string, bool)
- func NotNil(ctx *ValidatorCtx) (string, bool)
- func NotRegex(ctx *ValidatorCtx) (string, bool)
- func Numeric(ctx *ValidatorCtx) (string, bool)
- func Regex(ctx *ValidatorCtx) (string, bool)
- func RegisterMessages(lang language.Tag, resolvers map[string]MessageResolver)
- func RegisterMessagesStrict(lang language.Tag, resolvers map[string]MessageResolver)
- func RegisterValidator(name string, validator ValidatorFn)
- func Required(ctx *ValidatorCtx) (string, bool)
- func Size(ctx *ValidatorCtx) (string, bool)
- func StartsWith(ctx *ValidatorCtx) (string, bool)
- func URL(ctx *ValidatorCtx) (string, bool)
- func Ulid(ctx *ValidatorCtx) (string, bool)
- func Uppercase(ctx *ValidatorCtx) (string, bool)
- func Uuid(ctx *ValidatorCtx) (string, bool)
- type BasicMessageResolver
- type ConvertStatus
- type CustomError
- type DigitsStatus
- type FieldErrors
- type FieldValidatorError
- type LaravelErrorObj
- type LaravelErrorObjEntry
- type LaravelValidationError
- type MessageHintResolver
- type MessageResolver
- type Mode
- type Needle
- func (n *Needle) Date() (time.Time, ConvertStatus)
- func (n *Needle) Float64() (float64, bool)
- func (n *Needle) HasLen() bool
- func (n *Needle) HasValue() bool
- func (n *Needle) Int64() (int64, bool)
- func (n *Needle) IsFloat() bool
- func (n *Needle) IsInt() bool
- func (n *Needle) IsKind(kinds ...reflect.Kind) bool
- func (n *Needle) IsList() bool
- func (n *Needle) IsNumeric() bool
- func (n *Needle) IsUint() bool
- func (n *Needle) Kind() reflect.Kind
- func (n *Needle) String() (string, ConvertStatus)
- func (n *Needle) StringLike() (string, ConvertStatus)
- func (n *Needle) UnwrapPointer() bool
- type SimpleStackElement
- type SizeCompareStatus
- type Stack
- func (s Stack) AppendField(field reflect.StructField, parent *reflect.Value, parentType reflect.Type) Stack
- func (s Stack) AppendIndex(index int, parent *reflect.Value, parentType reflect.Type) Stack
- func (s Stack) LooslyEqualsWithRule(key string, rule string) bool
- func (s Stack) ToPaths() (golang, json string)
- type StackElement
- type StackKind
- type ValidationError
- type Validator
- func (v *Validator) CustomValidationRule(ruleName string, stack Stack) MessageResolver
- func (v *Validator) CustomValidationRules() []CustomError
- func (v *Validator) Error() error
- func (v *Validator) ErrorMessage(ruleName string, resolvers map[string]MessageResolver, hint string, ...) string
- func (v *Validator) ErrorMessageTemplate(ruleName string, resolvers map[string]MessageResolver, hint string, ...) string
- func (v *Validator) List(stack Stack, value reflect.Value, validateInner []validationRule)
- func (v *Validator) Nil(stack Stack, valueType reflect.Type)
- func (v *Validator) NilStruct(stack Stack, valueType reflect.Type)
- func (v *Validator) Struct(stack Stack, value reflect.Value)
- func (v *Validator) Validate(stack Stack, value *reflect.Value, valueType reflect.Type, ...)
- type ValidatorCtx
- func (ctx *ValidatorCtx) Bail()
- func (ctx *ValidatorCtx) BailStatus() bool
- func (ctx *ValidatorCtx) Context() context.Context
- func (ctx *ValidatorCtx) Date() (time.Time, ConvertStatus)
- func (ctx *ValidatorCtx) DateFromArgs(argIndex int) (time.Time, bool)
- func (ctx *ValidatorCtx) Field(key string) *Needle
- func (ctx *ValidatorCtx) GetState(key string) (any, bool)
- func (ctx *ValidatorCtx) ObjectFieldName() string
- func (ctx *ValidatorCtx) SetState(key string, value any)
- func (ctx *ValidatorCtx) Stack() Stack
- func (ctx *ValidatorCtx) UnBail()
- type ValidatorCtxState
- type ValidatorFn
Constants ¶
const (
ParsedDateKey = "parsed-date" // Stores a custom parsed date, this is used by the date_format validator to store the results so they can be used by other validators
)
Variables ¶
var FallbackMessageResolver = BasicMessageResolver("The :attribute field is invalid")
Functions ¶
func Accepted ¶
func Accepted(ctx *ValidatorCtx) (string, bool)
func ActiveUrl ¶
func ActiveUrl(ctx *ValidatorCtx) (string, bool)
func AfterDate ¶
func AfterDate(ctx *ValidatorCtx) (string, bool)
func AfterOrEqualDate ¶
func AfterOrEqualDate(ctx *ValidatorCtx) (string, bool)
func Alpha ¶
func Alpha(ctx *ValidatorCtx) (string, bool)
func AlphaDash ¶
func AlphaDash(ctx *ValidatorCtx) (string, bool)
func AlphaNumeric ¶
func AlphaNumeric(ctx *ValidatorCtx) (string, bool)
func Ascii ¶
func Ascii(ctx *ValidatorCtx) (string, bool)
func Bail ¶
func Bail(ctx *ValidatorCtx) (string, bool)
func BaseRegisterMessages ¶
func BaseRegisterMessages(resolvers map[string]MessageResolver)
func BeforeDate ¶
func BeforeDate(ctx *ValidatorCtx) (string, bool)
func BeforeOrEqualDate ¶
func BeforeOrEqualDate(ctx *ValidatorCtx) (string, bool)
func Between ¶
func Between(ctx *ValidatorCtx) (string, bool)
func Boolean ¶
func Boolean(ctx *ValidatorCtx) (string, bool)
func Confirmed ¶
func Confirmed(ctx *ValidatorCtx) (string, bool)
func CreateGoError ¶ added in v1.0.3
func CreateJsonError ¶ added in v1.0.3
func Date ¶
func Date(ctx *ValidatorCtx) (string, bool)
func DateFormat ¶
func DateFormat(ctx *ValidatorCtx) (string, bool)
func Declined ¶
func Declined(ctx *ValidatorCtx) (string, bool)
func Digits ¶
func Digits(ctx *ValidatorCtx) (string, bool)
func DigitsBetween ¶
func DigitsBetween(ctx *ValidatorCtx) (string, bool)
func Email ¶
func Email(ctx *ValidatorCtx) (string, bool)
func EndsWith ¶
func EndsWith(ctx *ValidatorCtx) (string, bool)
func Extensions ¶
func Extensions(ctx *ValidatorCtx) (string, bool)
func Filled ¶
func Filled(ctx *ValidatorCtx) (string, bool)
func GoValidate ¶
GoValidate should be used to validate something within a go codebase with validation errors that apply to the go codebase.
If an error is returned the type should be of *ValidationError.
Ctx can be set to nil, default value will be context.Background(). Languages can be set to nil, default value will be []language.Tag{language.English}.
func Gt ¶ added in v1.0.1
func Gt(ctx *ValidatorCtx) (string, bool)
func Gte ¶ added in v1.0.1
func Gte(ctx *ValidatorCtx) (string, bool)
func HexColor ¶
func HexColor(ctx *ValidatorCtx) (string, bool)
func IP ¶
func IP(ctx *ValidatorCtx) (string, bool)
func IPV4 ¶
func IPV4(ctx *ValidatorCtx) (string, bool)
func IPV6 ¶
func IPV6(ctx *ValidatorCtx) (string, bool)
func In ¶
func In(ctx *ValidatorCtx) (string, bool)
func JSON ¶
func JSON(ctx *ValidatorCtx) (string, bool)
func JsonValidate ¶
JsonValidate should be used to validate a json parsed message, errors returned will have a json paths.
If an error is returned the type should be of *ValidationError.
Ctx can be set to nil, default value will be context.Background(). Languages can be set to nil, default value will be []language.Tag{language.English}.
func LogValidatorsWithoutMessages ¶
func LogValidatorsWithoutMessages()
func Lowercase ¶
func Lowercase(ctx *ValidatorCtx) (string, bool)
func Lt ¶ added in v1.0.1
func Lt(ctx *ValidatorCtx) (string, bool)
func Lte ¶ added in v1.0.1
func Lte(ctx *ValidatorCtx) (string, bool)
func MacAddress ¶
func MacAddress(ctx *ValidatorCtx) (string, bool)
func Max ¶
func Max(ctx *ValidatorCtx) (string, bool)
func MaxDigits ¶
func MaxDigits(ctx *ValidatorCtx) (string, bool)
func Mimes ¶
func Mimes(ctx *ValidatorCtx) (string, bool)
func Mimetypes ¶
func Mimetypes(ctx *ValidatorCtx) (string, bool)
func Min ¶
func Min(ctx *ValidatorCtx) (string, bool)
func MinDigits ¶
func MinDigits(ctx *ValidatorCtx) (string, bool)
func NotIn ¶
func NotIn(ctx *ValidatorCtx) (string, bool)
func NotNil ¶
func NotNil(ctx *ValidatorCtx) (string, bool)
func NotRegex ¶
func NotRegex(ctx *ValidatorCtx) (string, bool)
func Numeric ¶
func Numeric(ctx *ValidatorCtx) (string, bool)
func Regex ¶
func Regex(ctx *ValidatorCtx) (string, bool)
func RegisterMessages ¶
func RegisterMessages(lang language.Tag, resolvers map[string]MessageResolver)
RegisterMessages registers messages for a validator
func RegisterMessagesStrict ¶
func RegisterMessagesStrict(lang language.Tag, resolvers map[string]MessageResolver)
RegisterMessagesStrict registers messages for a validator Compared to RegisterMessages this function does not try to match the language with the base language For example if you register a message for "en-GB" it will only be used for "en-GB" and not for "en"
func RegisterValidator ¶
func RegisterValidator(name string, validator ValidatorFn)
RegisterValidator registers a new validator function
func Required ¶
func Required(ctx *ValidatorCtx) (string, bool)
func Size ¶
func Size(ctx *ValidatorCtx) (string, bool)
func StartsWith ¶
func StartsWith(ctx *ValidatorCtx) (string, bool)
func URL ¶
func URL(ctx *ValidatorCtx) (string, bool)
func Ulid ¶
func Ulid(ctx *ValidatorCtx) (string, bool)
func Uppercase ¶
func Uppercase(ctx *ValidatorCtx) (string, bool)
func Uuid ¶
func Uuid(ctx *ValidatorCtx) (string, bool)
Types ¶
type BasicMessageResolver ¶
type BasicMessageResolver string
BasicMessageResolver is a simple message resolver that always returns the same message, no matter the language or hint
func (BasicMessageResolver) Resolve ¶
func (d BasicMessageResolver) Resolve(hint string) string
type ConvertStatus ¶
type ConvertStatus uint8
const ( ConverstionOk ConvertStatus = iota InvalidType // Usually means you should return ("invalid_type", false) Invalid // Usually means you should return ("invalid", false) ValueNil // Usually means you should return ("", true) )
func (ConvertStatus) Oke ¶
func (s ConvertStatus) Oke() bool
func (ConvertStatus) Response ¶
func (s ConvertStatus) Response() (string, bool)
type CustomError ¶
type CustomError struct { Key string Resolver MessageResolver }
type DigitsStatus ¶
type DigitsStatus uint8
const ( DigitStatusValid DigitsStatus = iota DigitStatusInvalidType DigitStatusNil DigitStatusStringWithoutDigits )
func (DigitsStatus) Oke ¶
func (s DigitsStatus) Oke() bool
func (DigitsStatus) Response ¶
func (s DigitsStatus) Response() (string, bool)
type FieldErrors ¶
type FieldErrors struct { Path string `json:"path"` Errors []FieldValidatorError `json:"errors"` }
type FieldValidatorError ¶
type LaravelErrorObj ¶
type LaravelErrorObj []LaravelErrorObjEntry
func (LaravelErrorObj) MarshalJSON ¶
func (obj LaravelErrorObj) MarshalJSON() ([]byte, error)
type LaravelErrorObjEntry ¶
type LaravelValidationError ¶
type LaravelValidationError struct { Errors LaravelErrorObj `json:"errors"` Message string `json:"message"` }
func (*LaravelValidationError) Error ¶ added in v1.0.4
func (v *LaravelValidationError) Error() string
type MessageHintResolver ¶
func (MessageHintResolver) Resolve ¶
func (d MessageHintResolver) Resolve(hint string) string
type MessageResolver ¶
type Needle ¶
type Needle struct { Type reflect.Type // Value might be nil if we are validating a pointer to a nil struct. // Also running ctx.UnwrapPointer() might cause the value to be unset Value *reflect.Value }
func (*Needle) Date ¶
func (n *Needle) Date() (time.Time, ConvertStatus)
Date tries to convert the value to a time.Time
func (*Needle) IsKind ¶
IsKinds asserts the .Kind method for one of the input values If it matches one it returns true
func (*Needle) IsNumeric ¶
IsNumeric returns true if the type kind is a int[..] , uint[..] or float[..]
func (*Needle) Kind ¶
Kind returns the kind of the value if available an falls back to the kind of the type
func (*Needle) String ¶
func (n *Needle) String() (string, ConvertStatus)
String is a wrapper around (*ValidatorCtx).value.String() This one checks the following extra things and if one of them does not match returns ("", false) 1. The value is set 2. The value is a string
Example: ```
str, status := ctx.String() if !status.Oke() { return status.Response() } fmt.Println(str)
```
func (*Needle) StringLike ¶
func (n *Needle) StringLike() (string, ConvertStatus)
StringLike returns all values that can be interpreted as a string
Supported: - string - []byte - []rune - rune - byte
Example: ```
str, status := ctx.StringLike() if !status.Oke() { return status.Response() } fmt.Println(str)
```
func (*Needle) UnwrapPointer ¶
UnwrapPointer unwraps the pointer
type SimpleStackElement ¶
type SizeCompareStatus ¶ added in v1.0.1
type SizeCompareStatus uint8
const ( SizeCompareStatusEq SizeCompareStatus = iota SizeCompareStatusLt SizeCompareStatusGt )
type Stack ¶
type Stack []StackElement
func (Stack) AppendField ¶
func (Stack) AppendIndex ¶
func (Stack) LooslyEqualsWithRule ¶
LooslyEquals checks if the stack is equal to the given key The key might ignore the index of the list elements and only check the object fields
type StackElement ¶
type ValidationError ¶
type ValidationError struct { Mode Mode `json:"mode"` Language []string `json:"language"` Errors []FieldErrors `json:"errors"` }
func (*ValidationError) Error ¶
func (v *ValidationError) Error() string
func (*ValidationError) Prefix ¶ added in v1.0.4
func (e *ValidationError) Prefix(prefix string)
Prefix prefixes all paths in the error with the given prefix
func (*ValidationError) ToLaravelError ¶
func (e *ValidationError) ToLaravelError() *LaravelValidationError
ToLaravelError converts a ValidationError to Laravel like validation error
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func (*Validator) CustomValidationRule ¶
func (v *Validator) CustomValidationRule(ruleName string, stack Stack) MessageResolver
func (*Validator) CustomValidationRules ¶
func (v *Validator) CustomValidationRules() []CustomError
func (*Validator) ErrorMessage ¶
func (v *Validator) ErrorMessage(ruleName string, resolvers map[string]MessageResolver, hint string, ctx *ValidatorCtx) string
func (*Validator) ErrorMessageTemplate ¶
type ValidatorCtx ¶
type ValidatorCtx struct { // Needle contains the field under validation Needle // Args are the arguments that were passed to the validator Args []string // contains filtered or unexported fields }
func (*ValidatorCtx) Bail ¶
func (ctx *ValidatorCtx) Bail()
Bail indicates that the validator should stop after the first error for this field
func (*ValidatorCtx) BailStatus ¶
func (ctx *ValidatorCtx) BailStatus() bool
BailStatus returns the current bail status, if true the validator will stop after the first error
func (*ValidatorCtx) Context ¶
func (ctx *ValidatorCtx) Context() context.Context
NewValidatorCtx returns the underlying context of the validator
func (*ValidatorCtx) Date ¶
func (ctx *ValidatorCtx) Date() (time.Time, ConvertStatus)
Date tries to convert the value to a time.Time
func (*ValidatorCtx) DateFromArgs ¶
func (ctx *ValidatorCtx) DateFromArgs(argIndex int) (time.Time, bool)
func (*ValidatorCtx) Field ¶
func (ctx *ValidatorCtx) Field(key string) *Needle
field tries to return a value from the input based on the requested path There are 2 main ways of using this function
1. Absolute path:
- "foo.1.bar" = Get from the input (struct) the field "foo", then when it's a list like get the element at index 1 from the list, then get the field "bar" from the struct
- "" = Get the source input
2. Relative path:
- ".foo" = Get relative to the currently processed struct the field "foo"
- ".1" = Get relative to the currently processed list the element at index 1
- "." = Get the currently processed struct
- "..foo" = Get the parent of the currentl`y processed struct and then get the field "foo" from it
If nil is returned the field does not exist or path is invalid If a needle with only a reflect.Type is returned the path exists but the value is nil
func (*ValidatorCtx) GetState ¶
func (ctx *ValidatorCtx) GetState(key string) (any, bool)
GetState gets a value from the state
func (*ValidatorCtx) ObjectFieldName ¶
func (ctx *ValidatorCtx) ObjectFieldName() string
ObjectFieldName returns the name of the currently processed field in the object If the currently processed field is not an object field it will return an empty string
func (*ValidatorCtx) SetState ¶
func (ctx *ValidatorCtx) SetState(key string, value any)
SetState sets a value in the state
func (*ValidatorCtx) Stack ¶
func (ctx *ValidatorCtx) Stack() Stack
Stack returns the path to the currently processed field !!DO NOT MODIFY THE STACK!!, it will break the validator and cause panics
func (*ValidatorCtx) UnBail ¶
func (ctx *ValidatorCtx) UnBail()
UnBail indicates that the validator should continue after the first error for this field
type ValidatorCtxState ¶
type ValidatorCtxState struct {
// contains filtered or unexported fields
}
type ValidatorFn ¶
type ValidatorFn func(ctx *ValidatorCtx) (string, bool)