$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.

Kind Reserved Location
Applies To Any
Dialect 2020-12
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
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; its value must be a valid JSON Schema.

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
    }
  }
}
The instance has a valid "age" property that meets the requirement specified in the "/$defs/positiveInteger" subschema Instance
{ "age": 25 }
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 }
      }
    }
  }
}
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 }
]
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" }
      }
    }
  }
}
Instance with the required properties is valid Instance
{
  "title": "Fundamental Physics",
  "author": {
    "name": "John Doe",
    "age": 55
  }
}
'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.