Skip to content

Commit 5ad5c12

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #207 from joeybloggs/v8-development
Fix issue with structOnly + struct level validation
2 parents 6268452 + 5985996 commit 5ad5c12

File tree

2 files changed

+59
-61
lines changed

2 files changed

+59
-61
lines changed

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,34 +310,34 @@ Benchmarks
310310
```go
311311
$ go test -cpu=4 -bench=. -benchmem=true
312312
PASS
313-
BenchmarkFieldSuccess-4 5000000 305 ns/op 16 B/op 1 allocs/op
314-
BenchmarkFieldFailure-4 5000000 301 ns/op 16 B/op 1 allocs/op
315-
BenchmarkFieldDiveSuccess-4 500000 3544 ns/op 528 B/op 28 allocs/op
316-
BenchmarkFieldDiveFailure-4 300000 4120 ns/op 928 B/op 32 allocs/op
317-
BenchmarkFieldCustomTypeSuccess-4 3000000 465 ns/op 32 B/op 2 allocs/op
318-
BenchmarkFieldCustomTypeFailure-4 2000000 769 ns/op 400 B/op 4 allocs/op
319-
BenchmarkFieldOrTagSuccess-4 1000000 1372 ns/op 32 B/op 2 allocs/op
320-
BenchmarkFieldOrTagFailure-4 1000000 1218 ns/op 432 B/op 6 allocs/op
321-
BenchmarkStructLevelValidationSuccess-4 2000000 840 ns/op 160 B/op 6 allocs/op
322-
BenchmarkStructLevelValidationFailure-4 1000000 1443 ns/op 592 B/op 11 allocs/op
323-
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1262 ns/op 80 B/op 5 allocs/op
324-
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1812 ns/op 624 B/op 11 allocs/op
325-
BenchmarkStructPartialSuccess-4 1000000 1419 ns/op 400 B/op 11 allocs/op
326-
BenchmarkStructPartialFailure-4 1000000 1967 ns/op 816 B/op 16 allocs/op
327-
BenchmarkStructExceptSuccess-4 2000000 954 ns/op 368 B/op 9 allocs/op
328-
BenchmarkStructExceptFailure-4 1000000 1422 ns/op 400 B/op 11 allocs/op
329-
BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1286 ns/op 128 B/op 6 allocs/op
330-
BenchmarkStructSimpleCrossFieldFailure-4 1000000 1885 ns/op 560 B/op 11 allocs/op
331-
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1948 ns/op 176 B/op 9 allocs/op
332-
BenchmarkStructSimpleCrossStructCrossFieldFailure-4 500000 2491 ns/op 608 B/op 14 allocs/op
333-
BenchmarkStructSimpleSuccess-4 1000000 1239 ns/op 48 B/op 3 allocs/op
334-
BenchmarkStructSimpleFailure-4 1000000 1891 ns/op 624 B/op 11 allocs/op
335-
BenchmarkStructSimpleSuccessParallel-4 5000000 386 ns/op 48 B/op 3 allocs/op
336-
BenchmarkStructSimpleFailureParallel-4 2000000 842 ns/op 624 B/op 11 allocs/op
337-
BenchmarkStructComplexSuccess-4 200000 8604 ns/op 512 B/op 30 allocs/op
338-
BenchmarkStructComplexFailure-4 100000 13332 ns/op 3416 B/op 72 allocs/op
339-
BenchmarkStructComplexSuccessParallel-4 1000000 2929 ns/op 512 B/op 30 allocs/op
340-
BenchmarkStructComplexFailureParallel-4 300000 5220 ns/op 3416 B/op 72 allocs/op
313+
BenchmarkFieldSuccess-4 5000000 288 ns/op 16 B/op 1 allocs/op
314+
BenchmarkFieldFailure-4 5000000 292 ns/op 16 B/op 1 allocs/op
315+
BenchmarkFieldDiveSuccess-4 500000 3464 ns/op 528 B/op 28 allocs/op
316+
BenchmarkFieldDiveFailure-4 500000 4031 ns/op 928 B/op 32 allocs/op
317+
BenchmarkFieldCustomTypeSuccess-4 3000000 446 ns/op 32 B/op 2 allocs/op
318+
BenchmarkFieldCustomTypeFailure-4 2000000 755 ns/op 400 B/op 4 allocs/op
319+
BenchmarkFieldOrTagSuccess-4 1000000 1356 ns/op 32 B/op 2 allocs/op
320+
BenchmarkFieldOrTagFailure-4 1000000 1177 ns/op 432 B/op 6 allocs/op
321+
BenchmarkStructLevelValidationSuccess-4 2000000 810 ns/op 160 B/op 6 allocs/op
322+
BenchmarkStructLevelValidationFailure-4 1000000 1424 ns/op 592 B/op 11 allocs/op
323+
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1231 ns/op 80 B/op 5 allocs/op
324+
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1779 ns/op 624 B/op 11 allocs/op
325+
BenchmarkStructPartialSuccess-4 1000000 1396 ns/op 400 B/op 11 allocs/op
326+
BenchmarkStructPartialFailure-4 1000000 1928 ns/op 816 B/op 16 allocs/op
327+
BenchmarkStructExceptSuccess-4 2000000 946 ns/op 368 B/op 9 allocs/op
328+
BenchmarkStructExceptFailure-4 1000000 1396 ns/op 400 B/op 11 allocs/op
329+
BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1241 ns/op 128 B/op 6 allocs/op
330+
BenchmarkStructSimpleCrossFieldFailure-4 1000000 1837 ns/op 560 B/op 11 allocs/op
331+
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1867 ns/op 176 B/op 9 allocs/op
332+
BenchmarkStructSimpleCrossStructCrossFieldFailure-4 500000 2495 ns/op 608 B/op 14 allocs/op
333+
BenchmarkStructSimpleSuccess-4 1000000 1253 ns/op 48 B/op 3 allocs/op
334+
BenchmarkStructSimpleFailure-4 1000000 1852 ns/op 624 B/op 11 allocs/op
335+
BenchmarkStructSimpleSuccessParallel-4 5000000 372 ns/op 48 B/op 3 allocs/op
336+
BenchmarkStructSimpleFailureParallel-4 2000000 784 ns/op 624 B/op 11 allocs/op
337+
BenchmarkStructComplexSuccess-4 200000 8080 ns/op 512 B/op 30 allocs/op
338+
BenchmarkStructComplexFailure-4 100000 12643 ns/op 3416 B/op 72 allocs/op
339+
BenchmarkStructComplexSuccessParallel-4 1000000 2688 ns/op 512 B/op 30 allocs/op
340+
BenchmarkStructComplexFailureParallel-4 300000 4727 ns/op 3416 B/op 72 allocs/op
341341
```
342342

