Permalink
Fetching contributors…
Cannot retrieve contributors at this time
2127 lines (2040 sloc) 111 KB
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
<!ENTITY RFC2119 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC3986 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3986.xml">
<!ENTITY RFC6839 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6839.xml">
<!ENTITY RFC6901 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6901.xml">
<!ENTITY RFC7049 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.7049.xml">
<!ENTITY RFC8259 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.8259.xml">
<!ENTITY RFC7231 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.7231.xml">
<!ENTITY RFC8288 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.8288.xml">
<!ENTITY ldp SYSTEM "https://xml2rfc.tools.ietf.org/public/rfc/bibxml4/reference.W3C.REC-ldp-20150226.xml">
<!ENTITY fragid-best-practices SYSTEM "https://xml2rfc.tools.ietf.org/public/rfc/bibxml4/reference.W3C.WD-fragid-best-practices-20121025.xml">
]>
<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc strict="no"?>
<?rfc rfcedstyle="yes"?>
<?rfc comments="yes"?>
<?rfc inline="yes" ?>
<rfc category="info" docName="draft-handrews-json-schema-02" ipr="trust200902">
<front>
<title abbrev="JSON Schema">JSON Schema: A Media Type for Describing JSON Documents</title>
<author fullname="Austin Wright" initials="A" surname="Wright" role="editor">
<address>
<email>aaa@bzfx.net</email>
</address>
</author>
<author fullname="Henry Andrews" initials="H" surname="Andrews" role="editor">
<address>
<postal>
<street></street>
<city>San Francisco</city>
<region>CA</region>
<country>USA</country>
</postal>
<email>henry@cloudflare.com</email>
</address>
</author>
<date year="2018"/>
<workgroup>Internet Engineering Task Force</workgroup>
<keyword>JSON</keyword>
<keyword>Schema</keyword>
<keyword>Hyper Schema</keyword>
<keyword>Hypermedia</keyword>
<abstract>
<t>
JSON Schema defines the media type "application/schema+json", a JSON-based format
for describing the structure of JSON data.
JSON Schema asserts what a JSON document must look like,
ways to extract information from it,
and how to interact with it.
The "application/schema-instance+json" media type provides additional
feature-rich integration with "application/schema+json" beyond what can be offered
for "application/json" documents.
</t>
</abstract>
<note title="Note to Readers">
<t>
The issues list for this draft can be found at
<eref target="https://github.com/json-schema-org/json-schema-spec/issues"/>.
</t>
<t>
For additional information, see <eref target="http://json-schema.org/"/>.
</t>
<t>
To provide feedback, use this issue tracker, the communication methods listed on the
homepage, or email the document editors.
</t>
</note>
</front>
<middle>
<section title="Introduction">
<t>
JSON Schema is a JSON media type for defining the structure of JSON data. JSON Schema
is intended to define validation, documentation, hyperlink navigation, and interaction
control of JSON data.
</t>
<t>
This specification defines JSON Schema core terminology and mechanisms, including
pointing to another JSON Schema by reference,
dereferencing a JSON Schema reference,
and specifying the vocabulary being used.
</t>
<t>
Other specifications define the vocabularies that perform assertions about validation,
linking, annotation, navigation, and interaction.
</t>
</section>
<section title="Conventions and Terminology">
<t>
<!-- The text in this section has been copied from the official boilerplate,
and should not be modified.-->
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
interpreted as described in <xref target="RFC2119">RFC 2119</xref>.
</t>
<t>
The terms "JSON", "JSON text", "JSON value", "member", "element", "object", "array",
"number", "string", "boolean", "true", "false", and "null" in this document are to
be interpreted as defined in <xref target="RFC8259">RFC 8259</xref>.
</t>
</section>
<section title="Overview">
<t>
This document proposes a new media type "application/schema+json" to identify a JSON
Schema for describing JSON data.
It also proposes a further optional media type, "application/schema-instance+json",
to provide additional integration features.
JSON Schemas are themselves JSON documents.
This, and related specifications, define keywords allowing authors to describe JSON
data in several ways.
</t>
<section title="Keyword Behaviors">
<t>
JSON Schema keywords fall into several general behavior categories.
Assertions validate that an instance satisfies constraints, producing
a boolean result. Annotations attach information that applications
may use in any way they see fit.
Applicators apply subschemas to parts of the instance and combine
their results.
</t>
<t>
Extension keywords SHOULD stay within these categories, keeping in mind
that annotations in particular are extremely flexible. Complex behavior
is usually better delegated to applications on the basis of annotation
data than implemented directly as schema keywords. However, extension
keywords MAY define other behaviors for specialized purposes.
</t>
<t>
Evaluating an instance against a schema involves processing all of the
keywords in the schema against the appropriate locations within the instance.
Typically, applicator keywords are processed until a schema object with no
applicators (and therefore no subschemas) is reached. The appropriate
location in the instance is evaluated against the assertion and
annotation keywords in the schema object, and their results are gathered
into the parent schema according to the rules of the applicator.
</t>
<t>
Evaluation of a parent schema object can complete once all of its
subschemas have been evaluated, although in some circumstance evaluation
may be short-circuited due to assertion results.
</t>
<section title="Keyword Interactions">
<t>
Keyword behavior MAY be defined in terms of the annotation results
of <xref target="root">subschemas</xref> and/or adjacent keywords.
Such keywords MUST NOT result in a circular dependency.
Keywords MAY modify their behavior based on the presence or absence
of another keyword in the same
<xref target="schema-document">schema object</xref>.
</t>
</section>
<section title="Default Behaviors">
<t>
A missing keyword MUST NOT produce a false assertion result, MUST
NOT produce annotation results, and MUST NOT cause any other schema
to be evaluated as part of its own behavioral definition.
However, given that missing keywords do not contribute annotations,
the lack of annotation results may indirectly change the behavior
of other keywords.
</t>
<t>
In some cases, the missing keyword assertion behavior of a keyword is
identical to that produced by a certain value, and keyword definitions
SHOULD note such values where known. However, even if the value which
produces the default behavior would produce annotation results if
present, the default behavior still MUST NOT result in annotations.
</t>
<t>
Because annotation collection can add significant cost in terms of both
computation and memory, implementations MAY opt out of this feature.
Keywords known to an implementation to have assertion or applicator behavior
that depend on annotation results MUST then be treated as errors, unless
an alternate implementation producing the same behavior is available.
Keywords of this sort SHOULD describe reasonable alternate approaches
when appropriate. This approach is demonstrated by the
"<xref target="additionalItems" format="title"/>" and
"<xref target="additionalProperties" format="title"/>" keywords in this
document.
</t>
</section>
<section title="Applicators" anchor="applicators">
<t>
Applicators allow for building more complex schemas than can be accomplished
with a single schema object. Evaluation of an instance against a
<xref target="schema-document">schema document</xref> begins by applying
the <xref target="root">root schema</xref> to the complete instance
document. From there, keywords known as applicators are used to determine
which additional schemas are applied. Such schemas may be applied in-place
to the current location, or to a child location.
</t>
<t>
The schemas to be applied may be present as subschemas comprising all or
part of the keyword's value. Alternatively, an applicator may refer to
a schema elsewhere in the same schema document, or in a different one.
The mechanism for identifying such referred schemas is defined by the
keyword.
</t>
<t>
Applicator keywords also define how subschema or referred schema
boolean <xref target="assertions">assertion</xref>
results are modified and/or combined to produce the boolean result
of the applicator. Applicators may apply any boolean logic operation
to the assertion results of subschemas, but MUST NOT introduce new
assertion conditions of their own.
</t>
<t>
<xref target="annotations">Annotation</xref> results are
combined according to the rules specified by each annotation keyword.
</t>
</section>
<section title="Assertions" anchor="assertions">
<t>
JSON Schema can be used to assert constraints on a JSON document, which
either passes or fails the assertions. This approach can be used to validate
conformance with the constraints, or document what is needed to satisfy them.
</t>
<t>
JSON Schema implementations produce a single boolean result when evaluating
an instance against schema assertions.
</t>
<t>
An instance can only fail an assertion that is present in the schema.
</t>
<section title="Assertions and Instance Primitive Types">
<t>
Most assertions only constrain values within a certain
primitive type. When the type of the instance is not of the type
targeted by the keyword, the instance is considered to conform
to the assertion.
</t>
<t>
For example, the "maxLength" keyword from the companion validation
vocabulary will only restrict certain strings
(that are too long) from being valid. If the instance is a number,
boolean, null, array, or object, then it is valid against this assertion.
</t>
</section>
</section>
<section title="Annotations" anchor="annotations">
<t>
JSON Schema can annotate an instance with information, whenever the instance
validates against the schema object containing the annotation, and all of its
parent schema objects. The information can be a simple value, or can be
calculated based on the instance contents.
</t>
<t>
Annotations are attached to specific locations in an instance.
Since many subschemas can be applied to any single
location, annotation keywords need to specify any unusual handling of
multiple applicable occurrences of the keyword with different values.
</t>
<t>
The default behavior is simply to collect all values in a list in
indeterminate order. Given the extensibility of keywords, including
applicators, it is not possible to define a universally predictable
order of processing.
</t>
<t>
Unlike assertion results, annotation data can take a wide variety of forms,
which are provided to applications to use as they see fit. JSON Schema
implementations are not expected to make use of the collected information
on behalf of applications.
</t>
<t>
While "short-circuit" evaluation is possible for assertions, collecting
annotations requires examining all schemas that apply to an instance
location, even if they cannot change the overall assertion result.
</t>
</section>
</section>
<section title="Schema Vocabularies" anchor="vocabulary">
<t>
A JSON Schema vocabulary is a set of keywords defined for a particular
purpose. The vocabulary specifies the meaning of its keywords as
assertions, annotations, and/or any vocabulary-defined keyword category.
</t>
<t>
Several vocabularies are provided as
standards in this and closely related documents. These vocabularies
are used with the core keywords defined as fundamental to the
"application/schema+json" media type.
</t>
<t>
Schema authors are encouraged to define their own vocabularies for
domain-specific concepts. A vocabulary need not be a standard to
be re-usable, although users of extension vocabularies MUST NOT
assume that any JSON Schema implementation can support the vocabulary
unless it specifically documents such support.
</t>
<section title="Subschema Application">
<t>
This vocabulary provides keywords for applying subschemas to the
instance in various ways. It is defined in this document, and
it is RECOMMENDED that all JSON Schema implementations support it.
All other vocabularies in this section are designed to be used
alongside the subschema application vocabulary.
</t>
<t>
Without this vocabulary or an equivalent one, JSON Schema can only
be applied to a JSON document as a whole. In most cases, schema
keywords need to be applied to specific object properties or array items.
</t>
</section>
<section title="Validation">
<t>
This vocabulary describes the structure of a JSON document
(for instance, required properties and length limitations).
Applications can use this information to validate instances (check that
constraints are met), or inform interfaces to collect user input
such that the constraints are satisfied.
</t>
<t>
Validation behaviour and keywords are specified in
<xref target="json-schema-validation">a separate document</xref>.
</t>
</section>
<section title="Basic Meta-Data">
<t>
A small set of annotation keywords are defined in
<xref target="json-schema-validation">the validation specification</xref>
to allow associating common kinds of meta-data with an instance.
</t>
</section>
<section title="Hypermedia and Linking">
<t>
JSON Hyper-Schema produces hyperlinks as annotations available for
use with a JSON document. It supports resolving URI Templates
and describing the resource and data submission formats required
to use an API.
</t>
<t>
Hyper-schema behaviour and keywords are specified in
<xref target="json-hyper-schema">a separate document</xref>.
</t>
</section>
</section>
</section>
<section title="Definitions">
<section title="JSON Document">
<t>
A JSON document is an information resource (series of octets) described by the
application/json media type.
</t>
<t>
In JSON Schema, the terms "JSON document", "JSON text", and "JSON value" are
interchangeable because of the data model it defines.
</t>
<t>
JSON Schema is only defined over JSON documents. However, any document or memory
structure that can be parsed into or processed according to the JSON Schema data
model can be interpreted against a JSON Schema, including media types like
<xref target="RFC7049">CBOR</xref>.
</t>
</section>
<section title="Instance">
<t>
A JSON document to which a schema is applied is known as an "instance".
</t>
<section title="Instance Data Model">
<t>
JSON Schema interprets documents according to a data model. A JSON value
interpreted according to this data model is called an "instance".
</t>
<t>
An instance has one of six primitive types, and a range of possible values
depending on the type:
<list style="hanging">
<t hangText="null:">A JSON "null" production</t>
<t hangText="boolean:">A "true" or "false" value, from the JSON "true" or "false" productions</t>
<t hangText="object:">An unordered set of properties mapping a string to an instance, from the JSON "object" production</t>
<t hangText="array:">An ordered list of instances, from the JSON "array" production</t>
<t hangText="number:">An arbitrary-precision, base-10 decimal number value, from the JSON "number" production</t>
<t hangText="string:">A string of Unicode code points, from the JSON "string" production</t>
</list>
</t>
<t>
Whitespace and formatting concerns, including different lexical
representations of numbers that are equal within the data model, are thus
outside the scope of JSON Schema. JSON Schema
<xref target="vocabulary">vocabularies</xref> that wish
to work with such differences in lexical representations SHOULD define
keywords to precisely interpret formatted strings within the data model
rather than relying on having the original JSON representation Unicode
characters available.
</t>
<t>
Since an object cannot have two properties with the same key, behavior for a
JSON document that tries to define two properties (the "member" production) with
the same key (the "string" production) in a single object is undefined.
</t>
<t>
Note that JSON Schema vocabularies are free to define their own extended
type system. This should not be confused with the core data model types
defined here. As an example, "integer" is a reasonable type for a
vocabulary to define as a value for a keyword, but the data model
makes no distinction between integers and other numbers.
</t>
</section>
<section title="Instance Media Types">
<t>
JSON Schema is designed to fully work with "application/json" documents,
as well as media types using the "+json" structured syntax suffix.
</t>
<t>
Some functionality that is useful for working with schemas is
defined by each media type, namely media type parameters and
URI fragment identifier syntax and semantics. These features are
useful in content negotiation and in calculating URIs for specific
locations within an instance, respectively.
</t>
<t>
This specification defines the "application/schema-instance+json"
media type in order to allow instance authors to take full advantage
of parameters and fragment identifiers for these purposes.
</t>
</section>
<section title="Instance Equality">
<t>
Two JSON instances are said to be equal if and only if they are of the same type
and have the same value according to the data model. Specifically, this means:
<list>
<t>both are null; or</t>
<t>both are true; or</t>
<t>both are false; or</t>
<t>both are strings, and are the same codepoint-for-codepoint; or</t>
<t>both are numbers, and have the same mathematical value; or</t>
<t>both are arrays, and have an equal value item-for-item; or</t>
<t>both are objects, and each property in one has exactly one property with
a key equal to the other's, and that other property has an equal
value.</t>
</list>
</t>
<t>
Implied in this definition is that arrays must be the same length,
objects must have the same number of members,
properties in objects are unordered,
there is no way to define multiple properties with the same key,
and mere formatting differences (indentation, placement of commas, trailing
zeros) are insignificant.
</t>
</section>
</section>
<section title="JSON Schema Documents" anchor="schema-document">
<t>
A JSON Schema document, or simply a schema, is a JSON document used to describe
an instance.
A schema is itself interpreted as an instance, but SHOULD always be given
the media type "application/schema+json" rather than
"application/schema-instance+json". The "application/schema+json" media
type is defined to offer a superset of the media type parameter and
fragment identifier syntax and semantics provided by
"application/schema-instance+json".
</t>
<t>
A JSON Schema MUST be an object or a boolean.
</t>
<section title="JSON Schema Objects and Keywords">
<t>
Object properties that are applied to the instance are called keywords,
or schema keywords. Broadly speaking, keywords fall into one
of three categories:
<list style="hanging">
<t hangText="assertions:">
produce a boolean result when applied to an instance
</t>
<t hangText="annotations:">
attach information to an instance for application use
</t>
<t hangText="applicators:">
apply one or more subschemas to a particular location
in the instance, and combine or modify their results
</t>
</list>
</t>
<t>
Keywords may fall into multiple categories, although applicators
SHOULD only produce assertion results based on their subschemas'
results. They should not define additional constraints independent
of their subschemas.
</t>
<t>
Extension keywords, meaning those defined outside of this document
and its companions, are free to define other behaviors as well.
</t>
<t>
A JSON Schema MAY contain properties which are not schema keywords.
Unknown keywords SHOULD be ignored.
</t>
<t>
An empty schema is a JSON Schema with no properties, or only unknown
properties.
</t>
</section>
<section title="Boolean JSON Schemas">
<t>
The boolean schema values "true" and "false" are trivial schemas that
always produce themselves as assertions results, regardless of the
instance value. They never produce annotation results.
</t>
<t>
These boolean schemas exist to clarify schema author intent and
facilitate schema processing optimizations. They behave identically
to the following schema objects (where "not" is part of the
subschema application vocabulary defined in this document).
<list style="hanging">
<t hangText="true:">
Always passes validation, as if the empty schema {}
</t>
<t hangText="false:">
Always fails validation, as if the schema { "not":{} }
</t>
</list>
While the empty schema object is unambiguous, there are many
possible equivalents to the "false" schema. Using the boolean
values ensures that the intent is clear to both human readers
and implementations.
</t>
</section>
<section title="Root Schema and Subschemas" anchor="root">
<t>
The root schema is the schema that comprises the entire JSON document
in question.
</t>
<t>
Some keywords take schemas themselves, allowing JSON Schemas to be nested:
</t>
<figure>
<artwork>
<![CDATA[
{
"title": "root",
"items": {
"title": "array item"
}
}
]]>
</artwork>
</figure>
<t>
In this example document, the schema titled "array item" is a subschema,
and the schema titled "root" is the root schema.
</t>
<t>
As with the root schema, a subschema is either an object or a boolean.
</t>
</section>
<section title="Referred and Referring Schemas" anchor="referred">
<t>
As noted in <xref target="applicators" />, an applicator keyword may
refer to a schema to be applied, rather than including it as a
subschema in the applicator's value. In such situations, the
schema being applied is known as the referred schema, while
the schema containing the applicator keyword is the referring schema.
</t>
<t>
While root schemas and subschemas are static concepts based on a
schema's position within a schema document, referred and referring
schemas are dynamic. Different pairs of schemas may find themselves
in various referred and referring arrangements during the evaluation
of an instance against a schema.
</t>
<t>
For some by-reference applicators, such as
<xref target="ref">"$ref"</xref>, the referred schema can be determined
by static analysis of the schema document. Others may take evaluation
context into account, and only be resolvable in the process of evaluating
with an instance.
<cref>
Assuming some form of dynamic reference keyword is introduced
related to GitHub issue #558, this section will be updated
to make a concrete reference to that keyword for clarification.
</cref>
</t>
</section>
</section>
</section>
<section title="Fragment Identifiers" anchor="fragments">
<t>
In accordance with section 3.1 of <xref target="RFC6839"></xref>,
the syntax and semantics of fragment identifiers specified for
any +json media type SHOULD be as specified for "application/json".
(At publication of this document, there is no fragment identification
syntax defined for "application/json".)
</t>
<t>
Additionally, the "application/schema+json" media type supports two
fragment identifier structures: plain names and JSON Pointers.
The "application/schema-instance+json" media type supports one
fragment identifier structure: JSON Pointers.
</t>
<t>
The use of JSON Pointers as URI fragment identifiers is described in
<xref target="RFC6901">RFC 6901</xref>.
For "application/schema+json", which supports two fragment identifier syntaxes,
fragment identifiers matching the JSON Pointer syntax, including the empty string,
MUST be interpreted as JSON Pointer fragment identifiers.
</t>
<t>
Per the W3C's
<xref target="W3C.WD-fragid-best-practices-20121025">best practices for fragment identifiers</xref>,
plain name fragment identifiers in "application/schema+json" are reserved for referencing
locally named schemas. All fragment identifiers that do
not match the JSON Pointer syntax MUST be interpreted as
plain name fragment identifiers.
</t>
<t>
Defining and referencing a plain name fragment identifier within an
"application/schema+json" document are specified
in the <xref target="id-keyword">"$id" keyword</xref> section.
</t>
<t>
</t>
</section>
<section title="General Considerations">
<section title="Range of JSON Values">
<t>
An instance may be any valid JSON value as defined by <xref target="RFC8259">JSON</xref>.
JSON Schema imposes no restrictions on type: JSON Schema can describe any JSON
value, including, for example, null.
</t>
</section>
<section title="Programming Language Independence" anchor="language">
<t>
JSON Schema is programming language agnostic, and supports the full range of
values described in the data model.
Be aware, however, that some languages and JSON parsers may not be able to
represent in memory the full range of values describable by JSON.
</t>
</section>
<section title="Mathematical Integers" anchor="integers">
<t>
Some programming languages and parsers use different internal representations
for floating point numbers than they do for integers.
</t>
<t>
For consistency, integer JSON numbers SHOULD NOT be encoded with a fractional
part.
</t>
</section>
<section title="Regular Expressions" anchor="regex">
<t>
Keywords MAY use regular expressions to express constraints, or constrain
the instance value to be a regular expression.
These regular expressions SHOULD be valid according to the
<xref target="ecma262">ECMA 262</xref> regular expression dialect.
</t>
<t>
Furthermore, given the high disparity in regular expression constructs support,
schema authors SHOULD limit themselves to the following regular expression
tokens:
<list>
<t>individual Unicode characters, as defined by the <xref
target="RFC8259">JSON specification</xref>;</t>
<t>simple character classes ([abc]), range character classes ([a-z]);</t>
<t>complemented character classes ([^abc], [^a-z]);</t>
<t>simple quantifiers: "+" (one or more), "*" (zero or more), "?" (zero or
one), and their lazy versions ("+?", "*?", "??");</t>
<t>range quantifiers: "{x}" (exactly x occurrences), "{x,y}" (at least x, at
most y, occurrences), {x,} (x occurrences or more), and their lazy
versions;</t>
<t>the beginning-of-input ("^") and end-of-input ("$") anchors;</t>
<t>simple grouping ("(...)") and alternation ("|").</t>
</list>
</t>
<t>
Finally, implementations MUST NOT take regular expressions to be
anchored, neither at the beginning nor at the end. This means, for instance,
the pattern "es" matches "expression".
</t>
</section>
<section title="Extending JSON Schema">
<t>
Additional schema keywords and schema vocabularies MAY be defined
by any entity. Save for explicit agreement, schema authors SHALL NOT
expect these additional keywords and vocabularies to be supported by
implementations that do not explicitly document such support.
Implementations SHOULD ignore keywords they do not support.
</t>
<t>
Vocabulary authors SHOULD
take care to avoid keyword name collisions if the vocabulary is intended
for broad use, and potentially combined with other vocabularies. JSON
Schema does not provide any formal namespacing system, but also does
not constrain keyword names, allowing for any number of namespacing
approaches.
</t>
<t>
Vocabularies may build on each other, such as by defining the behavior
of their keywords with respect to the behavior of keywords from another
vocabulary, or by using a keyword from another vocabulary with
a restricted or expanded set of acceptable values. Not all such
vocabulary re-use will result in a new vocabulary that is compatible
with the vocabulary on which it is built. Vocabulary authors SHOULD
clearly document what level of compatibility, if any, is expected.
</t>
<t>
A schema that itself describes a schema is called a meta-schema.
Meta-schemas are used to validate JSON Schemas and specify which vocabulary
it is using.
</t>
<t>
Authors of extensions to JSON Schema are encouraged to write their own
meta-schemas, which extend the existing meta-schemas using "allOf".
This extended meta-schema SHOULD be referenced using the "$schema" keyword, to
allow tools to follow the correct behaviour.
</t>
<t>
Note that the recursive nature of meta-schemas requires re-defining
recursive keywords in the extended meta-schema, as can be seen in
the JSON Hyper-Schema meta-schema.
</t>
</section>
</section>
<section title='The "$schema" Keyword'>
<!-- TODO a custom $schema keyword might also be used to enforce minimum required functionality of a validator -->
<t>
The "$schema" keyword is both used as a JSON Schema version identifier and the
location of a resource which is itself a JSON Schema, which describes any schema
written for this particular version.
</t>
<t>
The value of this keyword MUST be a <xref target="RFC3986">URI</xref>
(containing a scheme) and this URI MUST be normalized.
The current schema MUST be valid against the meta-schema identified by this URI.
</t>
<t>
If this URI identifies a retrievable resource, that resource SHOULD be of
media type "application/schema+json".
</t>
<t>
The "$schema" keyword SHOULD be used in a root schema.
It MUST NOT appear in subschemas.
</t>
<t>
<cref>
Using multiple "$schema" keywords in the same document would imply that the
vocabulary and therefore behavior can change within a document. This would
necessitate resolving a number of implementation concerns that have not yet
been clearly defined. So, while the pattern of using "$schema" only in root
schemas is likely to remain the best practice for schema authoring,
implementation behavior is subject to be revised or liberalized in
future drafts.
</cref>
<!--
In particular, the process of validating an instance, including validating a
schema as an instance against its meta-schema, only allows for a single set
of rules across the entire instance document. There is no equivalent of
changing the meta-schema partway through the validation for non-schema
instances.
-->
</t>
<t>
Values for this property are defined in other documents and by other parties.
JSON Schema implementations SHOULD implement support for current and previous
published drafts of JSON Schema vocabularies as deemed reasonable.
</t>
</section>
<section title="Base URI and Dereferencing">
<t>
To differentiate between schemas in a vast ecosystem, schemas are
identified by <xref target="RFC3986">URI</xref>, and can embed references to other schemas by specifying their URI.
</t>
<section title="Initial Base URI">
<t>
<xref target="RFC3986">RFC3986 Section 5.1</xref> defines how to determine the
default base URI of a document.
</t>
<t>
Informatively, the initial base URI of a schema is the URI at which it was
found, or a suitable substitute URI if none is known.
</t>
</section>
<section title='The "$id" Keyword' anchor="id-keyword">
<t>
The "$id" keyword defines a URI for the schema, and the base URI that
other URI references within the schema are resolved against.
A subschema's "$id" is resolved against the base URI of its parent schema.
If no parent sets an explicit base with "$id", the base URI is that of the
entire document, as determined per
<xref target="RFC3986">RFC 3986 section 5</xref>.
</t>
<t>
If present, the value for this keyword MUST be a string, and MUST represent a
valid <xref target="RFC3986">URI-reference</xref>.
This value SHOULD be normalized, and SHOULD NOT be an empty fragment &lt;#&gt;
or an empty string &lt;&gt;.
</t>
<section title="Identifying the root schema">
<t>
The root schema of a JSON Schema document SHOULD contain an "$id" keyword with
an <xref target="RFC3986">absolute-URI</xref> (containing a scheme, but no fragment),
or this absolute URI but with an empty fragment.
<!-- All of the standard meta-schemas use an empty fragment in their id/$id values. -->
</t>
</section>
<section title="Changing the base URI within a schema file">
<t>
When an "$id" sets the base URI, the object containing that "$id" and all of
its subschemas can be identified by using a JSON Pointer fragment starting
from that location. This is true even of subschemas that further change the
base URI. Therefore, a single subschema may be accessible by multiple URIs,
each consisting of base URI declared in the subschema or a parent, along with
a JSON Pointer fragment identifying the path from the schema object that
declares the base to the subschema being identified. Examples of this are
shown in section <xref target="idExamples" format="counter"></xref>.
</t>
</section>
<section title="Location-independent identifiers">
<t>
Using JSON Pointer fragments requires knowledge of the structure of the schema.
When writing schema documents with the intention to provide re-usable
schemas, it may be preferable to use a plain name fragment that is not tied to
any particular structural location. This allows a subschema to be relocated
without requiring JSON Pointer references to be updated.
</t>
<t>
To specify such a subschema identifier,
the "$id" keyword is set to a URI reference with a plain name fragment (not a JSON Pointer fragment).
This value MUST begin with the number sign that specifies a fragment ("#"),
then a letter ([A-Za-z]),
followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
colons (":"), or periods (".").
</t>
<t>
The effect of using a fragment in "$id" that isn't blank or doesn't follow the
plain name syntax is undefined.
<cref>
How should an "$id" URI reference containing a fragment with other components
be interpreted? There are two cases: when the other components match
the current base URI and when they change the base URI.
</cref>
</t>
</section>
<section title="Schema identification examples" anchor="idExamples">
<figure>
<preamble>
Consider the following schema, which shows "$id" being used to identify
the root schema, change the base URI for subschemas, and assign plain
name fragments to subschemas:
</preamble>
<artwork>
<![CDATA[
{
"$id": "http://example.com/root.json",
"$defs": {
"A": { "$id": "#foo" },
"B": {
"$id": "other.json",
"$defs": {
"X": { "$id": "#bar" },
"Y": { "$id": "t/inner.json" }
}
},
"C": {
"$id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f"
}
}
}
]]>
</artwork>
</figure>
<t>
The schemas at the following URI-encoded <xref target="RFC6901">JSON
Pointers</xref> (relative to the root schema) have the following
base URIs, and are identifiable by any listed URI in accordance with
Section <xref target="fragments" format="counter"></xref> above:
</t>
<t>
<list style="hanging">
<t hangText="# (document root)">
<list>
<t>http://example.com/root.json</t>
<t>http://example.com/root.json#</t>
</list>
</t>
<t hangText="#/$defs/A">
<list>
<t>http://example.com/root.json#foo</t>
<t>http://example.com/root.json#/$defs/A</t>
</list>
</t>
<t hangText="#/$defs/B">
<list>
<t>http://example.com/other.json</t>
<t>http://example.com/other.json#</t>
<t>http://example.com/root.json#/$defs/B</t>
</list>
</t>
<t hangText="#/$defs/B/$defs/X">
<list>
<t>http://example.com/other.json#bar</t>
<t>http://example.com/other.json#/$defs/X</t>
<t>http://example.com/root.json#/$defs/B/$defs/X</t>
</list>
</t>
<t hangText="#/$defs/B/$defs/Y">
<list>
<t>http://example.com/t/inner.json</t>
<t>http://example.com/t/inner.json#</t>
<t>http://example.com/other.json#/$defs/Y</t>
<t>http://example.com/root.json#/$defs/B/$defs/Y</t>
</list>
</t>
<t hangText="#/$defs/C">
<list>
<t>urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f</t>
<t>urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f#</t>
<t>http://example.com/root.json#/$defs/C</t>
</list>
</t>
</list>
</t>
</section>
</section>
<section title='Schema References With "$ref"' anchor="ref">
<t>
The "$ref" keyword can be used to reference a schema which is to be applied to the
current instance location. "$ref" is an applicator key word, applying the referred
schema to the instance.
</t>
<t>
The value of the "$ref" property MUST be a string which is a URI Reference.
Resolved against the current URI base, it identifies the URI of a schema to use.
</t>
<t>
As the value of "$ref" is a URI Reference, this allows the possibility to externalise or
divide a schema across multiple files, and provides the ability to validate recursive structures
through self-reference.
</t>
<t>
The URI is not a network locator, only an identifier. A schema need not be
downloadable from the address if it is a network-addressable URL, and
implementations SHOULD NOT assume they should perform a network operation when they
encounter a network-addressable URI.
</t>
<t>
A schema MUST NOT be run into an infinite loop against a schema. For example, if two
schemas "#alice" and "#bob" both have an "allOf" property that refers to the other,
a naive validator might get stuck in an infinite recursive loop trying to validate
the instance.
Schemas SHOULD NOT make use of infinite recursive nesting like this; the behavior is
undefined.
</t>
<section title="Loading a referenced schema">
<t>
The use of URIs to identify remote schemas does not necessarily mean anything is downloaded,
but instead JSON Schema implementations SHOULD understand ahead of time which schemas they will be using,
and the URIs that identify them.
</t>
<t>
When schemas are downloaded,
for example by a generic user-agent that doesn't know until runtime which schemas to download,
see <xref target="hypermedia">Usage for Hypermedia</xref>.
</t>
<t>
Implementations SHOULD be able to associate arbitrary URIs with an arbitrary
schema and/or automatically associate a schema's "$id"-given URI, depending
on the trust that the validator has in the schema. Such URIs and schemas
can be supplied to an implementation prior to processing instances, or may
be noted within a schema document as it is processed, producing associations
as shown in section <xref target="idExamples" format="counter"></xref>.
</t>
<t>
A schema MAY (and likely will) have multiple URIs, but there is no way for a
URI to identify more than one schema. When multiple schemas try to identify
as the same URI, validators SHOULD raise an error condition.
</t>
</section>
<section title="Dereferencing">
<t>
Schemas can be identified by any URI that has been given to them, including
a JSON Pointer or their URI given directly by "$id". In all cases,
dereferencing a "$ref" reference involves first resolving its value as a
URI reference against the current base URI per
<xref target="RFC3986">RFC 3986</xref>.
</t>
<t>
If the resulting URI identifies a schema within the current document, or
within another schema document that has been made available to the implementation,
then that schema SHOULD be used automatically.
</t>
<t>
For example, consider this schema:
</t>
<figure>
<artwork>
<![CDATA[
{
"$id": "http://example.net/root.json",
"items": {
"type": "array",
"items": { "$ref": "#item" }
},
"$defs": {
"single": {
"$id": "#item",
"type": "object",
"additionalProperties": { "$ref": "other.json" }
}
}
}
]]>
</artwork>
</figure>
<t>
When an implementation encounters the &lt;#/$defs/single&gt; schema,
it resolves the "$id" URI reference against the current base URI to form
&lt;http://example.net/root.json#item&gt;.
</t>
<t>
When an implementation then looks inside the &lt;#/items&gt; schema, it
encounters the &lt;#item&gt; reference, and resolves this to
&lt;http://example.net/root.json#item&gt;, which it has seen defined in
this same document and can therefore use automatically.
</t>
<t>
When an implementation encounters the reference to "other.json", it resolves
this to &lt;http://example.net/other.json&gt;, which is not defined in this
document. If a schema with that identifier has otherwise been supplied to
the implementation, it can also be used automatically.
<cref>
What should implementations do when the referenced schema is not known?
Are there circumstances in which automatic network dereferencing is
allowed? A same origin policy? A user-configurable option? In the
case of an evolving API described by Hyper-Schema, it is expected that
new schemas will be added to the system dynamically, so placing an
absolute requirement of pre-loading schema documents is not feasible.
</cref>
</t>
</section>
</section>
<section title='Schema Re-Use With "$defs"'>
<t>
The "$defs" keyword provides a standardized location for schema
authors to inline re-usable JSON Schemas into a more general schema.
The keyword does not directly affect the validation result.
</t>
<t>
This keyword's value MUST be an object.
Each member value of this object MUST be a valid JSON Schema.
</t>
<t>
As an example, here is a schema describing an array of positive
integers, where the positive integer constraint is a subschema in
"$defs":
<figure>
<artwork>
<![CDATA[
{
"type": "array",
"items": { "$ref": "#/$defs/positiveInteger" },
"$defs": {
"positiveInteger": {
"type": "integer",
"exclusiveMinimum": 0
}
}
}
]]>
</artwork>
</figure>
</t>
</section>
</section>
<section title='Comments With "$comment"'>
<t>
This keyword is reserved for comments from schema authors to readers or
maintainers of the schema.
The value of this keyword MUST be a string. Implementations MUST NOT present this
string to end users. Tools for editing schemas SHOULD support displaying and
editing this keyword. The value of this keyword MAY be used in debug or error
output which is intended for developers making use of schemas.
Schema vocabularies SHOULD allow "$comment" within any object containing
vocabulary keywords. Implementations MAY assume "$comment" is allowed
unless the vocabulary specifically forbids it. Vocabularies MUST NOT
specify any effect of "$comment" beyond what is described in this
specification.
Tools that translate other media types or programming languages
to and from application/schema+json MAY choose to convert that media type or
programming language's native comments to or from "$comment" values.
The behavior of such translation when both native comments and "$comment"
properties are present is implementation-dependent.
Implementations SHOULD treat "$comment" identically to an unknown extension
keyword. They MAY strip "$comment" values at any point during processing.
In particular, this allows for shortening schemas when the size of deployed
schemas is a concern.
Implementations MUST NOT take any other action based on the presence, absence,
or contents of "$comment" properties. In particular, the value of "$comment"
MUST NOT be collected as an annotation result.
</t>
</section>
<section title="Collecting Annotations">
<t>
<cref>
The exact structure and format of the information collected is TBD,
but will be defined before the next draft. Some details of this
section may change as a result, but the overall process is expected
to remain the same. See GitHub issue #396 to track progress.
</cref>
</t>
<t>
Annotations are collected by keywords that explicitly define
annotation-collecting behavior. Note that boolean schemas cannot
produce annotations as they do not make use of keywords.
</t>
<t>
A collected annotation MUST include the following information:
<list>
<t>
The name of the keyword that produces the annotation
</t>
<t>
The instance location to which it is attached, as a JSON Pointer
</t>
<t>
The absolute schema location of the attaching keyword, as a URI
</t>
<t>
The schema location path, indicating how reference keywords
such as "$ref" were followed to reach the absolute schema location
<cref>The exact format of this path is TBD, again see issue #396</cref>
</t>
<t>
The attached value(s)
</t>
</list>
</t>
<t>
If the same keyword attaches values from multiple schema locations
to the same instance location, and the annotation defines a process
for combining such values, then the combined value MUST also be associated
with the instance location.
</t>
<section title="Distinguishing Among Multiple Values">
<t>
Applications MAY make decisions on which of multiple annotation values
to use based on the schema location that contributed the value.
This is intended to allow flexible usage. Collecting the schema location
facilitates such usage.
</t>
<t>
For example, consider this schema, which uses annotations and assertions from
the <xref target="json-schema-validation">Validation specification</xref>:
</t>
<figure>
<preamble>
Note that some lines are wrapped for clarity.
</preamble>
<artwork>
<![CDATA[
{
"title": "Feature list",
"type": "array",
"items": [
{
"title": "Feature A",
"properties": {
"enabled": {
"$ref": "#/$defs/enabledToggle",
"default": true
}
}
},
{
"title": "Feature B",
"properties": {
"enabled": {
"description": "If set to null, Feature B
inherits the enabled
value from Feature A",
"$ref": "#/$defs/enabledToggle"
}
}
}
]
},
"$defs": {
"enabledToggle": {
"title": "Enabled",
"description": "Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)",
"type": ["boolean", "null"],
"default": null
}
}
}
]]>
</artwork>
</figure>
<t>
In this example, both Feature A and Feature B make use of the re-usable
"enabledToggle" schema. That schema uses the "title", "description",
and "default" annotations, none of which define special behavior for
handling multiple values. Therefore the application has to decide how
to handle the additional "default" value for Feature A, and the additional
"description" value for Feature B.
</t>
<t>
The application programmer and the schema author need to agree on the
usage. For this example, let's assume that they agree that the most
specific "default" value will be used, and any additional, more generic
"default" values will be silently ignored. Let's also assume that they
agree that all "description" text is to be used, starting with the most
generic, and ending with the most specific. This requires the schema
author to write descriptions that work when combined in this way.
</t>
<t>
The application can use the schema location path to determine which
values are which. The values in the feature's immediate "enabled"
property schema are more specific, while the values under the re-usable
schema that is referred to with "$ref" are more generic. The schema
location path will show whether each value was found by crossing a
"$ref" or not.
</t>
<t>
Feature A will therefore use a default value of true, while Feature B
will use the generic default value of null. Feature A will only
have the generic description from the "enabledToggle" schema, while
Feature B will use that description, and also append its locally
defined description that explains how to interpret a null value.
</t>
<t>
Note that there are other reasonable approaches that a different application
might take. For example, an application may consider the presence of
two different values for "default" to be an error, regardless of their
schema locations.
</t>
</section>
<section title="Annotations and Assertions">
<t>
Schema objects that produce a false assertion result MUST NOT
produce any annotation results, whether from their own keywords
or from keywords in subschemas.
</t>
<t>
Note that the overall schema results may still include annotations
collected from other schema locations. Given this schema:
</t>
<figure>
<artwork>
<![CDATA[
{
"oneOf": [
{
"title": "Integer Value",
"type": "integer"
},
{
"title": "String Value",
"type": "string"
}
]
}
]]>
</artwork>
</figure>
<t>
And the instance <spanx style="verb">"This is a string"</spanx>, the
title annotation "Integer Value" is discarded because the type assertion
in that schema object fails. The title annotation "String Value"
is kept, as the instance passes the string type assertions.
</t>
</section>
<section title="Annotations and Applicators">
<t>
In addition to possibly defining annotation results of their own,
applicator keywords aggregate the annotations collected in their
subschema(s) or referenced schema(s). The rules for aggregating
annotation values are defined by each annotation keyword, and are
not directly affected by the logic used for combining assertion
results.
</t>
</section>
</section>
<section title="A Vocabulary for Applying Subschemas">
<t>
This section defines a vocabulary of applicator keywords that
are RECOMMENDED for use as the basis of other vocabularies.
</t>
<section title="Keyword Independence">
<t>
Schema keywords typically operate independently, without
affecting each other's outcomes.
</t>
<t>
For schema author convenience, there are some exceptions among the
keywords in this vocabulary:
<list>
<t>
"additionalProperties", whose behavior is defined in terms of
"properties" and "patternProperties"; and
</t>
<t>
"additionalItems", whose behavior is defined in terms of "items".
</t>
</list>
</t>
</section>
<section title="Keywords for Applying Subschemas in Place">
<t>
These keywords apply subschemas to the same location in the instance
as the parent schema is being applied. They allow combining
or modifying the subschema results in various ways.
</t>
<section title="Keywords for Applying Subschemas With Boolean Logic" anchor="logic">
<t>
These keywords correspond to logical operators for combining or modifying
the boolean assertion results of the subschemas. They have no direct
impact on annotation collection, although they enable the same annotation
keyword to be applied to an instance location with different values.
Annotation keywords define their own rules for combining such values.
</t>
<section title="allOf">
<t>
This keyword's value MUST be a non-empty array.
Each item of the array MUST be a valid JSON Schema.
</t>
<t>
An instance validates successfully against this keyword if it validates
successfully against all schemas defined by this keyword's value.
</t>
</section>
<section title="anyOf">
<t>
This keyword's value MUST be a non-empty array.
Each item of the array MUST be a valid JSON Schema.
</t>
<t>
An instance validates successfully against this keyword if it validates
successfully against at least one schema defined by this keyword's value.
</t>
</section>
<section title="oneOf">
<t>
This keyword's value MUST be a non-empty array.
Each item of the array MUST be a valid JSON Schema.
</t>
<t>
An instance validates successfully against this keyword if it validates
successfully against exactly one schema defined by this keyword's value.
</t>
</section>
<section title="not" anchor="not">
<t>
This keyword's value MUST be a valid JSON Schema.
</t>
<t>
An instance is valid against this keyword if it fails to validate
successfully against the schema defined by this keyword.
</t>
</section>
</section>
<section title="Keywords for Applying Subschemas Conditionally" anchor="conditional">
<t>
Three of these keywords work together to implement conditional
application of a subschema based on the outcome of another subschema.
The fourth is a shortcut for a specific conditional case.
</t>
<t>
"if", "then", and "else" MUST NOT interact with each other across
subschema boundaries. In other words, an "if" in one
branch of an "allOf" MUST NOT have an impact on a "then"
or "else" in another branch.
</t>
<t>
There is no default behavior for "if", "then", or "else"
when they are not present. In particular, they MUST NOT
be treated as if present with an empty schema, and when
"if" is not present, both "then" and "else" MUST be
entirely ignored.
</t>
<section title="if">
<t>
This keyword's value MUST be a valid JSON Schema.
</t>
<t>
This validation outcome of this keyword's subschema
has no direct effect on the overall validation
result. Rather, it controls which of the "then"
or "else" keywords are evaluated.
</t>
<t>
Instances that successfully validate against this
keyword's subschema MUST also be valid against
the subschema value of the "then" keyword, if
present.
</t>
<t>
Instances that fail to validate against this
keyword's subschema MUST also be valid against
the subschema value of the "else" keyword, if
present.
</t>
<t>
If <xref target="annotations">annotations</xref>
are being collected, they are collected from this
keyword's subschema in the usual way, including when
the keyword is present without either "then" or "else".
</t>
</section>
<section title="then">
<t>
This keyword's value MUST be a valid JSON Schema.
</t>
<t>
When "if" is present, and the instance successfully
validates against its subschema, then validation
succeeds against this keyword if the instance also
successfully validates against this keyword's subschema.
</t>
<t>
This keyword has no effect when "if" is absent, or
when the instance fails to validate against its
subschema. Implementations MUST NOT evaluate
the instance against this keyword, for either validation
or annotation collection purposes, in such cases.
</t>
</section>
<section title="else">
<t>
This keyword's value MUST be a valid JSON Schema.
</t>
<t>
When "if" is present, and the instance fails to
validate against its subschema, then validation
succeeds against this keyword if the instance
successfully validates against this keyword's subschema.
</t>
<t>
This keyword has no effect when "if" is absent, or
when the instance successfully validates against its
subschema. Implementations MUST NOT evaluate
the instance against this keyword, for either validation
or annotation collection purposes, in such cases.
</t>
</section>
<section title="dependentSchemas">
<t>
This keyword specifies subschemas that are evaluated if the instance
is an object and contains a certain property.
</t>
<t>
This keyword's value MUST be an object.
Each value in the object MUST be a valid JSON Schema.
</t>
<t>
If the object key is a property in the instance, the entire
instance must validate against the susbschema. Its use is
dependent on the presence of the property.
</t>
<t>
Omitting this keyword has the same behavior as an empty object.
</t>
</section>
</section>
</section>
<section title="Keywords for Applying Subschemas to Child Instances">
<t>
Each of these keywords defines a rule for applying its
subschema(s) to child instances, specifically object
properties and array items, and combining their results.
</t>
<section title="Keywords for Applying Subschemas to Arrays">
<section title="items">
<t>
The value of "items" MUST be either a valid JSON Schema or
an array of valid JSON Schemas.
</t>
<t>
If "items" is a schema, validation succeeds if all elements
in the array successfully validate against that schema.
</t>
<t>
If "items" is an array of schemas, validation succeeds if
each element of the instance validates against the schema at the
same position, if any.
</t>
<t>
This keyword produces an annotation value which is the largest
index to which this keyword applied a subschema. The value
MAY be a boolean true if a subschema was applied to every
index of the instance, such as when "items" is a schema.
</t>
<t>
Annotation results for "items" keywords from multiple
schemas applied to the same instance location are combined
by setting the combined result to true if any of the values
are true, and otherwise retaining the largest numerical value.
</t>
<t>
Omitting this keyword has the same assertion behavior as
an empty schema.
</t>
</section>
<section title="additionalItems" anchor="additionalItems">
<t>
The value of "additionalItems" MUST be a valid JSON Schema.
</t>
<t>
The behavior of this keyword depends on the presence and
annotation result of "items" within the same schema object.
If "items" is present, and its annotation result is a number,
validation succeeds if every instance element at an index
greater than that number validates against "additionalItems".
</t>
<t>
Otherwise, if "items" is absent or its annotation result
is the boolean true, "additionalItems" MUST be ignored.
</t>
<t>
If the "additionalItems" subschema is applied to any
positions within the instance array, it produces an
annotation result of boolean true, analogous to the
single schema behavior of "items". If any "additionalItems"
keyword from any subschema applied to the same instance
location produces an annotation value of true, then
the combined result from these keywords is also true.
</t>
<t>
Omitting this keyword has the same assertion behavior as
an empty schema.
</t>
<t>
Implementations MAY choose to implement or optimize this keyword
in another way that produces the same effect, such as by directly
checking for the presence and size of an "items" array.
Implementations that do not support annotation collection MUST do so.
</t>
</section>
<section title="contains">
<t>
The value of this keyword MUST be a valid JSON Schema.
</t>
<t>
An array instance is valid against "contains" if at least one of
its elements is valid against the given schema. This keyword
does not produce annotation results.
<cref>
Should it produce a set of the indices for which the
array element is valid against the subschema? "contains"
does not affect "additionalItems" or any other current
or proposed keyword, but the information could be useful,
and implementation that collect annotations need to
apply "contains" to every element anyway.
</cref>
</t>
</section>
</section>
<section title="Keywords for Applying Subschemas to Objects">
<section title="properties">
<t>
The value of "properties" MUST be an object.
Each value of this object MUST be a valid JSON Schema.
</t>
<t>
Validation succeeds if, for each name that appears in both
the instance and as a name within this keyword's value, the child
instance for that name successfully validates against the
corresponding schema.
</t>
<t>
The annotation result of this keyword is the set of instance
property names matched by this keyword. Annotation results
for "properties" keywords from multiple schemas applied to
the same instance location are combined by taking the union
of the sets.
</t>
<t>
Omitting this keyword has the same assertion behavior as
an empty object.
</t>
</section>
<section title="patternProperties">
<t>
The value of "patternProperties" MUST be an object. Each property name
of this object SHOULD be a valid regular expression, according to the
ECMA 262 regular expression dialect. Each property value of this object
MUST be a valid JSON Schema.
</t>
<t>
Validation succeeds if, for each instance name that matches any
regular expressions that appear as a property name in this keyword's value,
the child instance for that name successfully validates against each
schema that corresponds to a matching regular expression.
</t>
<t>
The annotation result of this keyword is the set of instance
property names matched by this keyword. Annotation results
for "patternProperties" keywords from multiple schemas applied to
the same instance location are combined by taking the union
of the sets.
</t>
<t>
Omitting this keyword has the same assertion behavior as
an empty object.
</t>
</section>
<section title="additionalProperties" anchor="additionalProperties">
<t>
The value of "additionalProperties" MUST be a valid JSON Schema.
</t>
<t>
The behavior of this keyword depends on the presence and
annotation results of "properties" and "patternProperties"
within the same schema object.
Validation with "additionalProperties" applies only to the child
values of instance names that do not appear in the annotation
results of either "properties" or "patternProperties".
</t>
<t>
For all such properties, validation succeeds if the child instance
validates against the "additionalProperties" schema.
</t>
<t>
The annotation result of this keyword is the set of instance
property names validated by this keyword's subschema.
Annotation results for "additionalProperties" keywords from
multiple schemas applied to the same instance location are combined
by taking the union of the sets.
</t>
<t>
Omitting this keyword has the same assertion behavior as
an empty schema.
</t>
<t>
Implementation MAY choose to implement or optimize this keyword
in another way that produces the same effect, such as by directly
checking the names in "properties" and the patterns in
"patternProperties" against the instance property set.
Implementations that do not support annotation collection MUST do so.
</t>
</section>
<section title="propertyNames">
<t>
The value of "propertyNames" MUST be a valid JSON Schema.
</t>
<t>
If the instance is an object, this keyword validates if every property name in
the instance validates against the provided schema.
Note the property name that the schema is testing will always be a string.
</t>
<t>
Omitting this keyword has the same behavior as an empty schema.
</t>
</section>
</section>
</section>
</section>
<section title="Usage for Hypermedia" anchor="hypermedia">
<t>
JSON has been adopted widely by HTTP servers for automated APIs and robots. This
section describes how to enhance processing of JSON documents in a more RESTful
manner when used with protocols that support media types and
<xref target="RFC8288">Web linking</xref>.
</t>
<section title='Linking to a Schema'>
<t>
It is RECOMMENDED that instances described by a schema provide a link to
a downloadable JSON Schema using the link relation "describedby", as defined by
<xref target="W3C.REC-ldp-20150226">Linked Data Protocol 1.0, section 8.1</xref>.
</t>
<t>
In HTTP, such links can be attached to any response using the
<xref target="RFC8288">Link header</xref>. An example of such a header would be:
</t>
<figure>
<artwork>
<![CDATA[
Link: <http://example.com/my-hyper-schema#>; rel="describedby"
]]>
</artwork>
</figure>
</section>
<section title='Identifying a Schema via a Media Type Parameter' anchor="parameter">
<t>
Media types MAY allow for a "schema" media type parameter, which gives
HTTP servers the ability to perform Content-Type Negotiation based on schema.
The media-type parameter MUST be a whitespace-separated list of URIs
(i.e. relative references are invalid).
</t>
<t>
When using the media type application/schema-instance+json, the "schema"
parameter MUST be supplied.
</t>
<t>
The schema URI is opaque and SHOULD NOT automatically be dereferenced.
If the implementation does not understand the semantics of the provided schema,
the implementation can instead follow the "describedby" links, if any, which may
provide information on how to handle the schema.
Since "schema" doesn't necessarily point to a network location, the
"describedby" relation is used for linking to a downloadable schema.
However, for simplicity, schema authors should make these URIs point to the same
resource when possible.
</t>
<t>
In HTTP, the media-type parameter would be sent inside the Content-Type header:
</t>
<figure>
<artwork>
<![CDATA[
Content-Type: application/json;
schema="http://example.com/my-hyper-schema#"
]]>
</artwork>
</figure>
<t>
Multiple schemas are whitespace separated:
</t>
<figure>
<artwork>
<![CDATA[
Content-Type: application/json;
schema="http://example.com/alice http://example.com/bob"
]]>
</artwork>
</figure>
<t>
<cref>
This paragraph assumes that we can register a "schema" link relation.
Should we instead specify something like "tag:json-schema.org,2017:schema"
for now?
</cref>
HTTP can also send the "schema" in a Link, though this may impact media-type
semantics and Content-Type negotiation if this replaces the media-type parameter
entirely:
</t>
<figure>
<artwork>
<![CDATA[
Link: </alice>;rel="schema", </bob>;rel="schema"
]]>
</artwork>
</figure>
</section>
<section title="Usage Over HTTP">
<t>
When used for hypermedia systems over a network,
<xref target="RFC7231">HTTP</xref> is frequently the protocol of choice for
distributing schemas. Misbehaving clients can pose problems for server
maintainers if they pull a schema over the network more frequently than
necessary, when it's instead possible to cache a schema for a long period of
time.
</t>
<t>
HTTP servers SHOULD set long-lived caching headers on JSON Schemas.
HTTP clients SHOULD observe caching headers and not re-request documents within
their freshness period.
Distributed systems SHOULD make use of a shared cache and/or caching proxy.
</t>
<t>
Clients SHOULD set or prepend a User-Agent header specific to the JSON Schema
implementation or software product. Since symbols are listed in decreasing order
of significance, the JSON Schema library name/version should precede the more
generic HTTP library name (if any). For example:
<figure>
<artwork>
<![CDATA[
User-Agent: product-name/5.4.1 so-cool-json-schema/1.0.2 curl/7.43.0
]]>
</artwork>
</figure>
</t>
<t>
Clients SHOULD be able to make requests with a "From" header so that server
operators can contact the owner of a potentially misbehaving script.
</t>
</section>
</section>
<section title="Security Considerations" anchor="security">
<t>
Both schemas and instances are JSON values. As such, all security considerations
defined in <xref target="RFC8259">RFC 8259</xref> apply.
</t>
<t>
Instances and schemas are both frequently written by untrusted third parties, to be
deployed on public Internet servers.
Validators should take care that the parsing and validating against schemas doesn't consume excessive
system resources.
Validators MUST NOT fall into an infinite loop.
</t>
<t>
Servers MUST ensure that malicious parties can't change the functionality of
existing schemas by uploading a schema with an pre-existing or very similar "$id".
</t>
<t>
Individual JSON Schema vocabularies are liable to also have their own security
considerations. Consult the respective specifications for more information.
</t>
<t>
Schema authors should take care with "$comment" contents, as a malicious
implementation can display them to end-users in violation of a spec, or
fail to strip them if such behavior is expected.
</t>
<t>
A malicious schema author could place executable code or other dangerous
material within a "$comment". Implementations MUST NOT parse or otherwise
take action based on "$comment" contents.
</t>
</section>
<section title="IANA Considerations">
<section title="application/schema+json">
<t>
The proposed MIME media type for JSON Schema is defined as follows:
<list>
<t>Type name: application</t>
<t>Subtype name: schema+json</t>
<t>Required parameters: N/A</t>
<t>
Encoding considerations: Encoding considerations are
identical to those specified for the "application/json"
media type. See <xref target="RFC8259">JSON</xref>.
</t>
<t>
Security considerations: See Section
<xref target="security" format="counter"></xref> above.
</t>
<t>
Interoperability considerations: See Sections
<xref target="language" format="counter"></xref>,
<xref target="integers" format="counter"></xref>, and
<xref target="regex" format="counter"></xref> above.
</t>
<t>
Fragment identifier considerations: See Section
<xref target="fragments" format="counter"></xref>
</t>
</list>
</t>
</section>
<section title="application/schema-instance+json">
<t>
The proposed MIME media type for JSON Schema Instances that require
a JSON Schema-specific media type is defined as follows:
<list>
<t>Type name: application</t>
<t>Subtype name: schema-instance+json</t>
<t>
Required parameters:
<list style="hanging">
<t hangText="schema:">
A non-empty list of space-separated URIs, each identifying
a JSON Schema resource. The instance SHOULD successfully
validate against at least one of these schemas.
Non-validating schemas MAY be included for purposes such
as allowing clients to make use of older versions of a schema
as long as the runtime instance validates against that
older version.
</t>
</list>
</t>
<t>
Encoding considerations: Encoding considerations are
identical to those specified for the "application/json"
media type. See <xref target="RFC8259">JSON</xref>.
</t>
<t>
Security considerations: See Section
<xref target="security" format="counter"></xref> above.
</t>
<t>
Interoperability considerations: See Sections
<xref target="language" format="counter"></xref>,
<xref target="integers" format="counter"></xref>, and
<xref target="regex" format="counter"></xref> above.
</t>
<t>
Fragment identifier considerations: See Section
<xref target="fragments" format="counter"></xref>
</t>
</list>
</t>
</section>
</section>
</middle>
<back>
<!-- References Section -->
<references title="Normative References">
&RFC2119;
&RFC3986;
&RFC6839;
&RFC6901;
&RFC8259;
&ldp;
<reference anchor="ecma262"
target="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf">
<front>
<title>ECMA 262 specification</title>
<author/>
<date/>
</front>
</reference>
</references>
<references title="Informative References">
&RFC7049;
&RFC7231;
&RFC8288;
&fragid-best-practices;
<reference anchor="json-schema-validation">
<front>
<title>JSON Schema Validation: A Vocabulary for Structural Validation of JSON</title>
<author initials="A." surname="Wright">
<organization/>
</author>
<author initials="H." surname="Andrews">
<organization/>
</author>
<author initials="G." surname="Luff">
<organization/>
</author>
<date year="2017" month="November"/>
</front>
<seriesInfo name="Internet-Draft" value="draft-handrews-json-schema-validation-02" />
</reference>
<reference anchor="json-hyper-schema">
<front>
<title>JSON Hyper-Schema: A Vocabulary for Hypermedia Annotation of JSON</title>
<author initials="H." surname="Andrews">
<organization/>
</author>
<author initials="A." surname="Wright">
<organization/>
</author>
<date year="2017" month="November"/>
</front>
<seriesInfo name="Internet-Draft" value="draft-handrews-json-schema-hyperschema-02" />
</reference>
</references>
<section title="Acknowledgments">
<t>
Thanks to
Gary Court,
Francis Galiegue,
Kris Zyp,
and Geraint Luff
for their work on the initial drafts of JSON Schema.
</t>
<t>
Thanks to
Jason Desrosiers,
Daniel Perrett,
Erik Wilde,
Ben Hutton,
Evgeny Poberezkin,
Brad Bowman,
Gowry Sankar,
Donald Pipowitch,
and Dave Finlay
for their submissions and patches to the document.
</t>
</section>
<section title="ChangeLog">
<t>
<cref>This section to be removed before leaving Internet-Draft status.</cref>
</t>
<t>
<list style="hanging">
<t hangText="draft-handrews-json-schema-02">
<list style="symbols">
<t>Moved "definitions" from the Validation specification here as "$defs"</t>
<t>Moved applicator keywords from the Validation specification as their own vocabulary</t>
<t>Moved the schema form of "dependencies" from the Validation specification as "dependentSchemas"</t>
</list>
</t>
<t hangText="draft-handrews-json-schema-01">
<list style="symbols">
<t>This draft is purely a clarification with no functional changes</t>
<t>Emphasized annotations as a primary usage of JSON Schema</t>
<t>Clarified $id by use cases</t>
<t>Exhaustive schema identification examples</t>
<t>Replaced "external referencing" with how and when an implementation might know of a schema from another doucment</t>
<t>Replaced "internal referencing" with how an implementation should recognized schema identifiers during parsing</t>
<t>Dereferencing the former "internal" or "external" references is always the same process</t>
<t>Minor formatting improvements</t>
</list>
</t>
<t hangText="draft-handrews-json-schema-00">
<list style="symbols">
<t>Make the concept of a schema keyword vocabulary more clear</t>
<t>Note that the concept of "integer" is from a vocabulary, not the data model</t>
<t>Classify keywords as assertions or annotations and describe their general behavior</t>
<t>Explain the boolean schemas in terms of generalized assertions</t>
<t>Reserve "$comment" for non-user-visible notes about the schema</t>
<t>Wording improvements around "$id" and fragments</t>
<t>Note the challenges of extending meta-schemas with recursive references</t>
<t>Add "application/schema-instance+json" media type</t>
<t>Recommend a "schema" link relation / parameter instead of "profile"</t>
</list>
</t>
<t hangText="draft-wright-json-schema-01">
<list style="symbols">
<t>Updated intro</t>
<t>Allowed for any schema to be a boolean</t>
<t>"$schema" SHOULD NOT appear in subschemas, although that may change</t>
<t>Changed "id" to "$id"; all core keywords prefixed with "$"</t>
<t>Clarify and formalize fragments for application/schema+json</t>
<t>Note applicability to formats such as CBOR that can be represented in the JSON data model</t>
</list>
</t>
<t hangText="draft-wright-json-schema-00">
<list style="symbols">
<t>Updated references to JSON</t>
<t>Updated references to HTTP</t>
<t>Updated references to JSON Pointer</t>
<t>Behavior for "id" is now specified in terms of RFC3986</t>
<t>Aligned vocabulary usage for URIs with RFC3986</t>
<t>Removed reference to draft-pbryan-zyp-json-ref-03</t>
<t>Limited use of "$ref" to wherever a schema is expected</t>
<t>Added definition of the "JSON Schema data model"</t>
<t>Added additional security considerations</t>
<t>Defined use of subschema identifiers for "id"</t>
<t>Rewrote section on usage with HTTP</t>
<t>Rewrote section on usage with rel="describedBy" and rel="profile"</t>
<t>Fixed numerous invalid examples</t>
</list>
</t>
<t hangText="draft-zyp-json-schema-04">
<list style="symbols">
<t>Salvaged from draft v3.</t>
<t>Split validation keywords into separate document.</t>
<t>Split hypermedia keywords into separate document.</t>
<t>Initial post-split draft.</t>
<t>Mandate the use of JSON Reference, JSON Pointer.</t>
<t>Define the role of "id". Define URI resolution scope.</t>
<t>Add interoperability considerations.</t>
</list>
</t>
<t hangText="draft-zyp-json-schema-00">
<list style="symbols">
<t>Initial draft.</t>
</list>
</t>
</list>
</t>
</section>
</back>
</rfc>