Merge pull request #208 from go-playground/v8-development
Fix issue with structOnly + struct level validation
Dean Karn
8 years ago
309 | 309 | ```go |
310 | 310 | $ go test -cpu=4 -bench=. -benchmem=true |
311 | 311 | PASS |
312 | BenchmarkFieldSuccess-4 5000000 305 ns/op 16 B/op 1 allocs/op | |
313 | BenchmarkFieldFailure-4 5000000 301 ns/op 16 B/op 1 allocs/op | |
314 | BenchmarkFieldDiveSuccess-4 500000 3544 ns/op 528 B/op 28 allocs/op | |
315 | BenchmarkFieldDiveFailure-4 300000 4120 ns/op 928 B/op 32 allocs/op | |
316 | BenchmarkFieldCustomTypeSuccess-4 3000000 465 ns/op 32 B/op 2 allocs/op | |
317 | BenchmarkFieldCustomTypeFailure-4 2000000 769 ns/op 400 B/op 4 allocs/op | |
318 | BenchmarkFieldOrTagSuccess-4 1000000 1372 ns/op 32 B/op 2 allocs/op | |
319 | BenchmarkFieldOrTagFailure-4 1000000 1218 ns/op 432 B/op 6 allocs/op | |
320 | BenchmarkStructLevelValidationSuccess-4 2000000 840 ns/op 160 B/op 6 allocs/op | |
321 | BenchmarkStructLevelValidationFailure-4 1000000 1443 ns/op 592 B/op 11 allocs/op | |
322 | BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1262 ns/op 80 B/op 5 allocs/op | |
323 | BenchmarkStructSimpleCustomTypeFailure-4 1000000 1812 ns/op 624 B/op 11 allocs/op | |
324 | BenchmarkStructPartialSuccess-4 1000000 1419 ns/op 400 B/op 11 allocs/op | |
325 | BenchmarkStructPartialFailure-4 1000000 1967 ns/op 816 B/op 16 allocs/op | |
326 | BenchmarkStructExceptSuccess-4 2000000 954 ns/op 368 B/op 9 allocs/op | |
327 | BenchmarkStructExceptFailure-4 1000000 1422 ns/op 400 B/op 11 allocs/op | |
328 | BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1286 ns/op 128 B/op 6 allocs/op | |
329 | BenchmarkStructSimpleCrossFieldFailure-4 1000000 1885 ns/op 560 B/op 11 allocs/op | |
330 | BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1948 ns/op 176 B/op 9 allocs/op | |
331 | BenchmarkStructSimpleCrossStructCrossFieldFailure-4 500000 2491 ns/op 608 B/op 14 allocs/op | |
332 | BenchmarkStructSimpleSuccess-4 1000000 1239 ns/op 48 B/op 3 allocs/op | |
333 | BenchmarkStructSimpleFailure-4 1000000 1891 ns/op 624 B/op 11 allocs/op | |
334 | BenchmarkStructSimpleSuccessParallel-4 5000000 386 ns/op 48 B/op 3 allocs/op | |
335 | BenchmarkStructSimpleFailureParallel-4 2000000 842 ns/op 624 B/op 11 allocs/op | |
336 | BenchmarkStructComplexSuccess-4 200000 8604 ns/op 512 B/op 30 allocs/op | |
337 | BenchmarkStructComplexFailure-4 100000 13332 ns/op 3416 B/op 72 allocs/op | |
338 | BenchmarkStructComplexSuccessParallel-4 1000000 2929 ns/op 512 B/op 30 allocs/op | |
339 | BenchmarkStructComplexFailureParallel-4 300000 5220 ns/op 3416 B/op 72 allocs/op | |
312 | BenchmarkFieldSuccess-4 5000000 288 ns/op 16 B/op 1 allocs/op | |
313 | BenchmarkFieldFailure-4 5000000 292 ns/op 16 B/op 1 allocs/op | |
314 | BenchmarkFieldDiveSuccess-4 500000 3464 ns/op 528 B/op 28 allocs/op | |
315 | BenchmarkFieldDiveFailure-4 500000 4031 ns/op 928 B/op 32 allocs/op | |
316 | BenchmarkFieldCustomTypeSuccess-4 3000000 446 ns/op 32 B/op 2 allocs/op | |
317 | BenchmarkFieldCustomTypeFailure-4 2000000 755 ns/op 400 B/op 4 allocs/op | |
318 | BenchmarkFieldOrTagSuccess-4 1000000 1356 ns/op 32 B/op 2 allocs/op | |
319 | BenchmarkFieldOrTagFailure-4 1000000 1177 ns/op 432 B/op 6 allocs/op | |
320 | BenchmarkStructLevelValidationSuccess-4 2000000 810 ns/op 160 B/op 6 allocs/op | |
321 | BenchmarkStructLevelValidationFailure-4 1000000 1424 ns/op 592 B/op 11 allocs/op | |
322 | BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1231 ns/op 80 B/op 5 allocs/op | |
323 | BenchmarkStructSimpleCustomTypeFailure-4 1000000 1779 ns/op 624 B/op 11 allocs/op | |
324 | BenchmarkStructPartialSuccess-4 1000000 1396 ns/op 400 B/op 11 allocs/op | |
325 | BenchmarkStructPartialFailure-4 1000000 1928 ns/op 816 B/op 16 allocs/op | |
326 | BenchmarkStructExceptSuccess-4 2000000 946 ns/op 368 B/op 9 allocs/op | |
327 | BenchmarkStructExceptFailure-4 1000000 1396 ns/op 400 B/op 11 allocs/op | |
328 | BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1241 ns/op 128 B/op 6 allocs/op | |
329 | BenchmarkStructSimpleCrossFieldFailure-4 1000000 1837 ns/op 560 B/op 11 allocs/op | |
330 | BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1867 ns/op 176 B/op 9 allocs/op | |
331 | BenchmarkStructSimpleCrossStructCrossFieldFailure-4 500000 2495 ns/op 608 B/op 14 allocs/op | |
332 | BenchmarkStructSimpleSuccess-4 1000000 1253 ns/op 48 B/op 3 allocs/op | |
333 | BenchmarkStructSimpleFailure-4 1000000 1852 ns/op 624 B/op 11 allocs/op | |
334 | BenchmarkStructSimpleSuccessParallel-4 5000000 372 ns/op 48 B/op 3 allocs/op | |
335 | BenchmarkStructSimpleFailureParallel-4 2000000 784 ns/op 624 B/op 11 allocs/op | |
336 | BenchmarkStructComplexSuccess-4 200000 8080 ns/op 512 B/op 30 allocs/op | |
337 | BenchmarkStructComplexFailure-4 100000 12643 ns/op 3416 B/op 72 allocs/op | |
338 | BenchmarkStructComplexSuccessParallel-4 1000000 2688 ns/op 512 B/op 30 allocs/op | |
339 | BenchmarkStructComplexFailureParallel-4 300000 4727 ns/op 3416 B/op 72 allocs/op | |
340 | 340 | ``` |
341 | 341 | |
342 | 342 | How to Contribute |
412 | 412 | |
413 | 413 | errs := v.errsPool.Get().(ValidationErrors) |
414 | 414 | |
415 | v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, false, m) | |
415 | v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, false, m, false) | |
416 | 416 | |
417 | 417 | if len(errs) == 0 { |
418 | 418 | v.errsPool.Put(errs) |
439 | 439 | |
440 | 440 | errs := v.errsPool.Get().(ValidationErrors) |
441 | 441 | |
442 | v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, true, m) | |
442 | v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, true, m, false) | |
443 | 443 | |
444 | 444 | if len(errs) == 0 { |
445 | 445 | v.errsPool.Put(errs) |
458 | 458 | errs := v.errsPool.Get().(ValidationErrors) |
459 | 459 | sv := reflect.ValueOf(current) |
460 | 460 | |
461 | v.tranverseStruct(sv, sv, sv, blank, errs, true, false, false, nil) | |
461 | v.tranverseStruct(sv, sv, sv, blank, errs, true, false, false, nil, false) | |
462 | 462 | |
463 | 463 | if len(errs) == 0 { |
464 | 464 | v.errsPool.Put(errs) |
469 | 469 | } |
470 | 470 | |
471 | 471 | // tranverseStruct traverses a structs fields and then passes them to be validated by traverseField |
472 | 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{}) { | |
472 | 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) { | |
473 | 473 | |
474 | 474 | if current.Kind() == reflect.Ptr && !current.IsNil() { |
475 | 475 | current = current.Elem() |
486 | 486 | errPrefix += typ.Name() + "." |
487 | 487 | } |
488 | 488 | |
489 | numFields := current.NumField() | |
490 | ||
491 | var fld reflect.StructField | |
492 | var customName string | |
493 | ||
494 | for i := 0; i < numFields; i++ { | |
495 | fld = typ.Field(i) | |
496 | ||
497 | if !unicode.IsUpper(rune(fld.Name[0])) { | |
498 | continue | |
499 | } | |
500 | ||
501 | if partial { | |
502 | ||
503 | _, ok = includeExclude[errPrefix+fld.Name] | |
504 | ||
505 | if (ok && exclude) || (!ok && !exclude) { | |
489 | // structonly tag present don't tranverseFields | |
490 | // but must still check and run below struct level validation | |
491 | // if present | |
492 | if !isStructOnly { | |
493 | numFields := current.NumField() | |
494 | ||
495 | var fld reflect.StructField | |
496 | var customName string | |
497 | ||
498 | for i := 0; i < numFields; i++ { | |
499 | fld = typ.Field(i) | |
500 | ||
501 | if !unicode.IsUpper(rune(fld.Name[0])) { | |
506 | 502 | continue |
507 | 503 | } |
508 | } | |
509 | ||
510 | customName = fld.Name | |
511 | if v.fieldNameTag != "" { | |
512 | ||
513 | name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0] | |
514 | ||
515 | // dash check is for json "-" means don't output in json | |
516 | if name != "" && name != "-" { | |
517 | customName = name | |
518 | } | |
519 | } | |
520 | ||
521 | v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude) | |
504 | ||
505 | if partial { | |
506 | ||
507 | _, ok = includeExclude[errPrefix+fld.Name] | |
508 | ||
509 | if (ok && exclude) || (!ok && !exclude) { | |
510 | continue | |
511 | } | |
512 | } | |
513 | ||
514 | customName = fld.Name | |
515 | if v.fieldNameTag != "" { | |
516 | ||
517 | name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0] | |
518 | ||
519 | // dash check is for json "-" means don't output in json | |
520 | if name != "" && name != "-" { | |
521 | customName = name | |
522 | } | |
523 | } | |
524 | ||
525 | v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude) | |
526 | } | |
522 | 527 | } |
523 | 528 | |
524 | 529 | // check if any struct level validations, after all field validations already checked. |
589 | 594 | typ = current.Type() |
590 | 595 | |
591 | 596 | if typ != timeType { |
592 | ||
593 | // required passed validation above so stop here | |
594 | // if only validating the structs existance. | |
595 | if strings.Contains(tag, structOnlyTag) { | |
596 | return | |
597 | } | |
598 | ||
599 | v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude) | |
597 | v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude, strings.Contains(tag, structOnlyTag)) | |
600 | 598 | return |
601 | 599 | } |
602 | 600 | } |