Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discriminator logic will not dereference schemas before validating preconditions #1554

Closed
ggoodman opened this issue Apr 16, 2021 · 1 comment · Fixed by #1815
Closed

Discriminator logic will not dereference schemas before validating preconditions #1554

ggoodman opened this issue Apr 16, 2021 · 1 comment · Fixed by #1815

Comments

@ggoodman
Copy link

What version of Ajv you are you using?

8.0.5

What problem do you want to solve?

Defer the evaluation of precondition checks on discriminators after dereferencing $refs. Currently, a schema will fail validation if the list of oneOf children contains a referenced schema instead of an embedded schema.

See:

for (let i = 0; i < oneOf.length; i++) {
const sch = oneOf[i]
const propSch = sch.properties?.[tagName]
if (typeof propSch != "object") {
throw new Error(`discriminator: oneOf schemas must have "properties/${tagName}"`)
}
tagRequired = tagRequired && (topRequired || hasRequired(sch))
addMappings(propSch, i)
}

What do you think is the correct solution to problem?

The preconditions for discriminator support should be deferred.

Will you be able to implement it?

I don't think so. 😬

@nabati
Copy link

nabati commented May 9, 2021

Bumping into a similar (?) issue as well.

Even if you embed the schema but you use compound keyword, it will throw a validation error.

Will work:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "discriminator": {
    "propertyName": "moduleType"
  },
  "oneOf": [
    {
      "properties": {
        "moduleType": {
          "const": "cards"
        }
      }
    },
    {
      "properties": {
        "moduleType": {
          "const": "chart"
        }
      }
    }
  ],
  "required": ["moduleType"]
}

Wont work:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "discriminator": {
    "propertyName": "moduleType"
  },
  "oneOf": [
    {
      "allOf": [
        {
          "properties": {
            "moduleType": {
              "const": "cards"
            }
          }
        }
      ]
    },
    {
      "allOf": [
        {
          "properties": {
            "moduleType": {
              "const": "chart"
            }
          }
        }
      ]
    }
  ],
  "required": ["moduleType"]
}

dfeufel added a commit to dfeufel/ajv that referenced this issue Nov 12, 2021
epoberezkin added a commit that referenced this issue Jan 15, 2022
* resolve references before validating the discriminator
fixes #1554

* prettier style

* adjust according to comments, add some doc

* resolve schema from SchemaEnv

* simplify code, change comment

* add import

* Update lib/vocabularies/discriminator/index.ts

* let to conts

* Update lib/vocabularies/discriminator/index.ts

* update error message

* fix regexp in the test

Co-authored-by: Evgeny Poberezkin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants