47

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 }

5 Answers 5

57

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" ] }
}
10
  • 7
    Not required does not mean "must not be present". Jun 9, 2015 at 6:52
  • 1
    @jruizaranguren, are you trying to say this answer is wrong? I can expand on my explanation if it is not clear why this schema answers the question. Jun 10, 2015 at 7:33
  • 2
    You are right. I'm wrong. Not need to expand explanation. Jun 10, 2015 at 7:36
  • 6
    While correct in JSON schema, I share @jruizaranguren's confusion about the logic. (Not the only place in JSON schema, sadly. :/)
    – Raphael
    Jul 5, 2018 at 15:40
  • 3
    @jrf It's funny you ask that, because I just answered that question a couple hours ago and it's not a common question. The "What's up with required-not section should answer your question stackoverflow.com/a/61062869/1320693 Apr 6, 2020 at 18:35
48

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
    }
}
6
  • The most convenient and non-confusing answer for me. The particular pro of it is that it is placed inside properties keyword, along with another properties.
    – 1valdis
    Dec 29, 2018 at 9:44
  • IMO it's not an answer to this particular question, but it's really good.
    – kris_IV
    Dec 5, 2019 at 12:34
  • 3
    A short-hand for ”x”: { ”not”: {} } in newer draft versions would be ”x”: false.
    – Carsten
    Apr 15, 2020 at 16:53
  • @kris_IV Why don't you think this answers the OP's question? This meets the criteria of "must not exist" precisely.
    – silkfire
    Oct 22, 2021 at 9:47
  • 1
    For anyone using it with AJV: if you want custom error messages with AJV validator for explaining why it is not allowed, then you cannot weave that into the shorthand syntax, but the classical "not" method works. Dec 6, 2021 at 12:18
6

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
}
1
  • Actually this should be the solution of the question. 🙂
    – brcebn
    May 18 at 6:43
5

To specify the absence of a field, you can expect it's type to be null.

{
    "type": "object",
    "properties": {
        "x": { "type": "integer" },
        "z": { "type": "null" }

    },
    "required": [ "x" ]
}
1
  • 4
    Is 'null' the same as 'undefined', though? Nov 6, 2019 at 4:11
-2

You can have type null for that particular property :

 z : {
"type": "null"
}
1
  • Works, however, with networknt jason-schema-validator, gives kind of ugly error: [$.<propert name>: array found, unknown expected]. But it is better than 'Boolean schema false is not valid '(refer: comment above)
    – Ironluca
    Feb 25, 2022 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.