What is the difference between "required" vs "optional" in JSON Schema

Cory picture Cory · Apr 25, 2013 · Viewed 45.9k times · Source

Sometimes, I noticed the following JSON Schemas:

{
    "type": "object",   
    "properties": {
        "address": {
                   "type": "string",
                   "required": true
            }
     }

}

vs

{
    "type": "object",   
    "properties": {
        "address": {
                   "type": "string",
                   "optional": false
            }
     }

}

So what is the difference between required vs optional in the above example?

Answer

Peace Makes Plenty picture Peace Makes Plenty · Jan 6, 2014

The IETF draft v4 of the JSON schema only defines required and does not include optional.

To quote the section on required from draft v4:

Valid values: The value of this keyword MUST be an array. This array MUST have at least one element. Elements of this array MUST be strings, and MUST be unique.

Conditions for successful validation: An object instance is valid against this keyword if its property set contains all elements in this keyword's array value.

In effect, using required makes optional all properties for which the name is not included in the given array of strings.