Open jonathan-innis opened 11 months ago
related to #1142
Nested struct validation was not supported in previous version, hence why no error was returned.
However, support extends only to a few tags including the ones that check against zero values such as required
and excluded_with
. This does not include tags like min
.
If you need more complex validation, you must register your own custom tag. Ex.:
func main() {
type Settings struct {
D *metav1.Duration `validate:"metamin=15m"`
}
v := validator.New()
if err := v.RegisterValidation("metamin", func(fl validator.FieldLevel) bool {
duration, ok := fl.Field().Interface().(metav1.Duration)
if !ok {
return false
}
return v.Var(duration.Duration, "min="+fl.Param()) == nil
}); err != nil {
panic(err)
}
// valid
s1 := Settings{&metav1.Duration{time.Minute * 15}}
if err := v.Struct(s1); err != nil {
panic(err)
}
// invalid
s2 := Settings{&metav1.Duration{time.Minute * 14}}
if err := v.Struct(s2); err != nil {
panic(err)
}
}
Also, it's worth noting that struct fields must be exported in order for them to get validated.
Is this the code you're having an issue with? https://github.com/aws/karpenter/blob/a87ed30c8e60d6be25ce10fb058ad66c6174edab/pkg/apis/settings/settings.go#L57
Example doesn't clearly demonstrate an issue.
@jonathan-innis can we consolidate the conversation here :) https://github.com/go-playground/validator/issues/1142
Package version eg. v9, v10:
Error starts occurring on
v10.15.0
No errors present onv10.14.1
Issue, Question or Enhancement:
When validating a
metav1.Duration
struct, we are receiving an error with the validation that we previously never receivedCode sample, to showcase or reproduce: