Documentation ¶
Index ¶
- Variables
- type ErrorDetail
- type ErrorReason
- type ErrorReport
- type ValidationFunc
- type ValidationResult
- func DefaultValidator(param string, v reflect.Value) (ValidationResult, error)
- func EmailValidator(param string, v reflect.Value) (ValidationResult, error)
- func EnumValidator(param string, v reflect.Value) (ValidationResult, error)
- func MaxLenValidator(param string, v reflect.Value) (ValidationResult, error)
- func MaxValidator(param string, v reflect.Value) (ValidationResult, error)
- func MinLenValidator(param string, v reflect.Value) (ValidationResult, error)
- func MinValidator(param string, v reflect.Value) (ValidationResult, error)
- func RegExpValidator(param string, v reflect.Value) (ValidationResult, error)
- func ReqValidator(param string, v reflect.Value) (ValidationResult, error)
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyValidationName = errors.New("validator name is required")
ErrEmptyValidationName means invalid validator name error.
var ErrInvalidConfigValue = errors.New("invalid configuration value")
ErrInvalidConfigValue means config value can't accept by validator.
var ErrUnsupportedValue = errors.New("unsupported type")
ErrUnsupportedValue means golidator is not support type of passed value.
var ErrValidateUnsupportedType = errors.New("unsupported field type")
ErrValidateUnsupportedType means validator is not support field type.
Functions ¶
This section is empty.
Types ¶
type ErrorDetail ¶
type ErrorDetail struct { ParentFieldName string `json:"-"` Current reflect.Value `json:"-"` Value reflect.Value `json:"-"` Field reflect.StructField `json:"-"` FieldName string `json:"fieldName"` // e.g. address , person.name ReasonList []*ErrorReason `json:"reasonList"` }
ErrorDetail provides error about 1 field.
type ErrorReason ¶
type ErrorReason struct { Type string `json:"type"` // e.g. req , min, enum Config string `json:"config,omitempty"` // e.g. 1, manual|auto, ^[^@]+@gmail.com$ }
ErrorReason contains why validation is failed?
type ErrorReport ¶
type ErrorReport struct { Root reflect.Value `json:"-"` Type string `json:"type"` // fixed value. https://github.com/favclip/golidator Details []*ErrorDetail `json:"details"` }
ErrorReport provides detail of error informations.
func (*ErrorReport) Error ¶
func (report *ErrorReport) Error() string
type ValidationFunc ¶
type ValidationFunc func(param string, value reflect.Value) (ValidationResult, error)
ValidationFunc is validation function itself.
type ValidationResult ¶
type ValidationResult int
ValidationResult provides result of validation.
const ( // ValidationNG means validation is failure. ValidationNG ValidationResult = iota // ValidationOK means validation is succeed. ValidationOK )
func DefaultValidator ¶
func DefaultValidator(param string, v reflect.Value) (ValidationResult, error)
DefaultValidator set value when value is empty.
func EmailValidator ¶
func EmailValidator(param string, v reflect.Value) (ValidationResult, error)
EmailValidator check value that must be email address format.
func EnumValidator ¶
func EnumValidator(param string, v reflect.Value) (ValidationResult, error)
EnumValidator check value that must contains in config values.
func MaxLenValidator ¶
func MaxLenValidator(param string, v reflect.Value) (ValidationResult, error)
MaxLenValidator check value length that must less or equal than config value.
func MaxValidator ¶
func MaxValidator(param string, v reflect.Value) (ValidationResult, error)
MaxValidator check value that must less or equal than config value.
func MinLenValidator ¶
func MinLenValidator(param string, v reflect.Value) (ValidationResult, error)
MinLenValidator check value length that must greater or equal than config value.
func MinValidator ¶
func MinValidator(param string, v reflect.Value) (ValidationResult, error)
MinValidator check value that must greater or equal than config value.
func RegExpValidator ¶
func RegExpValidator(param string, v reflect.Value) (ValidationResult, error)
RegExpValidator check value that must valid about config regexp pattern.
func ReqValidator ¶
func ReqValidator(param string, v reflect.Value) (ValidationResult, error)
ReqValidator check value that must not be empty.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is holder of validation information.
func NewValidator ¶
func NewValidator() *Validator
NewValidator create and setup new Validator.
Example ¶
v := NewValidator() { obj := &struct { Value string `validate:"req"` }{} err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Value int `validate:"d=10"` }{} v.Validate(obj) fmt.Println("value:", obj.Value) } { obj := &struct { Value int `validate:"min=10"` }{ Value: 0, } err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Value int `validate:"max=10"` }{ Value: 100, } err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Value string `validate:"minLen=3"` }{ Value: "ab", } err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Value string `validate:"maxLen=3"` }{ Value: "abcd", } err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Value string `validate:"email"` }{ Value: "foo@bar@buzz", } err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Value string `validate:"enum=A|B"` }{ Value: "C", } err := v.Validate(obj) fmt.Println(err.Error()) } { obj := &struct { Min int `validate:"min=1"` Max int `validate:"max=10"` }{ Min: 0, Max: 11, } err := v.Validate(obj) fmt.Println(err.Error()) }
Output: invalid. #1 Value: req actual: '' value: 10 invalid. #1 Value: min=10 actual: 0 invalid. #1 Value: max=10 actual: 100 invalid. #1 Value: minLen=3 actual: 'ab' invalid. #1 Value: maxLen=3 actual: 'abcd' invalid. #1 Value: email actual: 'foo@bar@buzz' invalid. #1 Value: enum=A|B actual: 'C' invalid. #1 Min: min=1 actual: 0, #2 Max: max=10 actual: 11
func (*Validator) SetValidationFunc ¶
func (vl *Validator) SetValidationFunc(name string, vf ValidationFunc)
SetValidationFunc is setup tag name with ValidationFunc.
Example ¶
v := &Validator{} v.SetTag("validate") v.SetValidationFunc("req", func(param string, val reflect.Value) (ValidationResult, error) { if str := val.String(); str == "" { return ValidationNG, nil } return ValidationOK, nil }) obj := &struct { Value string `validate:"req"` }{} err := v.Validate(obj) fmt.Println(err.Error())
Output: invalid. #1 Value: req actual: ''