Skip to content

Improve error messages when oneOf fails because of constraint-driven type-specific keywords #411

@jviotti

Description

@jviotti

Consider this schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "oneOf": [
    { "required": [ "foo" ] },
    { "required": [ "bar" ] },
    { "required": [ "baz" ] }
  ]
}

And this instance:

[ { "foo": 1 } ]

Trace validation gives us this:

$ jsonschema validate schema.json instance.json --trace
-> (push) "/oneOf" (LogicalXor)
   at ""
   at keyword location "file:///Users/jviotti/Projects/playground/schema.json#/oneOf"
   at vocabulary "https://json-schema.org/draft/2020-12/vocab/applicator"

<- (fail) "/oneOf" (LogicalXor)
   at ""
   at keyword location "file:///Users/jviotti/Projects/playground/instance.json#/oneOf"
   at vocabulary "https://json-schema.org/draft/2020-12/vocab/applicator"

We have no indication on which branches match or didn't match the oneOf at all. Also no indication that required worked because of the constraint-driven nature of JSON Schema.

It would have been nice to get something like this instead:

$ jsonschema validate schema.json instance.json --trace
-> (push) "/oneOf" (LogicalXor)
   at ""
   at keyword location "file:///Users/jviotti/Projects/playground/schema.json#/oneOf"
   at vocabulary "https://json-schema.org/draft/2020-12/vocab/applicator"

-> (push) "/oneOf/0/required"
   at ""

-> (pass) "/oneOf/0/required"
   at ""
   warning: Passed because of a type mismatch

-> (push) "/oneOf/1/required"
   at ""

-> (pass) "/oneOf/1/required"
   at ""
   warning: Passed because of a type mismatch

-> (push) "/oneOf/2/required"
   at ""

-> (pass) "/oneOf/2/required"
   at ""
   warning: Passed because of a type mismatch

<- (fail) "/oneOf" (LogicalXor)
   at ""
   at keyword location "file:///Users/jviotti/Projects/playground/instance.json#/oneOf"
   at vocabulary "https://json-schema.org/draft/2020-12/vocab/applicator"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions