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

There is no "exists" tag anymore and we can not use "required" tag with zero-value #692

Closed
mohsenasm opened this issue Nov 25, 2020 · 2 comments

Comments

@mohsenasm
Copy link

Hi,

We have upgraded from version 8 to version 10 recently.
It seems that there is no exists tag anymore and we should use required instead. But as states in this issue, there is a problem with the required tag that it doesn't allow zero-value.

What do you suggest?

@frederikhors
Copy link
Contributor

frederikhors commented Dec 1, 2020

@mohsenasm can this help you? #691

If yes please close the issue. Thanks.

@mohsenasm
Copy link
Author

Thanks. It's now clear.
So we should use *int if we want to allow zero-value.

type User struct {
	Age *int `validate:"required"`
}

age := 0
user := &User{Age: &age}
err := validate.Struct(user) // err == nil

user2 := &User{Age: nil}
err2 := validate.Struct(user2) // err != nil
type User struct {
	Age int `validate:"required"`
}

age := 0
user := &User{Age: age}
err := validate.Struct(user) // err != nil

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