What are Annotations?
JSON Schema can annotate an instance with information whenever the instance validates against the schema object containing the annotation. This information can be a simple value or calculated based on the instance contents.Annotations are attached to specific locations in an instance. Unlike assertions, annotation data doesn’t cause validation to fail - it provides metadata for applications to use as they see fit.
How Annotations Work
Annotations are produced by keywords whenever:- The instance validates successfully against the keyword’s schema
- All parent schema objects also validate successfully
- Validation hasn’t been short-circuited by a failed assertion
title→ “Product Name”description→ “The name of the product in the catalog”default→ “Untitled Product”examples→ [“Widget”, “Gadget”, “Gizmo”]
JSON Schema implementations are not expected to make use of collected annotations on behalf of applications. Applications receive the annotation data and decide how to use it.
Annotation Collection
A collected annotation MUST include:- Keyword name - The keyword that produced the annotation
- Instance location - Where it’s attached (as a JSON Pointer)
- Evaluation path - How reference keywords like
$refwere followed - Schema location - The absolute IRI of the keyword (may be omitted if same as evaluation path)
- Annotation value - The attached value(s)
Annotation Values
Unless otherwise specified, a keyword’s annotation value is the value of the keyword itself:title keyword produces annotation value "User" and description produces "A user account".
Some keywords produce calculated annotation values based on what was evaluated:
properties keyword produces an annotation containing the set of property names that were successfully evaluated: ["name", "age"] (assuming both properties were present in the instance).
Annotations and Validation
Schema objects that produce a false assertion result MUST NOT produce any annotation results, whether from their own keywords or from keywords in subschemas.- Schema
- Instance: hello
- Instance: 42
Common Annotation Keywords
JSON Schema defines several standard annotation keywords:Metadata Annotations
title - A short, human-readable title:
description - A longer explanation of the purpose:
default - A default value for the instance:
It is RECOMMENDED that a default value be valid against the associated schema.
examples - Sample values demonstrating usage:
deprecated - Indicates the instance is deprecated:
true value indicates applications SHOULD refrain from using this property as it may be removed in the future.
Usage Annotations
readOnly - Indicates the value is managed by the owning authority:
writeOnly - Indicates the value is never present when retrieved:
These keywords can assist in UI generation. For example, an application may hide input for write-only fields as they are typed.
Format Annotations
format - Provides semantic information about string values:
date-time- RFC 3339 date-timedate- RFC 3339 full-datetime- RFC 3339 full-timeemail- RFC 5321 email addresshostname- RFC 1123 hostnameipv4/ipv6- IP addressesuri/uri-reference- URI referencesuuid- UUID string
Content Annotations
For string-encoded data:contentEncoding - How the string is encoded:
contentMediaType - The media type of the content:
contentSchema - A schema for the decoded content:
Practical Uses of Annotations
Annotations enable various application behaviors:Documentation Generation
Usetitle, description, and examples to generate human-readable documentation:
UI Form Generation
- Schema
- Generated UI
Default Values
Applications can usedefault annotations to fill in missing values:
{} can be filled with defaults: {"theme": "auto", "pageSize": 25}.
API Documentation
Combine multiple annotation keywords for rich API documentation:Multiple Annotation Values
Since many subschemas can apply to a single instance location, applications may receive multiple annotation values for the same keyword.- Use the most specific value (from innermost schema)
- Combine all values
- Use the first or last value encountered
- Consider it an error
JSON Schema provides the schema location (evaluation path) with each annotation to help applications distinguish among multiple values.