Skip to content

Commit 5b9542b

Browse files
authored
fix: panics when private field is validated (#1423)
## Fixes Or Enhances `dive`, `omitnil` and time-related validations panic by themselves. panic inside panic happens for most of the checks in case of bad type. `(reflect.Value).Interface()` can't be used if a field is private. `getValue` must be used instead. In case of casting to `string`, `fmt` is smart enough by itself. In case of getting type with `%T`, `(reflect.Value).Type()` can be safely used. Without these changes tests fail with this error: ``` panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered] panic: reflect.Value.Interface: cannot return value obtained from unexported field or method goroutine 22 [running]: testing.tRunner.func1.2({0x10137e740, 0x101416580}) /Users/yan/.go/src/testing/testing.go:1734 +0x1ac testing.tRunner.func1() /Users/yan/.go/src/testing/testing.go:1737 +0x334 panic({0x10137e740?, 0x101416580?}) /Users/yan/.go/src/runtime/panic.go:792 +0x124 reflect.valueInterface({0x10137e740?, 0x140002183a0?, 0x10141c850?}, 0xa0?) /Users/yan/.go/src/reflect/value.go:1495 +0xc8 reflect.Value.Interface(...) /Users/yan/.go/src/reflect/value.go:1484 github.com/go-playground/validator/v10.(*validate).traverseField(0x140001805a0, {0x10141a610, 0x1016cd0a0}, {0x1013ba060?, 0x1400008f550?, 0x17?}, {0x1013a7500?, 0x1400008f550?, 0x10137e980?}, {0x14000190340, ...}, ...) /Users/yan/projects/validator/validator.go:315 +0x222c github.com/go-playground/validator/v10.(*validate).validateStruct(0x140001805a0, {0x10141a610, 0x1016cd0a0}, {0x101377bc0?, 0x1400008f550?, 0x101?}, {0x1013ba060?, 0x1400008f550?, 0x10137e980?}, {0x10141fd28, ...}, ...) /Users/yan/projects/validator/validator.go:72 +0x50c github.com/go-playground/validator/v10.(*Validate).StructCtx(0x140000d03f0, {0x10141a610, 0x1016cd0a0}, {0x101377bc0, 0x1400008f550}) /Users/yan/projects/validator/validator_instance.go:372 +0x324 github.com/go-playground/validator/v10.(*Validate).Struct(...) /Users/yan/projects/validator/validator_instance.go:346 github.com/go-playground/validator/v10.TestPrivateFieldsStruct(0x14000082fc0) /Users/yan/projects/validator/validator_test.go:14181 +0x368 testing.tRunner(0x14000082fc0, 0x101413400) /Users/yan/.go/src/testing/testing.go:1792 +0xe4 created by testing.(*T).Run in goroutine 1 /Users/yan/.go/src/testing/testing.go:1851 +0x374 FAIL github.com/go-playground/validator/v10 1.197s ``` **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers
1 parent f9a5a1f commit 5b9542b

File tree

5 files changed

+88
-71
lines changed

5 files changed

+88
-71
lines changed

0 commit comments

Comments
 (0)