Closed arafatazam closed 4 years ago
0 is considered as an empty value. You should add a Required
rule to make sure the value is not empty.
I'm not sure whether this works out. Consider I have an integer and I want to validate it is inside a given slice, like []int{0,23,42}
. Also applying Required
doesn't help either, because then I would exclude the value entirely.
How to implement this validation if the zero integer is to be considered a non-empty value? This is surely a good behavior for some cases, but blocking other valid use cases.
For your example, you can use an In(23, 42)
rule. This will let 0, 23, or 42 pass the validation and nothing else.
To add on to this: I'm using ozzo-validation to check a limit parameter being passed to my API. The limit determines how many results I should return to the user, and has type *int
- it's okay for this parameter to be nil
(in which case I can set it to a default value), but if it is explicitly passed by the user, then 0 isn't an acceptable input.
NotIn(0)
doesn't work either, as NotIn
does the same nil/zero check as Min
. Perhaps a good solve to this would be an explicit check in NotIn
that specifically denies zero values if they're the arg to NotIn
- ie
validation.ValidateField(0, validation.NotIn(0))
Would return an error.
I have an int field which needs to be at least 1. I am using validation.Min(1) for validation but, the validation is letting 0 (zero) to pass.