voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.55k stars 243 forks source link

`validation_errors` returns `[]` after running validation #506

Open exterm opened 1 year ago

exterm commented 1 year ago

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 commented 1 year ago

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.