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
Struct experiment #1150
Struct experiment #1150
Conversation
Would you consider adding an opt-in option to enable the This would prevent users that are already using the Something like: type Validate struct {
//...
requiredStructEnabled bool // new field
}
type ValidateOpt func(*Validate)
// Option to enable required tag on nested structs
func EnableRequiredStruct(v *Validate) {
v.requiredStructEnabled = true
}
func New(opts ...ValidateOpt) *Validate {
//...
v := &Validate{
//...
}
for _, opt := range opts {
opt(v)
}
//...
}
func (v *validate) traverseField(ctx context.Context, parent reflect.Value, current reflect.Value, ns []byte, structNs []byte, cf *cField, ct *cTag) {
//...
if ct != nil && ct.tag == requiredTag && !v.v.requiredStructEnabled {
ct = ct.next
}
//...
} |
Yep @MysteriousPotato totally an option :) was meaning to ask what u think of this alt approach that will support or tag etc on structs. will be a few days before I can finish adding the tests and docs back and then add that option. |
@deankarn I think this is clearly an improvement over my implementation. Besides adding One thing though. Looking a bit more into it, I think this may have changed the behavior of Not that it's a problem in and of itself since it doesn't make any sense to do that but considering the recent events, maybe adding some kind of check before calling Unrelated to this MR: |
Nice catch agreed, handled in this commit to preserve old behaviour. |
// This was made opt-in behaviour in order to maintain backward compatibility with the behaviour previous | ||
// to being able to apply struct level validations on struct fields directly. | ||
// | ||
// It is recommended you enabled this as it will be the default behaviour in v11+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the way to set requiredStructEnabled
to 'false'? If this option will be supported by the lib, I assume there should be a way to disable it as well:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it is off by default @phoenix2x
If you mean it will be the default and a way to turn off in v11+, there will not be an option for that or way to opt-out and people will have to fix their bad code using required
incorrectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That make sense, thank you for the explanation:)
PR
This PR does the following:
or
s etc.. which previous implementation did not.required
validation on non-pointer structs to preserve pre-struct level tag validation support.WithRequiredStructEnabled
option to opt-in to this new behaviour, that will become the default in the next major version.