Merge pull request #158 from bluesuncorp/v6-development

Backport v7 updates
v6 v6.7
Dean Karn 9 years ago
commit a32ef92045
  1. 38
      README.md
  2. 37
      baked_in.go
  3. 118
      benchmarks_test.go
  4. 82
      util.go
  5. 183
      validator.go
  6. 231
      validator_test.go

@ -192,24 +192,28 @@ NOTE: allocations for structs are up from v5, however ns/op for parallel operati
It was a decicion not to cache struct info because although it reduced allocation to v5 levels, it It was a decicion not to cache struct info because although it reduced allocation to v5 levels, it
hurt parallel performance too much. hurt parallel performance too much.
```go ```go
$ go test -cpu=4 -bench=. -benchmem=true go test -cpu=4 -bench=. -benchmem=true
PASS PASS
BenchmarkFieldSuccess-4 5000000 318 ns/op 16 B/op 1 allocs/op BenchmarkFieldSuccess-4 5000000 337 ns/op 16 B/op 1 allocs/op
BenchmarkFieldFailure-4 5000000 316 ns/op 16 B/op 1 allocs/op BenchmarkFieldFailure-4 5000000 331 ns/op 16 B/op 1 allocs/op
BenchmarkFieldCustomTypeSuccess-4 3000000 492 ns/op 32 B/op 2 allocs/op BenchmarkFieldCustomTypeSuccess-4 3000000 497 ns/op 32 B/op 2 allocs/op
BenchmarkFieldCustomTypeFailure-4 2000000 843 ns/op 416 B/op 6 allocs/op BenchmarkFieldCustomTypeFailure-4 2000000 842 ns/op 416 B/op 6 allocs/op
BenchmarkFieldOrTagSuccess-4 500000 2384 ns/op 20 B/op 2 allocs/op BenchmarkFieldOrTagSuccess-4 500000 2432 ns/op 20 B/op 2 allocs/op
BenchmarkFieldOrTagFailure-4 1000000 1295 ns/op 384 B/op 6 allocs/op BenchmarkFieldOrTagFailure-4 1000000 1323 ns/op 384 B/op 6 allocs/op
BenchmarkStructSimpleSuccess-4 1000000 1175 ns/op 24 B/op 3 allocs/op BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1409 ns/op 56 B/op 5 allocs/op
BenchmarkStructSimpleFailure-4 1000000 1822 ns/op 529 B/op 11 allocs/op BenchmarkStructSimpleCustomTypeFailure-4 1000000 1876 ns/op 577 B/op 13 allocs/op
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1302 ns/op 56 B/op 5 allocs/op BenchmarkStructPartialSuccess-4 1000000 1438 ns/op 384 B/op 13 allocs/op
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1847 ns/op 577 B/op 13 allocs/op BenchmarkStructPartialFailure-4 1000000 2040 ns/op 785 B/op 18 allocs/op
BenchmarkStructSimpleSuccessParallel-4 5000000 339 ns/op 24 B/op 3 allocs/op BenchmarkStructExceptSuccess-4 1000000 1000 ns/op 368 B/op 11 allocs/op
BenchmarkStructSimpleFailureParallel-4 2000000 733 ns/op 529 B/op 11 allocs/op BenchmarkStructExceptFailure-4 1000000 1431 ns/op 384 B/op 13 allocs/op
BenchmarkStructComplexSuccess-4 200000 7104 ns/op 368 B/op 30 allocs/op BenchmarkStructSimpleSuccess-4 1000000 1375 ns/op 24 B/op 3 allocs/op
BenchmarkStructComplexFailure-4 100000 11996 ns/op 2861 B/op 72 allocs/op BenchmarkStructSimpleFailure-4 1000000 1893 ns/op 529 B/op 11 allocs/op
BenchmarkStructComplexSuccessParallel-4 1000000 2252 ns/op 368 B/op 30 allocs/op BenchmarkStructSimpleSuccessParallel-4 5000000 362 ns/op 24 B/op 3 allocs/op
BenchmarkStructComplexFailureParallel-4 300000 4691 ns/op 2862 B/op 72 allocs/op BenchmarkStructSimpleFailureParallel-4 2000000 883 ns/op 529 B/op 11 allocs/op
BenchmarkStructComplexSuccess-4 200000 8237 ns/op 368 B/op 30 allocs/op
BenchmarkStructComplexFailure-4 100000 12617 ns/op 2861 B/op 72 allocs/op
BenchmarkStructComplexSuccessParallel-4 1000000 2398 ns/op 368 B/op 30 allocs/op
BenchmarkStructComplexFailureParallel-4 300000 5733 ns/op 2862 B/op 72 allocs/op
``` ```
How to Contribute How to Contribute

