json-schema-validator
json-schema-validator copied to clipboard
OneOf Validator Gives Unexpected Errors
Let's suppose that we have a schema like the one below:
{
"type": "object",
"oneOf": [
{
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"pattern": "^Name1$"
}
}
},
{
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"pattern": "^Name2$"
}
}
},
{
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"pattern": "^Name3$"
}
}
}
]
}
Let's also suppose that we have the below json to validate:
{
"name": "UnknownName"
}
If we were to validate the described json against the schema, then the validator will fail successfully. However, we'll also get 3 errors that don't quite make sense in this sort of situation.
These are the errors that we'd get:
[
"$.name: does not match the regex pattern ^Name1$",
"$.name: does not match the regex pattern ^Name2$",
"$.name: does not match the regex pattern ^Name3$"
]
The problem with these errors is that "$.name" only needs to match one of those patterns, but every single error encountered underneath the oneOf is reported.
It almost seems like the ValidationMessage should be a tree structure. The root validation problem has to do with the oneOf validation. However, there are child errors that caused the oneOf to fail. These child errors could be grouped together for each of the schemas within the oneOf.
[
{
"message" : "$ must match at least one of the valid schemas",
"children" : [
[{ "message" : "$.name: does not match the regex pattern ^Name1$" }],
[{ "message" : "$.name: does not match the regex pattern ^Name2$" }],
[{ "message" : "$.name: does not match the regex pattern ^Name3$" }],
]
]
Maybe this isn't too big of a deal, but it still seems a little weird to me. If it's not that big of deal then we can just close this issue. I just thought I'd bring it up so that it can be discussed.
I like you idea that the error messages in a tree structure. One of, Any of and AllOf were added recently and they have context to pass to the children. We have put a lot of effort to handle the relationship in the validation but never thought about the error messages yet. To convert the error message to tree structure will make the context and relationship very clear. Would you be able to submit a PR? Thanks.
@stevehu I can look into it when I get a chance, but I've been pretty busy lately. I'll keep you updated.
Completed in #720