Overview
A vocabulary is a set of keywords identified by an absolute URI. Vocabularies solve the problem of implementing custom keywords in an interoperable way across different JSON Schema implementations.The Problem
The specification allows implementations to support custom keywords, but this approach has drawbacks:- Support for extension keywords is optional, not required
- No standard mechanism exists for providing this support
- Each implementation that offers custom keyword support does so differently
- Users defining custom keywords face limited or impossible interoperability
The Solution
Vocabularies introduce:- A standard way to define sets of keywords
- A mechanism for implementations to declare support
- Schema-level vocabulary requirements
- Interoperable extension capabilities
The $vocabulary Keyword
The$vocabulary keyword is used in meta-schemas to declare which vocabularies a schema dialect uses.
Structure
The value is an object where:- Keys are vocabulary URIs (absolute URIs)
- Values are booleans indicating whether the vocabulary is required
Required vs Optional Vocabularies
Required vocabularies (true value):
- Implementation must recognize the vocabulary
- Implementation must be able to process all keywords defined in it
- If not supported, implementation must refuse to process the schema
false value):
- Implementation is not required to recognize the vocabulary
- May continue processing the schema anyway
- Unrecognized keywords must be handled as “unknown”
Required vocabularies ensure critical functionality is available, while optional vocabularies allow graceful degradation.
Vocabularies and Dialects
A dialect is the set of vocabularies listed by a meta-schema. It is ephemeral and carries no identifier of its own.Two meta-schemas with different
$id values can share a common dialect if they declare the same set of vocabularies.$schema that contains $vocabulary, it declares that:
- Only keywords defined by the included vocabularies should be processed
- All other keywords are considered “unknown”
- The implementation must handle unknown keywords appropriately
Vocabulary Components
Vocabulary Description Document
A vocabulary is typically defined in a human-readable document that describes:- The keywords included in the vocabulary
- The behavior of each keyword
- How keywords interact with each other
- Examples of usage
Vocabulary Schema
A vocabulary schema provides syntactic validation for the vocabulary’s keywords. It:- Should carry an
$idvalue distinct from the vocabulary URI - Validates keyword values when a schema is validated against its meta-schema
- Is not itself a meta-schema (doesn’t validate entire schemas)
Official JSON Schema Vocabularies
The Core and Validation specifications divide keywords into multiple vocabularies:Core Vocabulary
The Core vocabulary contains keywords that start with$ and are essential for processing any schema:
$schema$id$ref$defs$comment- And others…
Other Standard Vocabularies
Applicator vocabulary:allOf,anyOf,oneOf,notif,then,elseproperties,patternProperties,additionalPropertiesitems,prefixItems,contains
unevaluatedItemsunevaluatedProperties
type,enum,constminimum,maximumminLength,maxLengthpattern- And others…
title,descriptiondefaultexamplesdeprecated,readOnly,writeOnly
Creating Custom Vocabularies
Define the Vocabulary URI
Choose an absolute URI to identify your vocabulary:Write the Vocabulary Description
Create a human-readable document describing:- Purpose of the vocabulary
- List of keywords
- Behavior of each keyword
- Examples
Create a Vocabulary Schema (Optional)
Provide syntactic validation for keyword values:Create or Extend a Meta-Schema
Declare your vocabulary in a meta-schema:Implement the Keywords
Provide implementation code for your keywords. This is implementation-specific and requires custom code.Implementations may provide mechanisms to register keyword handlers, but the exact mechanism is implementation-dependent.
Current Limitations
Unknown Keywords and Unsupported Vocabularies
The current specification’s handling of unknown keywords from optional vocabularies is under discussion. This presents challenges when:- An implementation doesn’t support an optional vocabulary
- The schema contains keywords from that vocabulary
- The behavior in such cases needs clarification
Machine Readability
The vocabulary URI is an opaque value. There’s no standard machine-readable format for:- Listing keywords defined by a vocabulary
- Providing metadata about keywords
- Describing keyword behavior programmatically
Implicit Core Inclusion
The Core vocabulary’s implicit and mandatory inclusion can be confusing:- It’s always included, even if omitted from
$vocabulary - Best practice is to include it explicitly
- The implicit behavior may be refined in future versions
Best Practices
For Vocabulary Authors
- Choose clear URIs: Use URIs that clearly identify the vocabulary purpose
- Provide documentation: Write comprehensive description documents
- Create vocabulary schemas: Help users validate their schema syntax
- Consider interoperability: Design keywords that work well with standard keywords
- Define clear behaviors: Specify how keywords interact with each other
For Schema Authors
- Declare required vocabularies: Use
truefor critical functionality - Use optional carefully: Only use
falsefor truly optional features - Include Core explicitly: Follow best practice of explicitly declaring Core vocabulary
- Test across implementations: Verify vocabulary support in target implementations
For Implementation Authors
- Support vocabulary declaration: Implement the
$vocabularykeyword - Fail on missing required vocabularies: Don’t process schemas when requirements aren’t met
- Handle unknown keywords: Follow specification guidelines for unknown keyword handling
- Provide extension mechanisms: Allow users to register custom vocabulary handlers
- Document supported vocabularies: Clearly state which vocabularies are supported
Future Development
The vocabularies feature is under active development. Areas being explored include:- Better handling of unknown keywords from optional vocabularies
- Machine-readable vocabulary definition formats
- Simplified Core vocabulary inclusion semantics
- Enhanced interoperability guidelines
For the latest discussions and proposals, see the vocabulary-labeled issues in the JSON Schema specification repository.