-
-
Notifications
You must be signed in to change notification settings - Fork 882
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
Add behavior on predefined keyword (E.X. oneOf) #1113
Comments
Finally, i got that working code
But it is possible expose official API to do that ? That look odd |
I believe discriminator is no-op in the new version of swagger and should only be used to optimise oneOf behaviour (to avoid validating the branches that would fail), but it doesn't change the validation result. Discriminator doesn't really replace oneOf validation, but it shortcuts the branch selection by excluding all branches that are guaranteed to fail (so the implementation above is not correct). You could simply add it as no-op to allow it in strictKeywords mode: ajv.addKeyword('discriminator'); Or you could submit an enhancement PR that introduces option "discriminator" to enable discriminator keyword to optimise the validation of oneOf keyword (to only validate against one branch). Or maybe openAPI option to enable both "nullable" and "discriminator", and then "nullable" option can be deprecated and removed from the next major version. If you go this route, I would probably like to see some extra schema validation step (possibly optional) that would guarantee that only one branch can pass for each allowed discriminator value. And, obviously, tests both to test new behaviour and also to ensure "discriminator" is ignored without the option and all branches are still validated even if it is present. Or it indeed can be a plug-in introducing discriminator by replacing oneOf implementation - but it feels wrong though to replace oneOf - there likely to be some edge cases... |
@epoberezkin My current solution is transpiler discriminator to For reference , my implementation here: |
Going to close this ticket, supporting discriminator seems a separate issue to the ticket title - feel free to submit / implement it though, as suggested. Converting discriminator to custom select seems wrong for various reasons. Extending the behaviour of the standard keywords will not be supported. |
@davidnghk01 I'm not sure, that we have the same use case, but it seems to be very similar. I want to validate request body with OpenAPI-Schema. Validating object has something like The OpenApi doesn't support constants, but proposed solution from the author to add an enum seems to work: openapi: 3.0.0
info:
title: About MyObjects
version: 0.0.1
paths: {}
components:
schemas:
MyObject:
oneOf:
- $ref: "#/components/schemas/MyObjectHidden"
- $ref: "#/components/schemas/MyObjectActive"
discriminator:
propertyName: state
mapping:
HIDDEN: "#/components/schemas/MyObjectHidden"
ACTIVE: "#/components/schemas/MyObjectActive"
MyObjectBase:
properties:
phoneNumber:
type: string
MyObjectHidden:
allOf:
- $ref: "#/components/schemas/MyObjectBase"
- type: object
required:
- state
properties:
state:
type: string
enum:
- HIDDEN
MyObjectActive:
allOf:
- $ref: "#/components/schemas/MyObjectBase"
- type: object
required:
- state
- phoneNumber
properties:
state:
type: string
enum:
- ACTIVE const valid = ajv.validate({$ref: 'openapi.yml#/components/schemas/MyObject'}, body) The generation of |
see #1119 for discriminator keyword |
What version of Ajv you are you using?
6.9.2
What problem do you want to solve?
I would like implement discriminator feature on AJV by
addKeyword
featureBut i get stuck for AJV always report it has error under oneOf statement.
I has try call before validate
it work for disable oneOf checking, but i would like keep the original implementation if discriminator not available, so what should i do ?
What do you think is the correct solution to problem?
Here is my attempt code.
But it not working.
When i used to validate the schema
Schema
code:
it expect should throw error because missing cProp.
But in fact i can't get all error ....
Will you be able to implement it?
The text was updated successfully, but these errors were encountered: