Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ozzo validation in rule returns error for equal values

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading