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

Support OpenAPI discriminator keyword together with oneOf #1119

Closed
9 tasks
epoberezkin opened this issue Nov 5, 2019 · 7 comments · Fixed by #1494
Closed
9 tasks

Support OpenAPI discriminator keyword together with oneOf #1119

epoberezkin opened this issue Nov 5, 2019 · 7 comments · Fixed by #1494
Milestone

Comments

@epoberezkin
Copy link
Member

What problem do you want to solve?

  • Optimise oneOf validation in case discriminator keyword is present by only validating the matching branch (currently all branches are validated)
  • Apply defaults inside of oneOf in the presence of discriminator keyword (currently defaults inside oneOf are ignored)

The second can be addressed separately

The solution requirements?

  • requires an option "discriminator" to enable "discriminator" keyword
  • discriminator without oneOf should throw exception at schema compilation in strictKeywords mode, log warning and be ignored otherwise
  • validate that each oneOf branch can only be valid for one discriminator value - TBC, possibly depending on strictKeywords or another option (i.e. that from validation result point of view discriminator remains no-op)
  • apply defaults from the chosen oneOf branch
  • tests:
    • that the current behaviour is not affected (i.e. all oneOf branches are validated in case discrimintator keyword is absent or present without an option/with option false and that the defaults in oneOf are ignored)
    • all the new behaviours

Will you be able to implement it?

Yes, with the right incentive :) PR is welcome.

@thetumper
Copy link

Even without discriminator, couldn't/shouldn't it be possible to disambiguate between various schemas in a oneOf, and validate the sub-elements of the matching schema? By definition, the oneOf should not be valid if there is no differentiating property(ies) between the schemas.

@epoberezkin
Copy link
Member Author

oneOf by definition should fail if the data instance is valid against none or more than one sub-schema. In general case, it is not possible to determine by schema analysis (as you suggest) so ajv has to validate against ALL subschemas when oneOf is used.

@thetumper
Copy link

Gotcha. What I was getting at was that if the property names all line up with one of the schemas, but a value violates a regex pattern, for example, then I would hope to have an error generated indicating that. This actually does seem to be happening. There are also other errors for not matching any of the other schemas. Which is also expected.

I think my confusion was about oneOf matching a schema based solely on which properties exist, vs. whether the values also meet the criteria defined for those properties.

@epoberezkin
Copy link
Member Author

epoberezkin commented Nov 13, 2019 via email

@epoberezkin
Copy link
Member Author

released in v8.0.0-beta.2

@epoberezkin
Copy link
Member Author

v8 now released as the main version: https://ajv.js.org/v6-to-v8-migration.html

@Bessonov
Copy link

Thank you!

andriyl pushed a commit to Redocly/ajv that referenced this issue Jun 16, 2021
* discriminator keyword, ajv-validator#1119 (WIP)

* OpenAPI discriminator, tests, ajv-validator#1119

* docs: discriminator
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