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.

Value This keyword must be set to a non-empty array of unique strings
Kind Assertion
Applies To Object
Dialect 2020-12
Changed In Draft 4
Introduced In Draft 3
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.3
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Official Tests draft2020-12/required.json
Default As if it was set to the (invalid) value: []
Annotation None
Affected By None
Affects None
Also See

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

Examples

Schema with the 'required' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": [ "foo" ]
}
Valid An instance with all the required properties is valid Instance
{ "foo": "bar" }
Invalid An instance with missing required properties is invalid Instance
{ "bar": false }
Valid 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" ]
}
Valid An instance with all the required properties is valid Instance
{ "name": "John", "age": 65 }
Invalid An instance with missing required properties is invalid Instance
{ "name": "Doe" }
Invalid 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" ]
}
Valid An instance with all the required properties is valid Instance
{
  "name": "John",
  "address": {
    "city": "New York",
    "country": "USA"
  }
}
Invalid 'name' property is missing in the root object Instance
{
  "address": {
    "city": "Hyderabad",
    "country": "India"
  }
}
Invalid '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.