$ref keyword is an applicator that references a statically identified schema. The results of evaluating the current location are the results of the referenced schema.
Syntax
A string that is an IRI reference. When resolved against the current base IRI, it produces the IRI of the schema to apply.
Purpose
The$ref keyword enables:
- Schema Reuse: Reference the same schema from multiple locations
- Modular Design: Split large schemas into manageable, focused components
- Recursive Schemas: Create schemas that reference themselves
- Cross-Document References: Reference schemas in other documents
Usage
Fragment References (Same Document)
Reference schemas within the same document using JSON Pointer fragments:Plain Name Anchors
Reference schemas using$anchor plain name fragments:
#item resolves to the schema with $anchor: "item".
External References
Reference schemas in other documents:- Absolute reference:
https://example.com/category.json - Relative reference:
manufacturer.json(resolves tohttps://example.com/manufacturer.json)
External Reference with Fragment
Examples
Recursive Schema
# refers to the root schema, creating a recursive tree structure.
Reusable Definitions
Cross-Schema References
common.json:Reference Resolution
Resolution Process
- Parse: Parse the
$refvalue as an IRI reference - Resolve: Resolve it against the current base IRI per RFC 3986
- Locate: Locate the referenced schema resource
- Apply: Apply the referenced schema to the current instance location
Current Base IRI
The base IRI for resolving$ref is determined by:
- The
$idof the closest parent schema that has one - The document retrieval IRI if no
$idis present - Implementation-specific default
Adjacent Keywords
Other keywords can appear alongside$ref in the same schema object:
description and examples apply to the same location as $ref. All keywords in the object are evaluated.
Network Considerations
Identifiers, Not Locators
The resolved IRI from$ref is an identifier, not necessarily a network locator. A schema need not be downloadable from the IRI if it’s a network-addressable URL.
Offline Operation
Implementations which can access the network SHOULD default to operating offline. Schemas should be pre-loaded or registered rather than fetched on-demand.Error Handling
Failed Reference Resolution
If a reference cannot be resolved, evaluation MUST:- Halt and return an indeterminate result
- NOT return a passing or failing validation result
- NOT return any annotations
- Inform the consuming application of the failure (e.g., raise an exception)
Optimization Considerations
With certain optimizations, unresolvable references might be skipped:Related Keywords
$id- Identifies schemas that can be referenced$defs- Common location for reusable schemas$dynamicRef- Dynamic reference resolution$anchor- Creates plain name fragment identifiers for referencing