JSON schema - valid if object does *not* contain a particular property
I solved the issue by banning additional properties via "additionalProperties": false
but using patternProperties
to allow any property name except the banned one.
{
"type": "object",
"properties": {
"x": { "type": "integer" }
},
"required": [ "x" ],
"patternProperties": {
"^(?!^z$).*": {}
},
"additionalProperties": false
}
There is a simpler approach. Define that if x is present it must not satisfy any schema. By reduction to absurdity x can not be present:
{
"properties" : {
"x" : {
"not" : {}
}
}
}
Update 2020/04/16: As pointed out by @Carsten in a comment, from draft version 05 and above, the proposed schema can be simplified as follows:
{
"properties": {
"x": false
}
}
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" ] }
}