jhthorsen/json-validator

Order in which errors are reported is undefined

davidcantrell-bb opened this issue · 6 comments

  • JSON::Validator version: 4.02
  • Perl version: 5.30.1
  • Operating system: Mac OS Catalina

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.

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?

Sure. Gimme a few days.

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?

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.

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

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