Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json schema "not in" enum type?

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"
    }
]
like image 211
František Žiačik Avatar asked Jul 24 '15 12:07

František Žiačik


People also ask

Does JSON support enum?

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.

What is the purpose of the enum keyword in JSON Schema?

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.

What is anyOf in JSON Schema?

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.

Does JSON require schema?

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.


1 Answers

There's a not operator, and the enum keyword, and you can use them together, like

{
    "not": {
        "enum": ["1"]
    }
}
like image 102
cloudfeet Avatar answered Oct 17 '22 04:10

cloudfeet