tdegrunt / jsonschema

JSON Schema validation
Other
1.82k stars 262 forks source link

add optional onError callback #364

Open osi-jehrlich opened 2 years ago

osi-jehrlich commented 2 years ago

This is an enhancement request + PR in one:

The goal is to stop validating after 200 errors. Instead of hard-coding that number, and instead of a specific option like throwAfter: 200, a generic onError callback can be the building block to enable this.

To stop after 200 errors we could do something like this: onError: () => { if (++count >= 200) { throw ... } }.

awwright commented 2 years ago

I like the idea of using a callback, but we just have to make sure that usage inside "not" and other "non-cascading" keywords doesn't impact the root call. We want most errors to bubble all the way up, just not that one.

awwright commented 2 years ago

Today I'll push a patch release for #363 with no additional changes. Then sometime next week I'll push a feature release that re-uses the errors array, and adds the "onError" callback. Cascading the errors array doesn't seem to save that much memory, but it ensures that non-cascading sub-schemas ("not", "if", "oneOf" etc.) are properly isolated from their parent, which is necessary to add this feature.