343343
How to Contribute

validator.go

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error {
413413

414414
errs := v.errsPool.Get().(ValidationErrors)
415415

416-
v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, false, m)
416+
v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, false, m, false)
417417

418418
if len(errs) == 0 {
419419
v.errsPool.Put(errs)
@@ -440,7 +440,7 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) error {
440440

441441
errs := v.errsPool.Get().(ValidationErrors)
442442

443-
v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, true, m)
443+
v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, true, m, false)
444444

445445
if len(errs) == 0 {
446446
v.errsPool.Put(errs)
@@ -459,7 +459,7 @@ func (v *Validate) Struct(current interface{}) error {
459459
errs := v.errsPool.Get().(ValidationErrors)
460460
sv := reflect.ValueOf(current)
461461

462-
v.tranverseStruct(sv, sv, sv, blank, errs, true, false, false, nil)
462+
v.tranverseStruct(sv, sv, sv, blank, errs, true, false, false, nil, false)
463463

464464
if len(errs) == 0 {
465465
v.errsPool.Put(errs)
@@ -470,7 +470,7 @@ func (v *Validate) Struct(current interface{}) error {
470470
}
471471

472472
// tranverseStruct traverses a structs fields and then passes them to be validated by traverseField
473-
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{}) {
473+
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{}, isStructOnly bool) {
474474

475475
if current.Kind() == reflect.Ptr && !current.IsNil() {
476476
current = current.Elem()
@@ -487,39 +487,44 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
487487
errPrefix += typ.Name() + "."
488488
}
489489

490-
numFields := current.NumField()
490+
// structonly tag present don't tranverseFields
491+
// but must still check and run below struct level validation
492+
// if present
493+
if !isStructOnly {
494+
numFields := current.NumField()
491495

492-
var fld reflect.StructField
493-
var customName string
496+
var fld reflect.StructField
497+
var customName string
494498

495-
for i := 0; i < numFields; i++ {
496-
fld = typ.Field(i)
499+
for i := 0; i < numFields; i++ {
500+
fld = typ.Field(i)
497501

498-
if !unicode.IsUpper(rune(fld.Name[0])) {
499-
continue
500-
}
502+
if !unicode.IsUpper(rune(fld.Name[0])) {
503+
continue
504+
}
501505

502-
if partial {
506+
if partial {
503507

504-
_, ok = includeExclude[errPrefix+fld.Name]
508+
_, ok = includeExclude[errPrefix+fld.Name]
505509

506-
if (ok && exclude) || (!ok && !exclude) {
507-
continue
510+
if (ok && exclude) || (!ok && !exclude) {
511+
continue
512+
}
508513
}
509-
}
510514

511-
customName = fld.Name
512-
if v.fieldNameTag != "" {
515+
customName = fld.Name
516+
if v.fieldNameTag != "" {
513517

514-
name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0]
518+
name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0]
515519

516-
// dash check is for json "-" means don't output in json
517-
if name != "" && name != "-" {
518-
customName = name
520+
// dash check is for json "-" means don't output in json
521+
if name != "" && name != "-" {
522+
customName = name
523+
}
519524
}
520-
}
521525

522-
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude)
526+
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude)
527+
}
523528
}
524529

525530
// check if any struct level validations, after all field validations already checked.
@@ -590,14 +595,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
590595
typ = current.Type()
591596

592597
if typ != timeType {
593-
594-
// required passed validation above so stop here
595-
// if only validating the structs existance.
596-
if strings.Contains(tag, structOnlyTag) {
597-
return
598-
}
599-
600-
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude)
598+
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude, strings.Contains(tag, structOnlyTag))
601599
return
602600
}
603601
}

0 commit comments

Comments
 (0)