$schema : URI
$schema
URIThis keyword is both used as a JSON Schema dialect identifier and as a reference to a JSON Schema which describes the set of valid schemas written for this particular dialect.
Value | This keyword must be set to an absolute URI as defined by RFC 3986 |
---|---|
Kind | Identifier |
Applies To | Any |
Base Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 3 |
Vocabulary | Core |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.1.1 |
Metaschema | https://json-schema.org/draft/2020-12/meta/core |
Official Tests | None |
Default | Implementation or context dependent |
Annotation | None |
Affected By | None |
Affects | None |
Also See |
The $schema
keyword serves to explicitly associate a schema or subschema with
the JSON Schema dialect that defines it, where the dialect is the identifier of
a meta-schema that defines the vocabularies in use and imposes syntactic
constraints on its schema instances. If the $schema
keyword is not declared,
the schema inherits its context-specific or implementation-specific default
dialect.
Digging Deeper
It is common to avoid the
$schema
keyword when working
with OpenAPI. This is possible because the OpenAPI
specification clearly documents what the default JSON Schema dialect is for
every version. For example, OpenAPI
v3.1.1 defines
the default dialect as https://spec.openapis.org/oas/3.1/dialect/base
.
Strictly-compliant JSON Schema implementations will refuse to process a schema whose dialect cannot be unambiguously determined.
Best Practice
To avoid undefined behavior, it is generally recommended to always explicitly set the dialect of a schema using the
$schema
keyword. This
ensures that less strict implementations unambiguously know how to process the
schema and don’t attempt to guess.
Note that the $schema
keyword can occur multiple times in the same schema,
and not only at the top-level. This is often the case when performing JSON
Schema
Bundling
to inline externally referenced schemas that might be based on different
dialects of JSON Schema.
A schema is considered syntactic valid if it successfully validates against its
dialect meta-schema. You can validate a schema against its meta-schema using
the jsonschema metaschema
.
For example:
$ jsonschema metaschema my-schema.json
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
}
{
"items": [ { "type": "number" } ]
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"price": { "type": "number" },
"discount": {
"$ref": "#/$defs/discount"
}
},
"$defs": {
"discount": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "number"
}
}
}