Documentation ¶
Index ¶
- Variables
- func DefaultError(f reflect.StructField) error
- func EmailError(f reflect.StructField, actual string) error
- func EmptyParamError(in string, f reflect.StructField) error
- func EnumError(f reflect.StructField, actual interface{}, enum []string) error
- func MaxError(f reflect.StructField, actual, max interface{}) error
- func MaxLenError(f reflect.StructField, actual, max interface{}) error
- func MinError(f reflect.StructField, actual, min interface{}) error
- func MinLenError(f reflect.StructField, actual, min interface{}) error
- func ParamParseError(in string, f reflect.StructField, expected string) error
- func ReqError(f reflect.StructField, actual interface{}) error
- func UnsupportedTypeError(in string, f reflect.StructField) error
- type DefaultErrorOption
- type EmailErrorOption
- type EmptyParamErrorOption
- type EnumErrorOption
- type MaxErrorOption
- type MaxLenErrorOption
- type MinErrorOption
- type MinLenErrorOption
- type ParamParseErrorOption
- type ReqErrorOption
- type Target
- type UnsupportedTypeErrorOption
- type ValidationFunc
- func DefaultFactory(e *DefaultErrorOption) ValidationFunc
- func EmailFactory(e *EmailErrorOption) ValidationFunc
- func EnumFactory(e *EnumErrorOption) ValidationFunc
- func MaxFactory(e *MaxErrorOption) ValidationFunc
- func MaxLenFactory(e *MaxLenErrorOption) ValidationFunc
- func MinFactory(e *MinErrorOption) ValidationFunc
- func MinLenFactory(e *MinLenErrorOption) ValidationFunc
- func ReqFactory(e *ReqErrorOption) ValidationFunc
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedValue is error of unsupported value type ErrUnsupportedValue = errors.New("unsupported value") )
Functions ¶
func DefaultError ¶
func DefaultError(f reflect.StructField) error
DefaultError returns an error from "d" validator.
func EmailError ¶
func EmailError(f reflect.StructField, actual string) error
EmailError returns an error from "email" validator.
func EmptyParamError ¶
func EmptyParamError(in string, f reflect.StructField) error
EmptyParamError returns an error from empty param.
func EnumError ¶
func EnumError(f reflect.StructField, actual interface{}, enum []string) error
EnumError returns an error from "enum" validator.
func MaxError ¶
func MaxError(f reflect.StructField, actual, max interface{}) error
MaxError returns an error from "max" validator.
func MaxLenError ¶
func MaxLenError(f reflect.StructField, actual, max interface{}) error
MaxLenError returns an error from "maxLen" validator.
func MinError ¶
func MinError(f reflect.StructField, actual, min interface{}) error
MinError returns an error from "min" validator.
func MinLenError ¶
func MinLenError(f reflect.StructField, actual, min interface{}) error
MinLenError returns an error from "minLen" validator.
func ParamParseError ¶
func ParamParseError(in string, f reflect.StructField, expected string) error
ParamParseError returns an error from parsing param.
func ReqError ¶
func ReqError(f reflect.StructField, actual interface{}) error
ReqError returns an error from "req" validator.
func UnsupportedTypeError ¶
func UnsupportedTypeError(in string, f reflect.StructField) error
UnsupportedTypeError returns an error from unsupported type.
Types ¶
type DefaultErrorOption ¶
type DefaultErrorOption struct { *UnsupportedTypeErrorOption *ParamParseErrorOption DefaultError func(f reflect.StructField) error }
DefaultErrorOption is a factory of error caused by "d" validator.
type EmailErrorOption ¶
type EmailErrorOption struct { *UnsupportedTypeErrorOption EmailError func(f reflect.StructField, actual string) error }
EmailErrorOption is a factory of error caused by "email" validator.
type EmptyParamErrorOption ¶
type EmptyParamErrorOption struct {
EmptyParamError func(in string, f reflect.StructField) error
}
EmptyParamErrorOption is a factory of error caused by empty parameter.
type EnumErrorOption ¶
type EnumErrorOption struct { *UnsupportedTypeErrorOption *EmptyParamErrorOption EnumError func(f reflect.StructField, actual interface{}, enum []string) error }
EnumErrorOption is a factory of error caused by "enum" validator.
type MaxErrorOption ¶
type MaxErrorOption struct { *UnsupportedTypeErrorOption *ParamParseErrorOption MaxError func(f reflect.StructField, actual, max interface{}) error }
MaxErrorOption is a factory of error caused by "max" validator.
type MaxLenErrorOption ¶
type MaxLenErrorOption struct { *UnsupportedTypeErrorOption *ParamParseErrorOption MaxLenError func(f reflect.StructField, actual, max interface{}) error }
MaxLenErrorOption is a factory of error caused by "maxLen" validator.
type MinErrorOption ¶
type MinErrorOption struct { *UnsupportedTypeErrorOption *ParamParseErrorOption MinError func(f reflect.StructField, actual, min interface{}) error }
MinErrorOption is a factory of error caused by "min" validator.
type MinLenErrorOption ¶
type MinLenErrorOption struct { *UnsupportedTypeErrorOption *ParamParseErrorOption MinLenError func(f reflect.StructField, actual, min interface{}) error }
MinLenErrorOption is a factory of error caused by "minLen" validator.
type ParamParseErrorOption ¶
type ParamParseErrorOption struct {
ParamParseError func(in string, f reflect.StructField, expected string) error
}
ParamParseErrorOption is a factory of error caused by parsing parameter.
type ReqErrorOption ¶
type ReqErrorOption struct { *UnsupportedTypeErrorOption ReqError func(f reflect.StructField, actual interface{}) error }
ReqErrorOption is a factory of error caused by "req" validator.
type Target ¶
type Target struct { StructType reflect.Type StructValue reflect.Value FieldIndex int FieldInfo reflect.StructField FieldValue reflect.Value }
Target is information of validation target.
type UnsupportedTypeErrorOption ¶
type UnsupportedTypeErrorOption struct {
UnsupportedTypeError func(in string, f reflect.StructField) error
}
UnsupportedTypeErrorOption is a factory of error caused by unsupported type.
type ValidationFunc ¶
ValidationFunc is validation function itself.
func DefaultFactory ¶
func DefaultFactory(e *DefaultErrorOption) ValidationFunc
DefaultFactory returns function set default value.
func EmailFactory ¶
func EmailFactory(e *EmailErrorOption) ValidationFunc
EmailFactory returns function check value is `Email` format.
func EnumFactory ¶
func EnumFactory(e *EnumErrorOption) ValidationFunc
EnumFactory returns function check value is established value.
func MaxFactory ¶
func MaxFactory(e *MaxErrorOption) ValidationFunc
MaxFactory returns function check maximum value.
func MaxLenFactory ¶
func MaxLenFactory(e *MaxLenErrorOption) ValidationFunc
MaxLenFactory returns function check maximum length.
func MinFactory ¶
func MinFactory(e *MinErrorOption) ValidationFunc
MinFactory returns function check minimum value.
func MinLenFactory ¶
func MinLenFactory(e *MinLenErrorOption) ValidationFunc
MinLenFactory returns function check minimum length.
func ReqFactory ¶
func ReqFactory(e *ReqErrorOption) ValidationFunc
ReqFactory returns function validate parameter. it must not be empty.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is holder of validation information.
Example (UseCustomizedError) ¶
v := &Validator{} v.SetTag("validate") v.SetValidationFunc("req", ReqFactory(&ReqErrorOption{ ReqError: func(f reflect.StructField, actual interface{}) error { return fmt.Errorf("%s IS REQUIRED", f.Name) }, })) obj := &struct { FooBar string `validate:"req"` }{} err := v.Validate(obj) fmt.Println(err.Error())
Output: FooBar IS REQUIRED
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(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()) }
Output: Value: required, actual `` 10 Value: 0 less than 10 Value: 100 greater than 10 Value: less than 3: `ab` Value: greater than 3: `abcd` Value: unsupported email format foo@bar@buzz Value: `C` is not member of [A, B]
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(t *Target, param string) error { val := t.FieldValue if str := val.String(); str == "" { return fmt.Errorf("unexpected value: '%s'", str) } return nil }) obj := &struct { Value string `validate:"req"` }{} err := v.Validate(obj) fmt.Println(err.Error())
Output: unexpected value: ''