I'd like to use oneOf
schemas which differ only by value of xyType
property. I'd like to have two of them: one where the xyType
is set to "1"
and the second one where xyType
is any other value. Can this be done using json schemas?
"oneOf": [
{
"properties": {
"xyType": "enum": ["1"],
"whatever" : "string"
},
"type": "object"
},
{
"properties": {
"xyType": "enum": [], /// NOT "1"?
"whatever" : "string"
},
"type": "object"
}
]
JSON has no enum type. The two ways of modeling an enum would be: An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values.
The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.
allOf: (AND) Must be valid against all of the subschemas. anyOf: (OR) Must be valid against any of the subschemas. oneOf: (XOR) Must be valid against exactly one of the subschemas.
It's often necessary for applications to validate JSON objects, to ensure that required properties are present and that additional validation constraints (such as a price never being less than one dollar) are met. These validations are typically performed in the context of JSON Schema.
There's a not
operator, and the enum
keyword, and you can use them together, like
{
"not": {
"enum": ["1"]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With