Codebase list golang-github-go-playground-validator-v10 / 27557e4
Merge pull request #208 from go-playground/v8-development Fix issue with structOnly + struct level validation Dean Karn 8 years ago
2 changed file(s) with 69 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
309309 ```go
310310 $ go test -cpu=4 -bench=. -benchmem=true
311311 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
340340 ```
341341
342342 How to Contribute
412412
413413 errs := v.errsPool.Get().(ValidationErrors)
414414
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)
416416
417417 if len(errs) == 0 {
418418 v.errsPool.Put(errs)
439439
440440 errs := v.errsPool.Get().(ValidationErrors)
441441
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)
443443
444444 if len(errs) == 0 {
445445 v.errsPool.Put(errs)
458458 errs := v.errsPool.Get().(ValidationErrors)
459459 sv := reflect.ValueOf(current)
460460
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)
462462
463463 if len(errs) == 0 {
464464 v.errsPool.Put(errs)
469469 }
470470
471471 // 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) {
473473
474474 if current.Kind() == reflect.Ptr && !current.IsNil() {
475475 current = current.Elem()
486486 errPrefix += typ.Name() + "."
487487 }
488488
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])) {
506502 continue
507503 }
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 }
522527 }
523528
524529 // check if any struct level validations, after all field validations already checked.
589594 typ = current.Type()
590595
591596 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))
600598 return
601599 }
602600 }