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 an array of unique strings
Kind Assertion
Applies To Object
Base 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 []
Annotation None
Affected By None
Affects None
Also See

The required keyword restricts object instances to define the given set of properties.

Remember that JSON Schema is a constraint-driven language. Therefore, non-object instances successfully validate against this keyword. If needed, make use of the type keyword to constraint the accepted type accordingly.

Examples

A schema that constrains object instances to define certain properties Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "required": [ "foo", "bar", "baz" ]
}
Valid An object value that defines the required properties to any values is valid Instance
{ "foo": 1, "bar": 2, "baz": 3 }
Valid An object value that defines a superset of the required properties is valid Instance
{ "foo": 1, "bar": 2, "baz": 3, "extra": true }
Invalid An object value that only defines a subset of the required properties is invalid Instance
{ "foo": 1, "bar": 2, "extra": true }
Invalid An empty object is invalid Instance
{}
Valid A non-object value is valid Instance
"Hello World"
A schema that constrains object instances to define certain properties and to describe their value Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "required": [ "name", "age" ],
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  }
}
Valid An object value that defines the required properties and matches their definition is valid Instance
{ "name": "John Doe", "age": 30 }
Annotations
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
Valid An object value that defines a superset of the required properties and matches their definition is valid Instance
{ "name": "John Doe", "age": 30, "extra": true }
Annotations
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
Invalid An object value that only defines a subset of the required properties and matches their definition is invalid Instance
{ "name": "John Doe" }
Invalid An object value that defines the required properties but does not match their definition is invalid Instance
{ "name": 123, "age": "foo" }
Invalid An empty object is invalid Instance
{}
Valid A non-object value is valid Instance
"Hello World"