-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
xload: Struct level validation #42
Comments
Any kind of validation is already possible. Like tag based or function based can be done after loading the values. What is the advantage of supporting validations inside xload? |
I agree, validations can be done after loading the config as well. Currently, doing manual validation in my code. However, that is explicit validation. Baking this as an optional flow into xload itself can offer implicit behaviour. And it scopes the validation into a well defined function similar to It keeps struct validation closer to struct itself and reduces cognitive load when dealing with multiple validations. This requirement is mostly true for complex structs, made-up example: //go:embed root.crt
var rootCert []byte
type Credentials struct {
PrivateCert *x509.Certificate `vault:"ABC_CERT"`
}
func (c *Credentials) Validate() error {
return c.PrivateCert.CheckSignatureFrom(rootCert)
} I think traversal is a key part of why I would want this in xload and not outside, because xload visits every struct in the config, it is easier for it to call |
Something like https://github.com/go-ozzo/ozzo-validation helps with defining and invoking validations, even for nested structs and complex multi value validation. So the code will look like this:
|
I’m inclined to not add invoking to xload because it would only work for single key value validation, but might be complex for multi value validation. |
Makes sense, let me give github.com/go-ozzo/ozzo-validation a go.
I don't get it, can you give an example? |
Overview
With
xload
, I want to see what option(s) makes sense for doing validation post loading the values into struct and/or nested structs.Example
The
required
keyword from xload isn't enough to handle such use-case, I think having an optionalValidator
interface implementation can be used to check if the object passes custom validation checks while returning up the call stack(after all nested values are loaded), and fail if the validation errors out.Proposal
Add some mechanism to Validate structs at any level, and propagate errors all the way up, if any.
The text was updated successfully, but these errors were encountered: