-
Notifications
You must be signed in to change notification settings - Fork 65
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
Improve pretty error messages when schema composition constraints are violated #143
Comments
Hi @babelfish, thanks for taking the time to open an issue. What error message would you like to see in this case? Do you mind providing a pared down schema and example data?
Here's your example using {"valid"=>false,
"keywordLocation"=>"",
"absoluteKeywordLocation"=>"json-schemer://schema#",
"instanceLocation"=>"",
"error"=>"value at root does not match schema",
"errors"=>
[{"valid"=>false,
"keywordLocation"=>"/allOf/1/oneOf",
"absoluteKeywordLocation"=>"json-schemer://schema#/allOf/1/oneOf",
"instanceLocation"=>"",
"error"=>"value at root does not match exactly one `oneOf` schema"},
{"valid"=>false,
"keywordLocation"=>"/allOf/1/oneOf/0/not",
"absoluteKeywordLocation"=>"json-schemer://schema#/allOf/1/oneOf/0/not",
"instanceLocation"=>"",
"error"=>"value at root matches `not` schema"},
{"valid"=>false,
"keywordLocation"=>"/allOf/1/oneOf/1/not",
"absoluteKeywordLocation"=>"json-schemer://schema#/allOf/1/oneOf/1/not",
"instanceLocation"=>"",
"error"=>"value at root matches `not` schema"}]} And {"valid"=>false,
"keywordLocation"=>"/allOf/1/oneOf",
"absoluteKeywordLocation"=>"json-schemer://schema#/allOf/1/oneOf",
"instanceLocation"=>"",
"error"=>"value at root does not match exactly one `oneOf` schema",
"errors"=>
[{"valid"=>false,
"keywordLocation"=>"/allOf/1/oneOf/0/not",
"absoluteKeywordLocation"=>"json-schemer://schema#/allOf/1/oneOf/0/not",
"instanceLocation"=>"",
"error"=>"value at root matches `not` schema"},
{"valid"=>false,
"keywordLocation"=>"/allOf/1/oneOf/1/not",
"absoluteKeywordLocation"=>"json-schemer://schema#/allOf/1/oneOf/1/not",
"instanceLocation"=>"",
"error"=>"value at root matches `not` schema"}]} |
With the simpler example: properties:
a:
type: string
b:
type: string
oneOf:
- required: [a]
not:
required: [b]
- required: [b]
not:
required: [a] a: foo
b: bar I'm not entirely sure what the best way to handle it would be. Generating helpful, human-readable error messages when dealing with root-level composition keywords does seem like a daunting task. Ideally I'd be able to get an error message along the lines of "properties I've been using Currently it looks like I'll have to try to programmatically determine when json_schemer will be unable to provide a good error message and override it with one created specifically for our schema, but it's an unsatisfying and brittle solution. |
Yeah, it's a tough problem to solve in a generic way. It comes up enough, though, that I would like to have a better solution to offer. One approach I've thought about is a custom keyword that would override the error message, eg:
And you could also provide keyword-specific errors by defining The other approach I've considered is somehow using i18n to lookup error messages. That has the advantage of being locale-specific. I haven't thought this one through completely yet. Do either of those approaches seem like they would work for your use case? |
The first does seem reasonable, though I would have to experiment to make sure it doesn't cause problems. I'm using OAS 3.1 with API docs hosted by redocly and I don't know offhand how it handles unknown keywords. |
Yeah, I think the Redocly apparently doesn't handle these keywords well in the first place, but that's another problem for me to deal with. |
Is that something you think you could implement in the short term? I'm needing to decide whether to wait for a solution here or implement a custom one. |
@babelfish I opened a PR with |
I switched to that branch and started writing some test cases, and I think this'll meet my needs perfectly. Thanks so much. I am leaving one potential suggestion on the PR. |
Thanks for trying it out, @babelfish—glad it's working for you. Let me know if anything comes up. I'll probably merge and release next week. I'll look into your suggestion as well. Seems reasonable to add |
Custom error options were just released in 2.1.0. |
I have a schema where I'm enforcing rules like:
When writing tests I noticed I get really unhelpful error messages when violating these constraints. If I provide data that includes both
c
andd
, for example, I get the following "pretty" error:root is invalid: error_type=not
.The text was updated successfully, but these errors were encountered: