required : Array<String>

required

Array<String>

An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

The required keyword is used to specify which properties must be present within an object instance.

  • The value of this keyword must be an array.
  • Elements of this array, if any, must be strings, and must be unique.
  • Omitting this keyword has the same behavior as an empty array.

Examples

Schema with the 'required' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": [ "foo" ]
}
An instance with all the required properties is valid Instance
{ "foo": "bar" }
An instance with missing required properties is invalid Instance
{ "bar": false }
An instance with all the required properties is valid Instance
{ "foo": [ "bar" ], "baz": 13 }
  • It is important to note that when the required properties are not defined in the properties, then the only requirement to make the instance valid is to have those properties present in the instance irrespective of their value’s datatype.
Schema with the 'required' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  },
  "required": [ "name", "age" ]
}
An instance with all the required properties is valid Instance
{ "name": "John", "age": 65 }
An instance with missing required properties is invalid Instance
{ "name": "Doe" }
The value of 'age' property must be an integer Instance
{ "name": "John Doe", "age": "48" }
Schema with the 'required' keyword in nested subschemas Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "address": {
      "type": "object",
      "properties": {
        "city": { "type": "string" },
        "country": { "type": "string" }
      },
      "required": [ "city", "country" ]
    }
  },
  "required": [ "address" ]
}
An instance with all the required properties is valid Instance
{
  "name": "John",
  "address": {
    "city": "New York",
    "country": "USA"
  }
}
'name' property is missing in the root object Instance
{
  "address": {
    "city": "Hyderabad",
    "country": "India"
  }
}
'country' property is missing in the nested object Instance
{
  "name": "Doe",
  "address": {
    "city": "Dallas"
  }
}
Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" }
  },
  "required": [ "name", "age", "name" ]
}
// Schema with duplicate items in the required list is invalid.