pandastrike/jsck

Speculative error reporting for anyOf / oneOf

Opened this issue · 5 comments

cxreg commented

Would you be interested in optional behavior that allows JSCK to provide more specific errors when anyOf or oneOf fails? I have a proof of concept patch that accomplishes this. The basic idea is to simply increment a counter when a test is executed. The schema with the highest count wins. In practice this means that the schema that was the closest match reports its errors.

The motivation for this is that I'm writing a module to produce user-readable errors from the error structure returned by JSCK and this is the only real thing in the way.

I have not figured out where I would put the option, as the constructor currently takes no arguments besides the schema, and it doesn't really belong in there. Perhaps a second optional argument could be passed that includes options, of which this is one? Maybe something like:

var validator = new JSCK(schema, { "anyOfClosestMatch": true });
cxreg commented

It looks like the constructor is variadic (I'm not sure why I'd want this) but it will probably interfere with my proposed interface. Possibly another way of doing this would be a method on the object, preferably that returns the object itself so it can be done in-line

var validator = new JSCK(schema).options({ "anyOfClosestMatch": true });

Very interested. Also in any other improvements you think would better the error reports.

It looks like the constructor is variadic (I'm not sure why I'd want this)

Allowing for multiple schema documents was the workaround for not having remote refs. Variadic was possibly a bad idea.

Just popping in to note that jsonschema has a similar feature that can be enabled by passing the option nestedErrors: true.

https://github.com/tdegrunt/jsonschema#results

The "simple" error message leverages the title of the subschemas to state that the object in question doesn't not match anyOf/oneOf the subschemas (listed by their title).

When nestedErrors is true, it also prints the errors generated by validating the data against each of the anyOf subschemas. (Which is overly verbose, since it doesn't run any heuristics to validate against the closest matching schema, but rather, all of them. But still, as an opt-in it's useful.)

TODO: write some tests for the options.closestMatch behavior