-
Notifications
You must be signed in to change notification settings - Fork 61
Error messages #16
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
Comments
I think that regardless of option, the goal here should be to produce the most human readable error message and path. With that in mind: 1)I agree that in the case of anyOf the error can be a ambiguous. When it comes to usage in API this is partially mitigated by the fact that the schema becomes public, as it's part of the (ideally OpenAPI) spec. This option is clearly the easiest to implement and could be a good first version. 2)On complex schemas, this level of array nesting can become significant and hard to follow, but I think it's the most complete solution. There's no way to avoid nesting if you want to get the errors for each subschema. 3)Initially I was put off by this option, because it requires the schema designer to follow a convention (which means existing schemas need to be refactored just so they can make use of this type of validation). But given the difficulty in expressing an "ideal" message at point 2 in the case of complex subschemas, using the reference as an alias to refer to the entirety of the subschema doesn't seem that bad. The's still the issue of actually knowing what failed for each of the subschemas (in case of single field schemas that's easy, but when they are complex, knowing the path of the troublesome field and it's own error would be helpful). 4)Without the metadata 5)Another option would be to take option 2) and flatten it. There's a similar discussion about this option in this redux form issue. In the json schema case, the key of the error array would probably be the path. Personally I'm not really sold on this option, but I'm mentioning it for completeness. |
Please check json-schema-org/json-schema-spec#679 and json-schema-org/json-schema-spec#643 to meet standardized validation result format. |
Please consider refactoring the code so that localization of error messages is possible. |
Hi there. I made an attempt to write some sort of error-presenting library. Currently it has several presenting approaches and localization support. |
In |
I'm writing this because there is a demand for error messages (see #14 and #15) and I'm not sure
which way is the best. There are multiple ways of handling error messages and I'm pretty sure that they are different enough to require different implementations, and I need some feedback.
1)
One way is the json-guard's approach - a flat array of errors. But this approach is not so verbose, for example the
anyOf
keyword says that no schema is matched but it doesn't say why every schema failed - because of aminimum
keyword? or maybe atype
keyword?Schema
Data
5
Errors reported by json-guard
2)
The second way will be similar to json-guard for simple keywords, but the errors for keywords like
anyOf
will also contain nested arrays of sub-errors.Here is an example (consider unlimited errors allowed):
Schema
Errors when data is
9
Other optional properties can be:
3)
In the previous approach (2) the problem is that you cannot assign to a schema an error and treat that schema like a simple keyword (some kind of atomicity). Here is what I mean:
Data
Desired error:
As you can see, I don't care how the "digit" schema validates the data, I'm interested only in the result, therefore I want a custom message.
We can add some metadata keywords to solve the problems of the previous approach.
Now, when the
$ref
fails to validate the data, it will check for$error
keyword (in the referenced schema) and use the custom message if exists.4)
There is always room for extending so this approach will tackle the problems for keywords like
pattern
.Here is an example of what I mean:
Schema
Data
"abc"
Desired error:
Current/Default error:
Again, some metadata keyword can be used to replace the default message.
These are the cases that have come to my mind at the moment, probably there are many more.
The text was updated successfully, but these errors were encountered: