I have a simple code using echo as engine and ozzo-validation as request validator.
func (a MyRequest) Validate() error {
return validation.ValidateStruct(
&a,
validation.Field(&a.Value,
validation.Required,
validation.Length(1, 5),
validation.Each(validation.NilOrNotEmpty, validation.In([]string{"true", "false"}),
),
),
)
}
This is the request I send:
{"value":["true"]}
I get this error from In rule:
value: (0: must be a valid value.).
But when I check the values with == and reflect.DeppEqual, values are equal:
fmt.Println(reflect.DeepEqual([]string{"true", "false"}[0], a.Value[0]))
fmt.Println([]string{"true", "false"}[0] == a.Value[0])
output:
true
true
What am I doing wrong here?
>Solution :
Using validation.Each(validation.In([]string{"true", "false"})) will compare each element in the Value slice against the slice provided to validate.In, i.e. []string{"true", "false"}.
Use validation.In("true", "false") to compare each element in the Value slice against the individual values in validate.In.