type Config struct {
EncryptionMode string `yaml:"encryptionMode" validate="oneof=disabled legacyAES"`
SecretKey *config.SecretKeyConfig `yaml:"secretKey"`
}
I want to add validate tags to the SecretKey field, but i only want that to be validated if EncryptionMode == legacyAES, is that possible with the built in funcs? Or even i it possible to build a custom validator to do that generically? or does it have to be done as a struct level validator?
Thanks
You can define a custom struct validation function.
Example: https://github.com/go-playground/validator/blob/v9/_examples/struct-level/main.go#L37
@lbyte00 thanks, we were hoping to avoid doing that. Unlike the example we have dozens of small structs (nested) all over the code, and we are using a singlton validator object (created without visibility to every nested struct) and code standard is pushing us away from using init funcs to register them all.
Alternatively I was thinking about pushing a patch to attempt to cast each struct to a StructLevelValifator interface type to automatically call the validator func without needing registrations,. It sure how that would be received though.
@jdgordon yeah, I don't like this approach of defining custom struct validation functions too, and currently that's the only way that I found (had the same question as yours).
Thanks for the info @jdgordon @lbyte00
currently no there is no built-in functions to do this beyond StructLevel validation at this time.
I'm investigating options to support more expressive validations that have been being requested, without creating a customer syntax or query language, it is a breaking change though:
The honest truth is I don't know when I'll be able to get to these changes as my plate is pretty full.
Most helpful comment
Thanks for the info @jdgordon @lbyte00
currently no there is no built-in functions to do this beyond
StructLevelvalidation at this time.I'm investigating options to support more expressive validations that have been being requested, without creating a customer syntax or query language, it is a breaking change though:
The honest truth is I don't know when I'll be able to get to these changes as my plate is pretty full.