@ -5,7 +5,6 @@ import (
"net" "net"
"net/url" "net/url"
"reflect" "reflect"
"strconv"
"strings" "strings"
"time" "time"
"unicode/utf8" "unicode/utf8"
@ -902,39 +901,3 @@ func hasMaxOf(topStruct reflect.Value, currentStruct reflect.Value, field reflec
return isLte(topStruct, currentStruct, field, fieldType, fieldKind, param) return isLte(topStruct, currentStruct, field, fieldType, fieldKind, param)
} }
// asInt retuns the parameter as a int64
// or panics if it can't convert
func asInt(param string) int64 {
i, err := strconv.ParseInt(param, 0, 64)
panicIf(err)
return i
}
// asUint returns the parameter as a uint64
// or panics if it can't convert
func asUint(param string) uint64 {
i, err := strconv.ParseUint(param, 0, 64)
panicIf(err)
return i
}
// asFloat returns the parameter as a float64
// or panics if it can't convert
func asFloat(param string) float64 {
i, err := strconv.ParseFloat(param, 64)
panicIf(err)
return i
}
func panicIf(err error) {
if err != nil {
panic(err.Error())
}
}

@ -62,35 +62,31 @@ func BenchmarkFieldOrTagFailure(b *testing.B) {
} }
} }
func BenchmarkStructSimpleSuccess(b *testing.B) { func BenchmarkStructSimpleCustomTypeSuccess(b *testing.B) {
type Foo struct { customTypes := map[reflect.Type]CustomTypeFunc{}
StringValue string `validate:"min=5,max=10"` customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
IntValue int `validate:"min=5,max=10"` customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
}
validFoo := &Foo{StringValue: "Foobar", IntValue: 7} validate := New(Config{TagName: "validate", ValidationFuncs: BakedInValidators, CustomTypeFuncs: customTypes})
for n := 0; n < b.N; n++ { val := valuer{
validate.Struct(validFoo) Name: "1",
} }
}
func BenchmarkStructSimpleFailure(b *testing.B) {
type Foo struct { type Foo struct {
StringValue string `validate:"min=5,max=10"` Valuer valuer `validate:"len=1"`
IntValue int `validate:"min=5,max=10"` IntValue int `validate:"min=5,max=10"`
} }
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3} validFoo := &Foo{Valuer: val, IntValue: 7}
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
validate.Struct(invalidFoo) validate.Struct(validFoo)
} }
} }
func BenchmarkStructSimpleCustomTypeSuccess(b *testing.B) { func BenchmarkStructSimpleCustomTypeFailure(b *testing.B) {
customTypes := map[reflect.Type]CustomTypeFunc{} customTypes := map[reflect.Type]CustomTypeFunc{}
customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
@ -98,44 +94,112 @@ func BenchmarkStructSimpleCustomTypeSuccess(b *testing.B) {
validate := New(Config{TagName: "validate", ValidationFuncs: BakedInValidators, CustomTypeFuncs: customTypes}) validate := New(Config{TagName: "validate", ValidationFuncs: BakedInValidators, CustomTypeFuncs: customTypes})
val := valuer{ val := valuer{}
Name: "1",
}
type Foo struct { type Foo struct {
Valuer valuer `validate:"len=1"` Valuer valuer `validate:"len=1"`
IntValue int `validate:"min=5,max=10"` IntValue int `validate:"min=5,max=10"`
} }
validFoo := &Foo{Valuer: val, IntValue: 7} validFoo := &Foo{Valuer: val, IntValue: 3}
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
validate.Struct(validFoo) validate.Struct(validFoo)
} }
} }
func BenchmarkStructSimpleCustomTypeFailure(b *testing.B) { func BenchmarkStructPartialSuccess(b *testing.B) {
customTypes := map[reflect.Type]CustomTypeFunc{} type Test struct {
customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType Name string `validate:"required"`
customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType NickName string `validate:"required"`
}
validate := New(Config{TagName: "validate", ValidationFuncs: BakedInValidators, CustomTypeFuncs: customTypes}) test := &Test{
Name: "Joey Bloggs",
}
val := valuer{} for n := 0; n < b.N; n++ {
validate.StructPartial(test, "Name")
}
}
func BenchmarkStructPartialFailure(b *testing.B) {
type Test struct {
Name string `validate:"required"`
NickName string `validate:"required"`
}
test := &Test{
Name: "Joey Bloggs",
}
for n := 0; n < b.N; n++ {
validate.StructPartial(test, "NickName")
}
}
func BenchmarkStructExceptSuccess(b *testing.B) {
type Test struct {
Name string `validate:"required"`
NickName string `validate:"required"`
}
test := &Test{
Name: "Joey Bloggs",
}
for n := 0; n < b.N; n++ {
validate.StructPartial(test, "Nickname")
}
}
func BenchmarkStructExceptFailure(b *testing.B) {
type Test struct {
Name string `validate:"required"`
NickName string `validate:"required"`
}
test := &Test{
Name: "Joey Bloggs",
}
for n := 0; n < b.N; n++ {
validate.StructPartial(test, "Name")
}
}
func BenchmarkStructSimpleSuccess(b *testing.B) {
type Foo struct { type Foo struct {
Valuer valuer `validate:"len=1"` StringValue string `validate:"min=5,max=10"`
IntValue int `validate:"min=5,max=10"` IntValue int `validate:"min=5,max=10"`
} }
validFoo := &Foo{Valuer: val, IntValue: 3} validFoo := &Foo{StringValue: "Foobar", IntValue: 7}
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
validate.Struct(validFoo) validate.Struct(validFoo)
} }
} }
func BenchmarkStructSimpleFailure(b *testing.B) {
type Foo struct {
StringValue string `validate:"min=5,max=10"`
IntValue int `validate:"min=5,max=10"`
}
invalidFoo := &Foo{StringValue: "Fo", IntValue: 3}
for n := 0; n < b.N; n++ {
validate.Struct(invalidFoo)
}
}
func BenchmarkStructSimpleSuccessParallel(b *testing.B) { func BenchmarkStructSimpleSuccessParallel(b *testing.B) {
type Foo struct { type Foo struct {

@ -0,0 +1,82 @@
package validator
import (
"reflect"
"strconv"
)
const (
namespaceSeparator = "."
leftBracket = "["
rightBracket = "]"
)
func (v *Validate) extractType(current reflect.Value) (reflect.Value, reflect.Kind) {
switch current.Kind() {
case reflect.Ptr:
if current.IsNil() {
return current, reflect.Ptr
}
return v.extractType(current.Elem())
case reflect.Interface:
if current.IsNil() {
return current, reflect.Interface
}
return v.extractType(current.Elem())
case reflect.Invalid:
return current, reflect.Invalid
default:
if v.config.hasCustomFuncs {
if fn, ok := v.config.CustomTypeFuncs[current.Type()]; ok {
return v.extractType(reflect.ValueOf(fn(current)))
}
}
return current, current.Kind()
}
}
// asInt retuns the parameter as a int64
// or panics if it can't convert
func asInt(param string) int64 {
i, err := strconv.ParseInt(param, 0, 64)
panicIf(err)
return i
}
// asUint returns the parameter as a uint64
// or panics if it can't convert
func asUint(param string) uint64 {
i, err := strconv.ParseUint(param, 0, 64)
panicIf(err)
return i
}
// asFloat returns the parameter as a float64
// or panics if it can't convert
func asFloat(param string) float64 {
i, err := strconv.ParseFloat(param, 64)
panicIf(err)
return i
}
func panicIf(err error) {
if err != nil {
panic(err.Error())
}
}

@ -42,6 +42,7 @@ var (
timePtrType = reflect.TypeOf(&time.Time{}) timePtrType = reflect.TypeOf(&time.Time{})
errsPool = &sync.Pool{New: newValidationErrors} errsPool = &sync.Pool{New: newValidationErrors}
tagsCache = &tagCacheMap{m: map[string][]*tagCache{}} tagsCache = &tagCacheMap{m: map[string][]*tagCache{}}
emptyStructPtr = new(struct{})
) )
// returns new ValidationErrors to the pool // returns new ValidationErrors to the pool
@ -180,7 +181,7 @@ func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
errs := errsPool.Get().(ValidationErrors) errs := errsPool.Get().(ValidationErrors)
fieldVal := reflect.ValueOf(field) fieldVal := reflect.ValueOf(field)
v.traverseField(fieldVal, fieldVal, fieldVal, "", errs, false, tag, "") v.traverseField(fieldVal, fieldVal, fieldVal, "", errs, false, tag, "", false, false, nil)
if len(errs) == 0 { if len(errs) == 0 {
errsPool.Put(errs) errsPool.Put(errs)
@ -198,7 +199,92 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
errs := errsPool.Get().(ValidationErrors) errs := errsPool.Get().(ValidationErrors)
topVal := reflect.ValueOf(val) topVal := reflect.ValueOf(val)
v.traverseField(topVal, topVal, reflect.ValueOf(field), "", errs, false, tag, "") v.traverseField(topVal, topVal, reflect.ValueOf(field), "", errs, false, tag, "", false, false, nil)
if len(errs) == 0 {
errsPool.Put(errs)
return nil
}
return errs
}
// StructPartial validates the fields passed in only, ignoring all others.
// Fields may be provided in a namespaced fashion relative to the struct provided
// i.e. NestedStruct.Field or NestedArrayField[0].Struct.Name
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
// legacy data structure, it will be useful
func (v *Validate) StructPartial(current interface{}, fields ...string) ValidationErrors {
sv, _ := v.extractType(reflect.ValueOf(current))
name := sv.Type().Name()
m := map[string]*struct{}{}
if fields != nil {
for _, k := range fields {
flds := strings.Split(k, namespaceSeparator)
if len(flds) > 0 {
key := name + namespaceSeparator
for _, s := range flds {
idx := strings.Index(s, leftBracket)
if idx != -1 {
for idx != -1 {
key += s[:idx]
m[key] = emptyStructPtr
idx2 := strings.Index(s, rightBracket)
idx2++
key += s[idx:idx2]
m[key] = emptyStructPtr
s = s[idx2:]
idx = strings.Index(s, leftBracket)
}
} else {
key += s
m[key] = emptyStructPtr
}
key += namespaceSeparator
}
}
}
}
errs := errsPool.Get().(ValidationErrors)
v.tranverseStruct(sv, sv, sv, "", errs, true, len(m) != 0, false, m)
if len(errs) == 0 {
errsPool.Put(errs)
return nil
}
return errs
}
// StructExcept validates all fields except the ones passed in.
// Fields may be provided in a namespaced fashion relative to the struct provided
// i.e. NestedStruct.Field or NestedArrayField[0].Struct.Name
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
// legacy data structure, it will be useful
func (v *Validate) StructExcept(current interface{}, fields ...string) ValidationErrors {
sv, _ := v.extractType(reflect.ValueOf(current))
name := sv.Type().Name()
m := map[string]*struct{}{}
for _, key := range fields {
m[name+"."+key] = emptyStructPtr
}
errs := errsPool.Get().(ValidationErrors)
v.tranverseStruct(sv, sv, sv, "", errs, true, len(m) != 0, true, m)
if len(errs) == 0 { if len(errs) == 0 {
errsPool.Put(errs) errsPool.Put(errs)
@ -214,7 +300,7 @@ func (v *Validate) Struct(current interface{}) ValidationErrors {
errs := errsPool.Get().(ValidationErrors) errs := errsPool.Get().(ValidationErrors)
sv := reflect.ValueOf(current) sv := reflect.ValueOf(current)
v.tranverseStruct(sv, sv, sv, "", errs, true) v.tranverseStruct(sv, sv, sv, "", errs, true, false, false, nil)
if len(errs) == 0 { if len(errs) == 0 {
errsPool.Put(errs) errsPool.Put(errs)
@ -225,7 +311,7 @@ func (v *Validate) Struct(current interface{}) ValidationErrors {
} }
// tranverseStruct traverses a structs fields and then passes them to be validated by traverseField // tranverseStruct traverses a structs fields and then passes them to be validated by traverseField
func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, useStructName bool) { func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]*struct{}) {
if current.Kind() == reflect.Ptr && !current.IsNil() { if current.Kind() == reflect.Ptr && !current.IsNil() {
current = current.Elem() current = current.Elem()
@ -235,6 +321,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
panic("value passed for validation is not a struct") panic("value passed for validation is not a struct")
} }
var ok bool
typ := current.Type() typ := current.Type()
if useStructName { if useStructName {
@ -252,29 +339,31 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
continue continue
} }
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.config.TagName), fld.Name) if partial {
_, ok = includeExclude[errPrefix+fld.Name]
if (ok && exclude) || (!ok && !exclude) {
continue
}
}
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.config.TagName), fld.Name, partial, exclude, includeExclude)
} }
} }
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options // traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, isStructField bool, tag string, name string) { func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, isStructField bool, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
if tag == skipValidationTag { if tag == skipValidationTag {
return return
} }
kind := current.Kind() current, kind := v.extractType(current)
var typ reflect.Type
if kind == reflect.Ptr && !current.IsNil() {
current = current.Elem()
kind = current.Kind()
}
// this also allows for tags 'required' and 'omitempty' to be used on
// nested struct fields because when len(tags) > 0 below and the value is nil
// then required failes and we check for omitempty just before that
if ((kind == reflect.Ptr || kind == reflect.Interface) && current.IsNil()) || kind == reflect.Invalid {
switch kind {
case reflect.Ptr, reflect.Interface, reflect.Invalid:
if strings.Contains(tag, omitempty) { if strings.Contains(tag, omitempty) {
return return
} }
@ -314,41 +403,11 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
if kind == reflect.Invalid { if kind == reflect.Invalid {
return return
} }
}
typ := current.Type()
switch kind {
case reflect.Struct, reflect.Interface:
if kind == reflect.Interface {
current = current.Elem()
kind = current.Kind()
if kind == reflect.Ptr && !current.IsNil() {
current = current.Elem()
kind = current.Kind()
}
// changed current, so have to get inner type again case reflect.Struct:
typ = current.Type() typ = current.Type()
if kind != reflect.Struct { if typ != timeType {
goto FALLTHROUGH
}
}
if typ != timeType && typ != timePtrType {
if kind == reflect.Struct {
if v.config.hasCustomFuncs {
if fn, ok := v.config.CustomTypeFuncs[typ]; ok {
v.traverseField(topStruct, currentStruct, reflect.ValueOf(fn(current)), errPrefix, errs, isStructField, tag, name)
return
}
}
// required passed validation above so stop here // required passed validation above so stop here
// if only validating the structs existance. // if only validating the structs existance.
@ -356,24 +415,16 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
return return
} }
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false) v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude)
return return
} }
} }
FALLTHROUGH:
fallthrough
default:
if len(tag) == 0 { if len(tag) == 0 {
return return
} }
}
if v.config.hasCustomFuncs { typ = current.Type()
if fn, ok := v.config.CustomTypeFuncs[typ]; ok {
v.traverseField(topStruct, currentStruct, reflect.ValueOf(fn(current)), errPrefix, errs, isStructField, tag, name)
return
}
}
tags, isCached := tagsCache.Get(tag) tags, isCached := tagsCache.Get(tag)
@ -447,9 +498,9 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
// or panic ;) // or panic ;)
switch kind { switch kind {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name) v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude)
case reflect.Map: case reflect.Map:
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name) v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude)
default: default:
// throw error, if not a slice or map then should not have gotten here // throw error, if not a slice or map then should not have gotten here
// bad dive tag // bad dive tag
@ -459,18 +510,18 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
} }
// traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation // traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation
func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string) { func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
for i := 0; i < current.Len(); i++ { for i := 0; i < current.Len(); i++ {
v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i)) v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), partial, exclude, includeExclude)
} }
} }
// traverseMap traverses a map's elements and passes them to traverseField for validation // traverseMap traverses a map's elements and passes them to traverseField for validation
func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string) { func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) {
for _, key := range current.MapKeys() { for _, key := range current.MapKeys() {
v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface())) v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), partial, exclude, includeExclude)
} }
} }

