jhthorsen / json-validator

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

Add clearer error when more than one oneOf rule matched #184

Closed karenetheridge closed 4 years ago

karenetheridge commented 4 years ago

fixes #164.

karenetheridge commented 4 years ago

Would it be possible to let the user know which rules that failed?

Yes, that should be possible with a bit of juggling. I'll push an update.

jhthorsen commented 4 years ago

Don’t do it if it’s a lot of hassle/lines of code.

karenetheridge commented 4 years ago

It would be a few lines of code -- we can get all the failure rules numbers by doing

-    push @expected, $schema_type;
+    push @expected, [$i, $schema_type];

and then @failed = sort {$a <=> $b} map $_->[0], @errors, @expected, but then we need to subtract that list from 0..@rules-1 to get the rules that passed. I don't see how to do it without at least one extra local variable :)

karenetheridge commented 4 years ago

I pushed another commit that uses the indexes of the matching oneOf rules, which you can squash if you like it, or ignore if you don't :)

karenetheridge commented 4 years ago

I force-pushed a much nicer change for adding passing indexes. :)

karenetheridge commented 4 years ago

and done!