finish conversion of test + some fixes.

pull/256/head
joeybloggs 8 years ago
parent 3bf69e2bbd
commit 1ff58be284
  1. 3
      field_level.go
  2. 15
      validator.go
  3. 10
      validator_instance.go
  4. 10378
      validator_test.go

@ -9,7 +9,8 @@ type FieldLevel interface {
// returns the top level struct, if any // returns the top level struct, if any
Top() reflect.Value Top() reflect.Value
// returns the current fields parent struct, if any // returns the current fields parent struct, if any or
// the comparison value if called 'VarWithValue'
Parent() reflect.Value Parent() reflect.Value
// returns current field for validation // returns current field for validation

@ -33,14 +33,12 @@ type validate struct {
// parent and current will be the same the first run of validateStruct // parent and current will be the same the first run of validateStruct
func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, typ reflect.Type, ns []byte, structNs []byte, ct *cTag) { func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, typ reflect.Type, ns []byte, structNs []byte, ct *cTag) {
first := len(ns) == 0
cs, ok := v.v.structCache.Get(typ) cs, ok := v.v.structCache.Get(typ)
if !ok { if !ok {
cs = v.v.extractStructCache(current, typ.Name()) cs = v.v.extractStructCache(current, typ.Name())
} }
if first { if len(ns) == 0 {
ns = append(ns, cs.Name...) ns = append(ns, cs.Name...)
ns = append(ns, '.') ns = append(ns, '.')
@ -154,7 +152,16 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
return return
} }
v.validateStruct(current, current, typ, append(append(ns, cf.AltName...), '.'), append(append(structNs, cf.Name...), '.'), ct) // if len == 0 then validating using 'Var' or 'VarWithValue'
// Var - doesn't make much sense to do it that way, should call 'Struct', but no harm...
// VarWithField - this allows for validating against each field withing the struct against a specific value
// pretty handly in certain situations
if len(ns) > 0 {
ns = append(append(ns, cf.AltName...), '.')
structNs = append(append(structNs, cf.Name...), '.')
}
v.validateStruct(current, current, typ, ns, structNs, ct)
return return
} }
} }

@ -182,6 +182,8 @@ func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{
for _, t := range types { for _, t := range types {
v.customFuncs[reflect.TypeOf(t)] = fn v.customFuncs[reflect.TypeOf(t)] = fn
} }
v.hasCustomFuncs = true
} }
// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified. // Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
@ -351,6 +353,10 @@ func (v *Validate) StructExcept(s interface{}, fields ...string) (err error) {
// var i int // var i int
// validate.Var(i, "gt=1,lt=10") // validate.Var(i, "gt=1,lt=10")
// //
// WARNING: a struct can be passed for validation eg. time.Time is a struct or if you have a custom type and have registered
// a custom type handler, so must allow it; however unforseen validations will occur if trying to validate a struct
// that is meant to be passed to 'validate.Struct'
//
// It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise. // It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise.
// You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors. // You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors.
// validate Array, Slice and maps fields which may contain more than one error // validate Array, Slice and maps fields which may contain more than one error
@ -399,6 +405,10 @@ func (v *Validate) Var(field interface{}, tag string) (err error) {
// s2 := "abcd" // s2 := "abcd"
// validate.VarWithValue(s1, s2, "eqcsfield") // returns true // validate.VarWithValue(s1, s2, "eqcsfield") // returns true
// //
// WARNING: a struct can be passed for validation eg. time.Time is a struct or if you have a custom type and have registered
// a custom type handler, so must allow it; however unforseen validations will occur if trying to validate a struct
// that is meant to be passed to 'validate.Struct'
//
// It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise. // It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise.
// You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors. // You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors.
// validate Array, Slice and maps fields which may contain more than one error // validate Array, Slice and maps fields which may contain more than one error

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save