Closed kklash closed 4 years ago
Currently the discriminator subset supports only const keyword schemas for the discriminating property.
discriminator
const
We should extend this to allow enum keyword schemas as well.
enum
Consider this example schema, which takes an object and asserts the string property key matches the integer property value:
key
value
{ "type": "object", "required": ["key"], "unevaluatedProperties": false, "oneOf": [ { "required": [], "properties": { "key": { "const": "one" }, "value": { "const": 1 } } }, { "required": [], "properties": { "key": { "const": "ONE" }, "value": { "const": 1 } } }, { "required": [], "properties": { "key": { "const": "two" }, "value": { "const": 2 } } }, { "required": [], "properties": { "key": { "const": "TWO" }, "value": { "const": 2 } } } ] }
{ "key": "one", "value": 1 } // OK { "key": "TWO", "value": 2 } // OK { "key": "TWO", "value": 1 } // ERROR { "key": "one", "value": 2 } // ERROR { "key": "foo", "value": 1 } // ERROR
This could easily be simplified by allowing the discriminator to accept enums on the discriminating key property. Then you could do:
enums
{ "type": "object", "required": ["key"], "unevaluatedProperties": false, "oneOf": [ { "required": [], "properties": { "key": { "enum": ["one", "ONE"] }, "value": { "const": 1 } } }, { "required": [], "properties": { "key": { "enum": ["two", "TWO"] }, "value": { "const": 2 } } } ] }
And add more options to the enums, catching any number of "discriminatee's" and validating them against a schema that you only need to declare once.
Mixing const and enum keyword discriminator schemas would be fine, as long as the discriminating property values are unique strings
Currently the
discriminator
subset supports onlyconst
keyword schemas for the discriminating property.We should extend this to allow
enum
keyword schemas as well.Consider this example schema, which takes an object and asserts the string property
key
matches the integer propertyvalue
:This could easily be simplified by allowing the discriminator to accept
enums
on the discriminatingkey
property. Then you could do:And add more options to the
enum
s, catching any number of "discriminatee's" and validating them against a schema that you only need to declare once.Mixing
const
andenum
keyword discriminator schemas would be fine, as long as the discriminating property values are unique strings