Documentation ¶
Index ¶
- Variables
- type Validator
- func (v *Validator) AddCustom(s interface{}, f ...func(interface{}) string) error
- func (v *Validator) AddCustomFloat64Rule(name string, validatorFunc func(string, float64) string)
- func (v *Validator) AddCustomInt64Rule(name string, validatorFunc func(string, int64) string)
- func (v *Validator) AddCustomStringRule(name string, validatorFunc func(string, string) string)
- func (v *Validator) Register(structs ...interface{}) errordeprecated
- func (v *Validator) Violation(s interface{}) (string, error)
- func (v *Validator) Violations(s interface{}) ([]string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotRegistered = errors.New("structs must be registered before validating")
Deprecated: ErrNotRegistered is deprecated.
var ErrNotStruct = errors.New("only structs can be validated")
ErrNotStruct is encountered when an attempt is made to validate a type that is not a struct is made.
Functions ¶
This section is empty.
Types ¶
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator .
func (*Validator) AddCustomFloat64Rule ¶ added in v1.2.0
AddCustomFloat64Rule adds custom validation tag for float64.
func (*Validator) AddCustomInt64Rule ¶ added in v1.2.0
AddCustomInt64Rule adds custom validation tag for int64.
func (*Validator) AddCustomStringRule ¶ added in v1.2.0
AddCustomStringRule adds custom validation tag for string.
func (*Validator) Violation ¶
Violation checks the struct s against all constraints and custom validation functions, if any. It returns an violation if the struct fails validation. If the type being validated is not a struct, ErrNotStruct will be returned.
Example ¶
package main import ( "fmt" "github.com/twharmon/govalid" ) func main() { v := govalid.New() type User struct { Name string `govalid:"req|min:2|max:32"` Role string `govalid:"req|in:user,editor,admin"` Age int `govalid:"req|min:18"` } user := User{ Name: "foo", Role: "super_admin", Age: 10, } vio, _ := v.Violation(&user) fmt.Println(vio) }
Output: Role must be in [user editor admin]
func (*Validator) Violations ¶
Violations checks the struct s against all constraints and custom validation functions, if any. It returns a slice of violations if the struct fails validation. If the type being validated is not a struct, ErrNotStruct will be returned.