I'm using the package validator to help me deal with JSON fields in golang. I know how to use oneof to check for specific values in a field. Is there any option for doing the same, but for an array of strings?
I'm trying to do something like this:
type ResponseEntity struct {
Values []string `json:"values" validate:"oneof=VALUE1 VALUE2 VALUE3"`
}
As @mkopriva points out, you can use the
divekeyword to check the elements of a slice.To offer more explanation:
minare overloaded and apply to both scalar and slice typesoneofapply only to scalars; (in the example below, a call tovalidate.Structcauses a panic when the struct has aoneofrule directly on a slice)divepushes rules likeoneofdown to the elements of a slice. (the example also showsmin,dive, andoneofworking together without panicking).