Skip to content

fix: recursively check for validity of discriminator#2597

Open
AmadeusK525 wants to merge 1 commit intoajv-validator:masterfrom
profusion:fix/nested-discriminated-unions
Open

fix: recursively check for validity of discriminator#2597
AmadeusK525 wants to merge 1 commit intoajv-validator:masterfrom
profusion:fix/nested-discriminated-unions

Conversation

@AmadeusK525
Copy link
Copy Markdown

The old implementation was only checking for a single level of validity of a schema using the discriminator extension, without accounting for "unions of unions". So, for example:

{
  "discriminator": {
    "propertyName": "field"
  }
  "required": ["field"],
  "oneOf": [
    {
      "oneOf": [
        {
          "properties": {
            "field": { "const": "a" }
          },
          "required": ["field"]
        },
        {
          "properties": {
            "field": { "const": "b" }
          },
          "required": ["field"]
        }
      ],
      "required": ["field"]
    },
    {
      "properties": {
        "field": { "const": "c" }
      },
      "required": ["field"]
    }
  ]
}

This schema would fail because the compiler would complain about the first schema in oneOf not having the field property, which was defined as the discriminator. But it does have that property, just in a subschema inside another oneOf definition!

@AmadeusK525 AmadeusK525 force-pushed the fix/nested-discriminated-unions branch from 4aba655 to f3ed917 Compare March 10, 2026 22:07
The old implementation was only checking for a single level of validity
of a schema using the discriminator extension, without accounting for
"unions of unions". So, for example:

```json
{
  "discriminator": {
    "propertyName": "field"
  }
  "required": ["field"],
  "oneOf": [
    {
      "oneOf": [
        {
          "properties": {
            "field": { "const": "a" }
          },
          "required": ["field"]
        },
        {
          "properties": {
            "field": { "const": "b" }
          },
          "required": ["field"]
        }
      ],
      "required": ["field"]
    },
    {
      "properties": {
        "field": { "const": "c" }
      },
      "required": ["field"]
    }
  ]
}
```

This schema would fail because the compiler would complain about the
first schema in `oneOf` not having the `field` property, which was
defined as the `discriminator`. But it *does* have that property, just
in a subschema inside another `oneOf` definition!
@AmadeusK525 AmadeusK525 force-pushed the fix/nested-discriminated-unions branch from f3ed917 to 7ccf749 Compare March 10, 2026 22:17
@epoberezkin
Copy link
Copy Markdown
Member

It's debatable it's correct, given how discriminator is specified I think.

@AmadeusK525
Copy link
Copy Markdown
Author

It's debatable it's correct, given how discriminator is specified I think.

@epoberezkin Sorry, but why would this be debatable? The spec may not mention recursion explicitly, but there's no reason to not cover this case, as it's in line with the reasoning behind the discriminator keyword (making sure that every object in the discriminated union has the discriminator property). I really don't see how an implementation that doesn't cover the recursion "edge" case would be more correct than one that doesn't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants