$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
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 that describes the age of a person Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "age": {
      "$ref": "#/$defs/positiveInteger"
    }
  },
  "$defs": {
    "positiveInteger": {
      "type": "integer",
      "minimum": 0
    }
  }
}
Valid The instance has a valid "age" property that meets the requirement specified in the "/$defs/positiveInteger" subschema Instance
{ "age": 25 }
Invalid A string is not an integer Instance
{ "age": "some_string" }
Schema for product data Schema
{
  "type": "array",
  "minItems": 1,
  "items": { "$ref": "#/$defs/product" },
  "$defs": {
    "product": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "price": { "type": "number", "minimum": 0 }
      }
    }
  }
}
Valid The instance has a valid array of objects with product data as described in the "/$defs/product" subschema Instance
[
  { "name": "T-shirt", "price": 19.99 },
  { "name": "Mug", "price": 8.50 }
]
Invalid The array is empty, violating the "minItems" constraint requiring at least one product Instance
[]
Schema for book details Schema
{
  "$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" }
      }
    }
  }
}
Valid Instance with the required properties is valid Instance
{
  "title": "Fundamental Physics",
  "author": {
    "name": "John Doe",
    "age": 55
  }
}
Invalid 'author' proeprty is required Instance
{
  "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.