Documentation ¶
Index ¶
- func ValidateStruct(s interface{}) error
- func ValidateStructContext(ctx context.Context, s interface{}) error
- func ValidateVar(s interface{}, rawTag string) error
- func ValidateVarContext(ctx context.Context, s interface{}, rawTag string) error
- type Adapter
- type Error
- type Errors
- type Field
- type Func
- type FuncMap
- type FuncOption
- type Option
- type ParentField
- type Tag
- type Validator
- func (v *Validator) Apply(opts ...Option)
- func (v *Validator) ValidateStruct(s interface{}) error
- func (v *Validator) ValidateStructContext(ctx context.Context, s interface{}) error
- func (v *Validator) ValidateVar(s interface{}, rawTag string) error
- func (v *Validator) ValidateVarContext(ctx context.Context, s interface{}, rawTag string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateStruct ¶
func ValidateStruct(s interface{}) error
ValidateStruct validates a struct that uses the struct field's tag using default validator.
Example (Or) ¶
type user struct { ID string `valid:"or(alpha|numeric)"` } v := validator.New() err := v.ValidateStruct(&user{ ID: "abc", }) fmt.Println(err) err = v.ValidateStruct(&user{ ID: "123", }) fmt.Println(err) err = v.ValidateStruct(&user{ ID: "abc123", }) fmt.Println(err)
Output: <nil> <nil> ID: 'abc123' does validate as 'or(alpha|numeric)'
Example (SetFunc) ¶
type content struct { Type string `valid:"contentType(image/jpeg|image/png|image/gif)"` } v := validator.New( validator.WithFunc("contentType", func(_ context.Context, f validator.Field, opt validator.FuncOption) (bool, error) { v := f.Value().String() for _, param := range opt.TagParams { if v == param { return true, nil } } return false, nil }), ) err := v.ValidateStruct(&content{ Type: "image/jpeg", }) fmt.Println(err) err = v.ValidateStruct(&content{ Type: "image/bmp", }) fmt.Println(err)
Output: <nil> Type: 'image/bmp' does validate as 'contentType(image/jpeg|image/png|image/gif)'
Example (Simple) ¶
type user struct { Name string `valid:"required,alphanum"` Age uint `valid:"required,len(0|116)"` Email string `valid:"optional,email"` } v := validator.New() err := v.ValidateStruct(&user{ Name: "gopher", Age: 9, Email: "", }) fmt.Println(err) err = v.ValidateStruct(&user{ Name: "_", Age: 200, Email: "invalid", }) fmt.Println(err)
Output: <nil> Name: '_' does validate as 'alphanum';Age: '200' does validate as 'len(0|116)';Email: 'invalid' does validate as 'email'
func ValidateStructContext ¶
ValidateStructContext validates a struct that uses the struct field's tag using default validator. Pass context to each validating functions.
func ValidateVar ¶
ValidateVar validates a value using default validator.
Types ¶
type Error ¶
type Error interface { // Field returns a validating field. Field() Field // Tag returns a validation tag. Tag() Tag // Error returns an error message string. Error() string }
Error is an interface that represents a validation error
type Errors ¶
type Errors []Error
Errors represents validation errors
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a value.
func (Field) ShortString ¶
ShortString returns a string with 32 characters or more omitted.
type FuncOption ¶
type FuncOption struct { // TagParams represents the validation tag parameters. // e.g. len(1|2) -> []string{"1", "2"} TagParams []string // contains filtered or unexported fields }
FuncOption is the argument of validating function.
type Option ¶
type Option func(v *Validator)
func WithAdapters ¶
WithAdapters is a validator option that sets validator function adapters.
func WithFuncMap ¶
WithFuncMap is a validator option that sets validating functions.
func WithSuppressErrorFieldValue ¶
func WithSuppressErrorFieldValue() Option
WithSuppressErrorFieldValue is a validator option that enables suppress validating field value by error. If enabled this option, the field value always replaces `The value`.
func WithTagKey ¶
WithTagKey is a validator option that sets the key in the struct field's tag.
type ParentField ¶
type ParentField struct {
// contains filtered or unexported fields
}
ParentField represents a parent of Field.
func (ParentField) Interface ¶
func (f ParentField) Interface() interface{}
Interface returns an interface{}
type Tag ¶
type Tag struct {
// contains filtered or unexported fields
}
Tag represents the validation tag in struct field's tag.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is a validator that validates each fields using struct field's tag.
func DefaultValidator ¶
func DefaultValidator() *Validator
DefaultValidator returns a default validator.
func (*Validator) ValidateStruct ¶
ValidateStruct validates a struct that uses the struct field's tag.
func (*Validator) ValidateStructContext ¶
ValidateStructContext validates a struct that uses the struct field's tag. Pass context to each validating functions.
func (*Validator) ValidateVar ¶
ValidateVar validates a value.