-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Is there a tag like len_if
#1420
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
Comments
It actually does seem that this doesn't work on cases where the slice is not nil, but has no values inside it. My test case looks like: type Foo struct {
Enabled bool
Values []string `validate:"required_if=Enabled true,omitempty,min=1,dive,oneof=a b c"`
}
func TestFoo(t *testing.T) {
val := validator.New()
err := val.Struct(Foo{
Enabled: true,
Values: []string{},
})
assert.Error(t, err)
err = val.Struct(Foo{
Enabled: true,
})
assert.Error(t, err)
err = val.Struct(Foo{
Enabled: false,
})
assert.NoError(t, err)
err = val.Struct(Foo{
Enabled: false,
Values: []string{},
})
assert.NoError(t, err) // This one fails, since omitempty doesn't trigger on slices that have no values inside them.
} I don't like the
Usually the There are like two approaches we could take:
|
@zemzale cant remember off the top of my head but don’t we have a new omitzero tag that could be used in place of omitempty here? |
My case:
We should ensure the
Values
must has value ifEnabled
is true.If the
Values
is nil, the validator work well, but if theValues
is an empty slice, it can pass the validator.I've read the docs, I think it work as expected:
So, I think we should add a tag like
len_if
?The text was updated successfully, but these errors were encountered: