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

"valid" need to be first parameter? #159

Closed
avelino opened this issue Oct 14, 2016 · 6 comments
Closed

"valid" need to be first parameter? #159

avelino opened this issue Oct 14, 2016 · 6 comments
Labels

Comments

@avelino
Copy link

avelino commented Oct 14, 2016

When does not put the "valid" as the first parameter in the struct it does not work.

@asaskevich asaskevich added the bug label Jan 3, 2017
@wilsontamarozzi
Copy link

I'm sorry for my english.

In my project it works normally.
https://github.com/wilsontamarozzi/panda-api/blob/master/services/models/people.go#L20

When placing a tag, you can not use the TAB to indent, the tags should only contain one space between the others.

@colindickson
Copy link

colindickson commented Aug 10, 2017

i think what avelino means is that if "valid" is not the first struct tag, then govalidator doesn't work properly. example:

type Foo struct {
	Email string `json: "email" valid:"email"`
}

func main() {
	fmt.Println(govalidator.ValidateStruct(&Foo{"not_an_email"}))
}

this outputs:
true
which is incorrect
but if you fix the ordering of the struct tags to make "valid" first:

type Foo struct {
	Email string `valid:"email" json: "email"`
}

func main() {
	fmt.Println(govalidator.ValidateStruct(&Foo{"not_an_email"}))
}

then this will give the expected result:
false Email: not_an_email does not validate as email;

@emirhanmarlali
Copy link

emirhanmarlali commented Sep 27, 2017

@colindickson i think the problem is the space between json: and "email"

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 Email

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 Email.

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"}
}

@asaskevich
Copy link
Owner

Already resolved, thanks to everybody.

@avelino
Copy link
Author

avelino commented Nov 4, 2017

@asaskevich what does the solution commit? I'd like to understand the error

@asaskevich
Copy link
Owner

Can't find solution commit but there is the line responsible for it:

tag := t.Tag.Get(tagName)

Maybe an issue was in https://golang.org/pkg/reflect/#StructTag.Get function that splits the string by space.

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

No branches or pull requests

5 participants