Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speculative error reporting for anyOf / oneOf #98

Open
cxreg opened this issue Nov 1, 2015 · 5 comments · Fixed by #99
Open

Speculative error reporting for anyOf / oneOf #98

cxreg opened this issue Nov 1, 2015 · 5 comments · Fixed by #99

Comments

@cxreg
Copy link
Contributor

cxreg commented Nov 1, 2015

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
Copy link
Contributor Author

cxreg commented Nov 1, 2015

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 });

@automatthew
Copy link
Contributor

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

@automatthew
Copy link
Contributor

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.

@jasonkarns
Copy link

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.)

@automatthew
Copy link
Contributor

TODO: write some tests for the options.closestMatch behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants