json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

`validation_errors` returns `[]` after running validation

Open exterm opened this issue 2 years ago • 1 comments

It seems that in version 4.1.1, JSON::Validator#validation_errors always returns []. I think it's due to the @errors attribute being reset during validation: https://github.com/voxpupuli/json-schema/blob/76ea712a7fb444b738abbdc35295a6ccbd8d69e4/lib/json-schema/validator.rb#L126 .

To reproduce:

schema = { type: "string" }

v = JSON::Validator.new(schema, record_errors: true)
v.validate(1)
v.validation_errors

We previously used validation_errors to get to the error objects and be able to call .message on them, which yields a nicer error message for anonymous schemas (without URI). Now that seems no longer possible, and in addition the validation_errors method seems useless.

exterm avatar Oct 25 '23 14:10 exterm

Our problem would be solved by either restoring the way record_errors worked before, or adding an option that makes validate return the error objects, or by adding an option to omit the (generated) schema URI from the error messages.

I'm happy to put up a PR but am not sure which solution is preferred.

exterm avatar Oct 25 '23 14:10 exterm