go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.14k stars 1.29k forks source link

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

Open dablelv opened 10 months ago

dablelv commented 10 months ago

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.