jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

Order in which errors are reported is undefined #222

Closed davidcantrell-bb closed 3 years ago

davidcantrell-bb commented 3 years ago

Steps to reproduce the behavior

Validate some non-conforming data with multiple errors against a JSON schema

Expected behavior

Errors are returned in a defined order

Actual behavior

Errors are returned in an undefined order

That order appears to be consistently ASCIIbetical (sorted by the path to the offending data), both from a quick look at the code (although I didn't go through all of it) and from the results, so please could that be documented. This would make it easier to write and test code that does further processing on those errors.

jhthorsen commented 3 years ago

This sounds like a good first time PR? I guess it could be fixed by adding sort in front of keys where "properties" and "patternProperties" are iterated in lib/JSON/Validator.pm, lib/JSON/Validator/Schema/Draft4.pm and lib/JSON/Validator/Schema/Draft6.pm.

The tricky part might be to write tests for this. Are you up for the task?

davidcantrell-bb commented 3 years ago

Sure. Gimme a few days.

DrHyde commented 3 years ago

Looks like a simpler way to make sure they're sorted would be to sort @errors based on the objects' ->path()s before returning it from validate() would it not?

DrHyde commented 3 years ago

Testing shouldn't be too difficult, just construct a schema and some known-bad data and randomise the order in which things appear in both. Do that a few times, and make sure that the errors are always returned in the same order.

jhthorsen commented 3 years ago

@DrHyde: That could be a good idea, since the number of errors are probably much lower than the number of valid objects.

jhthorsen commented 3 years ago

I'm closing this issue, since #223 will get merged.