$defs : Object<String, Schema>
$defs
Object<String, Schema>This keyword reserves a location for schema authors to inline re-usable JSON Schemas into a more general schema.
Value |
This keyword must be set to an object where each value is a valid JSON Schema
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
---|---|
Kind | Reserved Location |
Applies To | Any |
Base 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 is a container for storing re-usable schemas within a
schema resource, which can be referenced using the $ref
or $dynamicRef
keywords. From a software engineering point of view, this keyword is
analogous to defining internal helper functions as part of a larger program.
Best Practice
Use this keyword to reduce duplication of internal declarations within a schema. However, prefer extracting standalone entities that represent more than just internal helpers into separate schema files, and externally referencing them instead. Otherwise, you will end up with big monolithic schemas that are challenging to understand and maintain.
If you need to resolve external references in advance (for distribution or
analysis), look at the jsonschema bundle
command.
Common Pitfall
This keyword declares helper schemas for use within the same schema file or resource. Defining schema files or resources that use this keyword (and typically no other keyword) to group common definitions for other schema files or resources to reference is considered to be an anti-pattern. If you want to share a schema across multiple schema files or resources, that common schema should be a standalone schema file or resource itself.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"firstName": { "$ref": "#/$defs/nonEmptyString" },
"lastName": { "$ref": "#/$defs/nonEmptyString" }
},
"$defs": {
"nonEmptyString": {
"type": "string",
"minLength": 1
}
}
}
{ "firstName": "John", "lastName": "Doe" }
{ "firstName": "", "lastName": "" }