Skip to main content
The $defs keyword reserves a location for schema authors to inline reusable JSON Schemas into a more general schema. The keyword does not directly affect validation results.

Syntax

object
required
An object where each property value MUST be a valid JSON Schema. Property names are arbitrary identifiers chosen by the schema author.

Purpose

The $defs keyword provides:
  1. Schema Organization: A standard location for reusable schema components
  2. Reference Target: Schemas that can be referenced with $ref
  3. Documentation: Groups related definitions together for readability
  4. Reserved Location: Ensures this location won’t be redefined by extension keywords

Usage

Basic Example

The schema defines an array of positive integers. The positive integer constraint is defined once in $defs and referenced by the items keyword.

Multiple Definitions

The address schema is reused for both billing and shipping addresses.

Examples

Complex Schema with Nested Definitions

This example shows definitions referencing other definitions (dimensions references positiveNumber).

Recursive Schema

The node definition references itself to create a binary tree structure.

Embedded Schema Resources in $defs

The embedded schema has its own $id and can be referenced as person.json. This is useful for bundling multiple schema resources into a single document.

Referencing Definitions

JSON Pointer Syntax

Definitions in $defs are referenced using JSON Pointer syntax:
  • # refers to the current document root
  • /$defs/ navigates to the $defs object
  • /definitionName selects the specific definition

External References

Definitions can be referenced from other schema documents:
This references the address definition in the external common.json schema.

Validation Behavior

The $defs keyword itself does not affect validation:
In this example, the instance is validated only against "type": "string". The unused definition in $defs has no effect unless referenced.

Naming Conventions

While property names in $defs are arbitrary, common conventions include:
  • camelCase: positiveInteger, emailAddress
  • PascalCase: PositiveInteger, EmailAddress
  • snake_case: positive_integer, email_address
  • Descriptive names: Use clear, meaningful names that describe the schema’s purpose

Comparison with Previous Versions

In older JSON Schema drafts, definitions was used instead of $defs:
The $ prefix was added to align with other core keywords and emphasize its reserved status.

Bundling Schemas

The $defs keyword is the RECOMMENDED location for bundling external schema resources into a compound document:
The keys in $defs (like bundledUser) are for organization and don’t affect references, which use the embedded $id values.
  • $ref - References schemas defined in $defs
  • $id - Can be used within definitions to create embedded resources
  • $anchor - Alternative way to create reference targets