jellydator / validation

An idiomatic validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
39 stars 6 forks source link

Min(1) validation rule does not fail when given the value zero integer #7

Closed mixedCase closed 1 year ago

mixedCase commented 1 year ago

Title.

swithek commented 1 year ago

Can you give an example of this issue?

mixedCase commented 1 year ago

validation.Min(1).Validate(0) should logically return an error given that 0 is less than 1, but because 0 is the zero value for the int type, it does not.

A workaround is to use the rule validation.Required in conjunction with validation.Min, of course, but API-wise this doesn't make much sense and breaks the principle of least surprise in a big way.

swithek commented 1 year ago

That's the intended behaviour, as stated in the doc comments: https://github.com/jellydator/validation/blob/f3d1c3d00ac7acf4dbcc16a5c1f75ef39d37edd9/minmax.go#L32-L37

// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.