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

Inconsistent Order of Errors? #260

Closed
jonmchan opened this issue Nov 15, 2016 · 5 comments
Closed

Inconsistent Order of Errors? #260

jonmchan opened this issue Nov 15, 2016 · 5 comments
Assignees
Labels
Milestone

Comments

@jonmchan
Copy link

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

Is there any way to make validator return errors in a consistent order or sort the validation errors? I'm trying to write a test against 3 errors, but the test fails randomly when the order of the errors are returned in different orders each time the test is run.

Code sample, to showcase or reproduce:

type TestStruct struct {
	Name string `binding:"required,alpha"`
	Age string `binding:"number"`
	Zipcode string `binding:"len=5"`
}

func TestHandleValidationError(t *testing.T) {
	var testData = TestStruct{"Jonny5", "sadf", "1"}
	err := binding.Validator.ValidateStruct(testData)

	fmt.Println(err.(validator.ValidationErrors).Error())
	return
}

Return Value Run 1:

Key: 'TestStruct.Zipcode' Error:Field validation for 'Zipcode' failed on the 'len' tag

Key: 'TestStruct.Name' Error:Field validation for 'Name' failed on the 'alpha' tag

Key: 'TestStruct.Age' Error:Field validation for 'Age' failed on the 'number' tag

Return Value Run 2:

Key: 'TestStruct.Name' Error:Field validation for 'Name' failed on the 'alpha' tag

Key: 'TestStruct.Age' Error:Field validation for 'Age' failed on the 'number' tag

Key: 'TestStruct.Zipcode' Error:Field validation for 'Zipcode' failed on the 'len' tag
@deankarn deankarn self-assigned this Nov 16, 2016
@deankarn deankarn added this to the v9 milestone Nov 16, 2016
@deankarn
Copy link
Contributor

deankarn commented Nov 16, 2016

Hey @jonmchan

That is very strange, the errors returned in v9 are in an array and validator is synchronous, so they should always return in the same order.

I will investigate, if you could provide your full test code that would be helpful, just to rule out the reuse of a variable or something.

P.S. it looks like your using validator through gin, could you please confirm that it is actually using v9 and not v8; the default vendored version in gin is v8 and v8 returns a map, so order cannot be guaranteed.

@deankarn deankarn added the bug label Nov 16, 2016
@deankarn
Copy link
Contributor

@jonmchan update,

found the issue, I was storing the field cache in a map (for some reason) converting to an array now; should be fixed within a few hours at most :)

thanks for reporting

@deankarn
Copy link
Contributor

Hey @jonmchan

I've made the appropriate changes in release 9.2.2, please confirm this solves this for you :)

@jonmchan
Copy link
Author

@joeybloggs - you work fast! Yes, I updated my gin validator to v9. I'll test release 9.2.2 tomorrow and close this bug if I can confirm that the order of errors remain static. Thanks!

@jonmchan
Copy link
Author

The test assertions pass everytime! That did the trick! Thanks!

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

2 participants