Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior validating minimums #165

Open
adrianbielsa1 opened this issue Jan 24, 2022 · 3 comments
Open

Unexpected behavior validating minimums #165

adrianbielsa1 opened this issue Jan 24, 2022 · 3 comments

Comments

@adrianbielsa1
Copy link

adrianbielsa1 commented Jan 24, 2022

Hi! I stumbled upon the following situation: validating a number's minimum threshold does not work as intended when said threshold is 0.

The following snippet works as expected, printing "must be greater than 1":

data := float64(1)

err := validation.Validate(data,
    validation.Min(float64(1)).Exclusive(),
)

fmt.Println(err)

However, changing the threshold to 0 nullifies the error:

data := float64(0)

err := validation.Validate(data,
    validation.Min(float64(0)).Exclusive(),
)

fmt.Println(err)

^ prints "<nil>" (i.e. no error).

@call-stack
Copy link

call-stack commented Jan 31, 2022

When you initialize data as 0 it treat is as an empty value when doing the validation. Hence you are getting nil.
Screenshot 2022-01-31 at 10 04 06 PM

@adrianbielsa1
Copy link
Author

Then, how would you validate that a value must be greater than 0?

@call-stack
Copy link

You can do something like this. Since it is treating 0 as empty.

data := float64(0)
err := validation.Validate(data,
	validation.Min(float64(0)).Exclusive(),
	validation.NilOrNotEmpty.Error("must be greater than 0"),
)
fmt.Println(err)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants