Skip to content

Is there a tag like len_if #1420

@picone

Description

@picone

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"`
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions