ExodusMovement / schemasafe

A reasonably safe JSON Schema validator with draft-04/06/07/2019-09/2020-12 support.
https://npmjs.com/@exodus/schemasafe
MIT License
155 stars 12 forks source link

Validation of schema with discriminator property #141

Closed Xbloud closed 1 year ago

Xbloud commented 3 years ago

I use the OpenAPI's discriminator property in my JSON schemas and I get the following error when compiling schemas: ERROR Error: [discriminator]: need propertyName, oneOf OR anyOf at ConfigBase# It seems that your discriminator property checking does not cover all valid options. Is there any option to disable the "discriminator" property checking/handling?

JSON schemas: "Base" : { "type" : "object", "properties" : { "description" : { "type" : "string", }, "dp" : { "type" : "string", } }, "discriminator" : { "propertyName" : "dp" } },

"IndividualBase" : { "type" : "object", "allOf" : [ { "$ref" : "Base#" }, { "type" : "object", "properties" : { "key" : { "type" : "string", } } } ] },

jchook commented 3 years ago

Your issue may relate to support for only a strict subset of discriminator.

This may not relate to your problem, but I wanted to note that OpenAPI 3.0.x does not match JSON Schema exactly and you must first convert it using e.g. openapi-schema-to-json-schema. You must also use the draft04 JSON Schema spec.

ChALkeR commented 1 year ago

Sorry, I missed this issue.

Is there any option to disable the "discriminator" property checking/handling?

The problem is that the behavior of discriminator is underspecified, and outside the strict subset mentioned above, that behavior is non-obvious.

Disabling coherence checks on schema could lead to unexpected and/or unspecified behavior.

More specifically: in the schema given above, the behavior apparently expected from the allOf usage contradicts the upstream JSON Schema spec. It should have been anyOf or oneOf.

As a rule of thumb, everything that passes with discriminator should also pass without it, which is something this schema does not follow.

Covering underspecified or contradictory cases of discriminator is not planned.

ChALkeR commented 1 year ago

Closing as answered, please feel free to comment if there are more questions.