JSON schema - valid if object does *not* contain a particular property

M Miller picture M Miller · May 28, 2015 · Viewed 11.3k times · Source

Is it possible to set up a JSON schema that still allows for additionalProperties but does not match if a very particular property name is present? In other words, I need to know if it's possible to have the exact opposite of the required declaration.

Schema:

{
    "type": "object",
    "properties": {
        "x": { "type": "integer" }
    },
    "required": [ "x" ],
    "ban": [ "z" ] // possible?
}

Match:

{ "x": 123 }

Match:

{ "x": 123, "y": 456 }

Do not match:

{ "x": 123, "y": 456, "z": 789 }

Answer

Jason Desrosiers picture Jason Desrosiers · Jun 5, 2015

What you want to do can be achieved using the not keyword. If the not schema validates, the parent schema will not validate.

{
    "type": "object",
    "properties": {
        "x": { "type": "integer" }
    },
    "required": [ "x" ],
    "not": { "required": [ "z" ] }
}