validator icon indicating copy to clipboard operation
validator copied to clipboard

Using required_with tag for a slice field, but need it's length greater than 0 if it is not nil, how write tag?

Open dablelv opened this issue 1 year ago • 0 comments

Below is my request struct.

type PostLicenseReq struct {
    DroneModel string 
    DroneSns   []string `validate:"required_with=DroneModel"`
}

I want the DroneSns length greater than 0 when it is not nil. How to write the tag?

I found write like below but doesn't work, because it must require the DroneSns isn't nil while DroneSns can be nil.

type PostLicenseReq struct {
    DroneModel string 
    DroneSns   []string `validate:"required_with=DroneModel,gt=0"`
}

Any one can help me, thanks in advance.

dablelv avatar Aug 25 '23 07:08 dablelv