$defs : Object<String, Schema>
$defs
Object<String, Schema>This keyword is used in meta-schemas to identify the required and optional vocabularies available for use in schemas described by that meta-schema.
Value | This keyword must be set to an object where each value is a valid JSON Schema |
---|---|
Kind | Reserved Location |
Applies To | Any |
Dialect | 2020-12 |
Changed In | None |
Introduced In | 2019-09 |
Vocabulary | Core |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.2.4 |
Metaschema | https://json-schema.org/draft/2020-12/meta/core |
Official Tests | draft2020-12/defs.json |
Default |
{}
|
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The $defs
keyword provides a standardized way to define reusable subschemas within a single schema document, promoting modularity, reducing code duplication, and improving schema organization. Each subschema within $defs
has a unique name, acting as a location for referencing, without directly affecting validation.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"age": {
"$ref": "#/$defs/positiveInteger"
}
},
"$defs": {
"positiveInteger": {
"type": "integer",
"minimum": 0
}
}
}
{ "age": 25 }
{ "age": "some_string" }
{
"type": "array",
"minItems": 1,
"items": { "$ref": "#/$defs/product" },
"$defs": {
"product": {
"type": "object",
"properties": {
"name": { "type": "string" },
"price": { "type": "number", "minimum": 0 }
}
}
}
}
[
{ "name": "T-shirt", "price": 19.99 },
{ "name": "Mug", "price": 8.50 }
]
[]
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/books",
"type": "object",
"properties": {
"title": { "type": "string" },
"author": { "$ref": "#author" }
},
"required": [ "title", "author" ],
"$defs": {
"author": {
"$anchor": "author",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
}
}
}
}
{
"title": "Fundamental Physics",
"author": {
"name": "John Doe",
"age": 55
}
}
{
"title": "Fundamental Chemistry"
}
- Note: JSON Pointer isn’t the only way of accessing a subschema. You can also use the
$anchor
keyword to reference a subschema within$defs
.