Skip to content

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

Open
picone opened this issue Apr 23, 2025 · 0 comments
Open

Is there a tag like len_if #1420

picone opened this issue Apr 23, 2025 · 0 comments

Comments

@picone
Copy link

picone commented Apr 23, 2025

My case:

We should ensure the Values must has value if Enabled is true.

struct Foo {
  Enabled bool
  Values []string `validate:"required_if=Enabled true,dive,oneof=a b c"`
}

If the Values is nil, the validator work well, but if the Values is an empty slice, it can pass the validator.

I've read the docs, I think it work as expected:

For slices, maps, pointers, interfaces, channels and functions ensures the value is not nil. For structs ensures value is not the zero value.

So, I think we should add a tag like len_if?

struct Foo {
  Enabled bool
  Values []string `validate:"len_if=Enabled true 0,dive,oneof=a b c"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant