Skip to content

Filter anyOf/oneOf object alternative results by the discriminator #175

@jdesrosiers

Description

@jdesrosiers

Let's start with an example.

{
  "anyOf": [
    {
      "type": "object",
      "properties": {
        "type": { "const": "a" },
        "a": { "type": "string" }
      },
      "required": ["type", "a"]
    },
    {
      "type": "object",
      "properties": {
        "type": { "const": "b" },
        "b": { "type": "number" }
      },
      "required": ["type", "b"]
    }
  ]
}

If given {}, we don't know which alternative was intended, so we give the errors for both alternatives.

  • Expected the value to match at least one alternative
    • Alternative 1
      • Missing required properties: "type" or "a"
    • Alternative 2
      • Missing required properties: "type" or "a"

If given { "type": "a" }, we know that the the user intended the first alternative and can collapse the message to the messages for that one schema.

  • Missing required property: "a"

In this case, it's clear that "type" is the discriminator and if "type" matches the const keyword, that's the alternative that's intended. But, there are other more subtle discriminators. The property name can be anything, the schema of a discriminator can be anything, and the discriminator can be more than one property.

We'll define the discriminator as the set of properties defined by all object alternatives. If an object alternative passed all the discriminator property(s), then any other alternatives that don't match the discriminator can be filtered out.

As before if there is only one alternative left, it can be collapsed and the generic anyOf/oneOf message doesn't need to be returned.

Keep in mind that an anyOf/oneOf can be have both objects and non-objects and the discriminator can still be used to filter the object alternatives.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions