asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.02k stars 556 forks source link

Test case for map validation fails sometimes #380

Closed gabriel-vasile closed 4 years ago

gabriel-vasile commented 4 years ago

Running tests with go test, sometimes results in map validation error:

--- FAIL: TestNestedStruct (0.00s) validator_test.go:3135: Got Error on ValidateMap(map["Nested":map["EvenMoreNested":map["Bar":""] "Foo":"123" "MapEvenMoreNested":map[] "SliceEvenMoreNested":[{"123456"}]]], map["Nested":map["EvenMoreNested":map["Bar":"length(3|5)"] "Foo":"length(3|5),required" "MapEvenMoreNested":"" "SliceEvenMoreNested":""]]) of {{"123" {""} [{"123456"}] map[]}}. Expected: Nested.EvenMoreNested.Bar: does not validate as length(3|5);Nested.SliceEvenMoreNested.0.Bar: 123456 does not validate as length(3|5) Actual: Nested.SliceEvenMoreNested.0.Bar: 123456 does not validate as length(3|5);Nested.EvenMoreNested.Bar: does not validate as length(3|5) FAIL exit status 1

I'm not sure what is the cause for this. First thing that came to my mind was maybe the map validation is not thread safe, but then I tested with go test -p 1, which disables parallelism when running tests, and the error still occured. Next thing I can think of is the non-deterministic order of maps when iterated. @mie00, maybe you could provide some knowledge on this.

mie00 commented 4 years ago

Hi @gabriel-vasile

It seems that this error happens due to an out of order comparison between actual and expected errors, this only has an impact when validation results in multiple errors.

I have created a PR implementing a proposed fix, that is sorting the errors when stringifying, ensuring a consistent ordering. It also can be fixed by comparing individual errors instead, leaving the current implementation as is.