Closed eugennicoara closed 11 months ago
Exhibit A
- you are missing the dive
tag on your Policies
field, see the documentation, validator does not automatically dive into arrays, slices or maps to allow validation at the top level and then on internal contents.
type FileConfiguration struct {
Policies []PolicyConfig `validate:"omitempty,dive"`
DefaultPolicy PolicyParameters `validate:"required"`
}
Exhibit B
- If I understand the example and you mean the PolicyParameters
within the []PolicyConfig
then same as Exibit A
, missing dive
.
Excellent! I can confirm adding dive
solved the problem.
Thank you @deankarn !
Package version eg. v9, v10:
v10
Issue, Question or Enhancement:
With reference to Exhibit A, the unit test should not pass because the Retry attribute is not present (line A - set to zero value) under Policies. Commenting out the
Retry
attribute under DefaultPolicy (line B) will cause the test to fail as expected.With reference to Exhibit B, the only output after running the unit test is something like
time="2023-10-11T16:33:44-06:00" level=error msg="retry, 53s"
which I think proves it's not even trying to run the validator for PolicyParameters.Why isn't the validator enforced on the embedded type?
Code sample, to showcase or reproduce:
Exhibit A:
Exhibit B: