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

json schema validator
json schema conditional required
ajv anyof
json schema multiple if then
json schema array
json schema enum
json schema generator
json schema examples

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 }

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" ] }
}

object, Try not to be confused by the two uses of the word "object" here: Python uses the word object to mean the The value of properties is an object, where each key is the name of a property and each value is a This is valid, since the additional property's value is a string: In Draft 4, required must contain at least one string. The value of properties is an object, where each key is the name of a property and each value is a JSON schema used to validate that property. For example, let’s say we want to define a simple schema for an address made up of a number, street name and street type:

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
    }
}

Combining schemas, JSON Schema includes a few keywords for combining schemas together. To validate against allOf , the given data must be valid against all of the given but what if we wanted to restrict the schema so no additional properties are allowed? schema: it does not behave like inheritance in an object-oriented language. Each property name of this object SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect. Each property value of this object MUST be a valid JSON Schema. This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.

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
}

JSON Schema validation keywords, JSON Schema can be used to require that a given JSON document (an Elements in an array value are said to be unique if no two elements of this array are that are evaluated if the instance is an object and contains a certain property. With this schema, if I try to evaluate a Json that does not contain the "Number" property, it considers that the property is missing ("Non-optional properties are missing from object: Number."). I experienced changing the "required" property into "optional", and the Json is correctly evaluated.

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" ]
}

JSON Schema Validation: A Vocabulary for Structural Validation of , JSON Schema can be used to require that a given JSON document (an instance) Elements in an array value are said to be unique if no two elements of this array An object instance is valid against this keyword if its property set contains all  The JSON specification allows numbers with arbitrary precision, and JSON Schema does not add any such bounds. This means that numeric instances processed by JSON Schema can be arbitrarily large and/or have an arbitrarily long decimal part, regardless of the ability of the underlying programming language to deal with such data.

Application of Brzozowski Derivatives to JSON Schema Validation, The required property excludes objects that do not have all of the indicated properties. The properties property defines constraints for a certain set  As Relequestual correctly ponted out in his comment, JSON Schema makes it hard to 'redefine' type. Moreover, each if-then-else content must be a valid schema on it's own. In order to achieve this effect, you may want to consider following approach: Define questionA as enum, as you did; Leave property for questionB undefined upfront

Creating a schema for a custom object – Zendesk Develop, A schema defines the named properties of a custom object. define validation rules in the schema to maintain the data integrity of The Custom Objects API schema is based on the JSON Schema A schema is a template for creating and validating object records of a certain Have more questions? The value of property is itself a key:value pair, while the value can be any JSON schema and it is used to specify how the value of the key:value pair should look. For example, the following schema specifies that objects should have at least two pairs, with keys first_name and last_name , and the values of those must be strings.

Schema Validation, The specification recommends that all schemas contain $schema properties for this reason. If no $schema property is found, the default validator class is the latest released draft. Lazily yield each of the validation errors in the given instance. The value of properties is an object, where each key is the name of a property and each value is a JSON schema used to validate that property. additionalProperties can restrict the object so that it either has no additional properties that weren’t explicitly listed, or it can specify a schema for any additional properties on the object.

Comments
  • Not required does not mean "must not be present".
  • @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.
  • You are right. I'm wrong. Not need to expand explanation.
  • While correct in JSON schema, I share @jruizaranguren's confusion about the logic. (Not the only place in JSON schema, sadly. :/)
  • @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
  • 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.
  • IMO it's not an answer to this particular question, but it's really good.
  • A short-hand for "x": { "not": {} } in newer draft versions would be "x": false.
  • Is 'null' the same as 'undefined', though?