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.
Common Pitfall
The presence of this keyword does not depend on the presence of the
properties
keyword. The required
keyword mandates that certain properties are present
(independently of their value), while the properties
keyword describes the value of such
properties when present.
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
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"required": [ "foo", "bar", "baz" ]
}
{ "foo": 1, "bar": 2, "baz": 3 }
{ "foo": 1, "bar": 2, "baz": 3, "extra": true }
{ "foo": 1, "bar": 2, "extra": true }
{}
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"required": [ "name", "age" ],
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
}
}
{ "name": "John Doe", "age": 30 }
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
{ "name": "John Doe", "age": 30, "extra": true }
{ "keyword": "/properties", "instance": "", "value": [ "name", "age" ] }
{ "name": "John Doe" }
{ "name": 123, "age": "foo" }
{}
"Hello World"