-
Notifications
You must be signed in to change notification settings - Fork 556
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
"valid" need to be first parameter? #159
Comments
I'm sorry for my english. In my project it works normally. When placing a tag, you can not use the TAB to indent, the tags should only contain one space between the others. |
i think what avelino means is that if "valid" is not the first struct tag, then govalidator doesn't work properly. example:
this outputs:
then this will give the expected result: |
@colindickson i think the problem is the space between in this example json tag also won't work type Foo struct {
Email string `json: "email" valid:"email"`
}
func main() {
foo := &Foo{"not_an_email"}
fmt.Println(govalidator.ValidateStruct(foo)) // prints => true <nil>
body, _ := json.Marshal(foo)
fmt.Println(string(body)) // prints => {"Email":"not_an_email"}
} Json marshaller uses exposed attribute name The syntax is incorrect so remaining tags won't be processed. But in this example; type Foo struct {
Email string `valid:"email" json: "email"`
}
func main() {
foo := &Foo{"not_an_email"}
fmt.Println(govalidator.ValidateStruct(foo)) // prints => false Email: not_an_email does not validate as email;
body, _ := json.Marshal(foo)
fmt.Println(string(body)) // prints => {"Email":"not_an_email"}
} First tag is processed then syntax error appears. So validation tag works correctly but json marshaller uses exposed attribute name Correct example 1: type Foo struct {
Email string `valid:"email" json:"email"`
}
func main() {
foo := &Foo{"not_an_email"}
fmt.Println(govalidator.ValidateStruct(foo)) // prints => false email: not_an_email does not validate as email;
body, _ := json.Marshal(foo)
fmt.Println(string(body)) // prints => {"email":"not_an_email"}
} Correct example 2: type Foo struct {
Email string `json:"email" valid:"email" `
}
func main() {
foo := &Foo{"not_an_email"}
fmt.Println(govalidator.ValidateStruct(foo)) // prints => false email: not_an_email does not validate as email;
body, _ := json.Marshal(foo)
fmt.Println(string(body)) // prints => {"email":"not_an_email"}
} |
Already resolved, thanks to everybody. |
@asaskevich what does the solution commit? I'd like to understand the error |
Can't find solution commit but there is the line responsible for it: Line 904 in 6c3cb32
Maybe an issue was in https://golang.org/pkg/reflect/#StructTag.Get function that splits the string by space. |
When does not put the "valid" as the first parameter in the struct it does not work.
The text was updated successfully, but these errors were encountered: