Sri Harsha
Sri Harsha

Reputation: 11

Why Json Schema Validators throw many exception messages instead of a simple & summarised reason of validation failure?

Schema :

{
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "x": {"type": "string"},
        "y": {"type": "string"},
        "z": {"type": "string"}
    },
    "oneOf":[{"required": ["x"]}, {"required": ["y"]}, {"required": ["z"]}]
}

Input Json :

{
    "x":"sample",
    "y":"sample"
}

The actual error and exception message expected is : "JSON is valid against more than one schema" but in addition to this, "Required Key [z] not found" is also thrown. I have checked more 3libraries and all are doing the same.

Is there any reason for throwing multiple messages like this? Is there any library(java) which analyse the actual issue and summarise the exception message?

Upvotes: 1

Views: 557

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

The reason you're getting multiple errors is because there are multiple errors.

  • /oneOf/0 and /oneOf/1 both pass, which means that /oneOf fails because only one of its subschemas can be valid. You're getting an error from this.
  • /oneOf/2 fails because z isn't in the instance. You're getting an error from this.

My guess is that if the instance did pass validation, e.g. {"x": "foo"}, the implementation you're using wouldn't report any errors. Or in the most verbose case, it would return a passing overall result but still show you the errors from the failing oneOf subschemas so that you can see which subschema passed and why the others failed.

What you're getting is correct.

If you want something else, you can ask the maintainer of the library or find another one here.

Upvotes: 1

Related Questions