@ -192,6 +192,237 @@ func ValidateValuerType(field reflect.Value) interface{} {
return nil return nil
} }
type TestPartial struct {
NoTag string
BlankTag string `validate:""`
Required string `validate:"required"`
SubSlice []*SubTest `validate:"required,dive"`
Sub *SubTest
SubIgnore *SubTest `validate:"-"`
Anonymous struct {
A string `validate:"required"`
ASubSlice []*SubTest `validate:"required,dive"`
SubAnonStruct []struct {
Test string `validate:"required"`
OtherTest string `validate:"required"`
} `validate:"required,dive"`
}
}
func TestStructPartial(t *testing.T) {
p1 := []string{
"NoTag",
"Required",
}
p2 := []string{
"SubSlice[0].Test",
"Sub",
"SubIgnore",
"Anonymous.A",
}
p3 := []string{
"SubTest.Test",
}
p4 := []string{
"A",
}
tPartial := &TestPartial{
NoTag: "NoTag",
Required: "Required",
SubSlice: []*SubTest{
{
Test: "Required",
},
{
Test: "Required",
},
},
Sub: &SubTest{
Test: "1",
},
SubIgnore: &SubTest{
Test: "",
},
Anonymous: struct {
A string `validate:"required"`
ASubSlice []*SubTest `validate:"required,dive"`
SubAnonStruct []struct {
Test string `validate:"required"`
OtherTest string `validate:"required"`
} `validate:"required,dive"`
}{
A: "1",
ASubSlice: []*SubTest{
{
Test: "Required",
},
{
Test: "Required",
},
},
SubAnonStruct: []struct {
Test string `validate:"required"`
OtherTest string `validate:"required"`
}{
{"Required", "RequiredOther"},
{"Required", "RequiredOther"},
},
},
}
// the following should all return no errors as everything is valid in
// the default state
errs := validate.StructPartial(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructPartial(tPartial, p2...)
Equal(t, errs, nil)
// this isnt really a robust test, but is ment to illustrate the ANON CASE below
errs = validate.StructPartial(tPartial.SubSlice[0], p3...)
Equal(t, errs, nil)
errs = validate.StructExcept(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructExcept(tPartial, p2...)
Equal(t, errs, nil)
// mod tParial for required feild and re-test making sure invalid fields are NOT required:
tPartial.Required = ""
errs = validate.StructExcept(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructPartial(tPartial, p2...)
Equal(t, errs, nil)
// inversion and retesting Partial to generate failures:
errs = validate.StructPartial(tPartial, p1...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.Required", "Required", "required")
errs = validate.StructExcept(tPartial, p2...)
AssertError(t, errs, "TestPartial.Required", "Required", "required")
// reset Required field, and set nested struct
tPartial.Required = "Required"
tPartial.Anonymous.A = ""
// will pass as unset feilds is not going to be tested
errs = validate.StructPartial(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructExcept(tPartial, p2...)
Equal(t, errs, nil)
// ANON CASE the response here is strange, it clearly does what it is being told to
errs = validate.StructExcept(tPartial.Anonymous, p4...)
Equal(t, errs, nil)
// will fail as unset feild is tested
errs = validate.StructPartial(tPartial, p2...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.Anonymous.A", "A", "required")
errs = validate.StructExcept(tPartial, p1...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.Anonymous.A", "A", "required")
// reset nested struct and unset struct in slice
tPartial.Anonymous.A = "Required"
tPartial.SubSlice[0].Test = ""
// these will pass as unset item is NOT tested
errs = validate.StructPartial(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructExcept(tPartial, p2...)
Equal(t, errs, nil)
// these will fail as unset item IS tested
errs = validate.StructExcept(tPartial, p1...)
AssertError(t, errs, "TestPartial.SubSlice[0].Test", "Test", "required")
Equal(t, len(errs), 1)
errs = validate.StructPartial(tPartial, p2...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.SubSlice[0].Test", "Test", "required")
Equal(t, len(errs), 1)
// Unset second slice member concurrently to test dive behavior:
tPartial.SubSlice[1].Test = ""
errs = validate.StructPartial(tPartial, p1...)
Equal(t, errs, nil)
// NOTE: When specifying nested items, it is still the users responsibility
// to specify the dive tag, the library does not override this.
errs = validate.StructExcept(tPartial, p2...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.SubSlice[1].Test", "Test", "required")
errs = validate.StructExcept(tPartial, p1...)
Equal(t, len(errs), 2)
AssertError(t, errs, "TestPartial.SubSlice[0].Test", "Test", "required")
AssertError(t, errs, "TestPartial.SubSlice[1].Test", "Test", "required")
errs = validate.StructPartial(tPartial, p2...)
NotEqual(t, errs, nil)
Equal(t, len(errs), 1)
AssertError(t, errs, "TestPartial.SubSlice[0].Test", "Test", "required")
// reset struct in slice, and unset struct in slice in unset posistion
tPartial.SubSlice[0].Test = "Required"
// these will pass as the unset item is NOT tested
errs = validate.StructPartial(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructPartial(tPartial, p2...)
Equal(t, errs, nil)
// testing for missing item by exception, yes it dives and fails
errs = validate.StructExcept(tPartial, p1...)
NotEqual(t, errs, nil)
Equal(t, len(errs), 1)
AssertError(t, errs, "TestPartial.SubSlice[1].Test", "Test", "required")
errs = validate.StructExcept(tPartial, p2...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.SubSlice[1].Test", "Test", "required")
tPartial.SubSlice[1].Test = "Required"
tPartial.Anonymous.SubAnonStruct[0].Test = ""
// these will pass as the unset item is NOT tested
errs = validate.StructPartial(tPartial, p1...)
Equal(t, errs, nil)
errs = validate.StructPartial(tPartial, p2...)
Equal(t, errs, nil)
errs = validate.StructExcept(tPartial, p1...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.Anonymous.SubAnonStruct[0].Test", "Test", "required")
errs = validate.StructExcept(tPartial, p2...)
NotEqual(t, errs, nil)
AssertError(t, errs, "TestPartial.Anonymous.SubAnonStruct[0].Test", "Test", "required")
}
func TestExistsValidation(t *testing.T) { func TestExistsValidation(t *testing.T) {
jsonText := "{ \"truthiness2\": true }" jsonText := "{ \"truthiness2\": true }"

Loading…
Cancel
Save