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": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": [ "foo" ]
}
{ "foo": "bar" }
{ "bar": false }
{ "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": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": [ "name", "age" ]
}
{ "name": "John", "age": 65 }
{ "name": "Doe" }
{ "name": "John Doe", "age": "48" }
{
"$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" ]
}
{
"name": "John",
"address": {
"city": "New York",
"country": "USA"
}
}
{
"address": {
"city": "Hyderabad",
"country": "India"
}
}
{
"name": "Doe",
"address": {
"city": "Dallas"
}
}
{
"$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.