Skip to content

Commit c07e1a7

Browse files
authored
Merge pull request #1614 from json-schema-org/gregsdennis/meta-schema-extensions
meta-schema extensions & disallowing unknown keywords
2 parents fb0a2ee + fa58ebc commit c07e1a7

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

specs/meta/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# JSON Schema Meta-Schema
2+
3+
The *meta.json* file contains the meta-schema for the in-progress JSON Schema
4+
specifications. You can find the latest published version on the [JSON Schema
5+
website](https://json-schema.org).
6+
7+
## Table of Contents
8+
9+
## Meta-Schemas
10+
11+
A meta-schema is a schema that itself describes a schema. This is possible
12+
because JSON Schema can be written as JSON.
13+
14+
Furthermore, the JSON Schema meta-schema is self-validating. That is, its JSON
15+
form validates against the meta-schema.
16+
17+
## Extensions and Unknown Keywords
18+
19+
The JSON Schema specification allows for extension keywords to be introduced,
20+
however it also disallows unknown keywords. While seemingly contradictory, the
21+
meta-schema has been set up to allow for extension.
22+
23+
For this example, we'll add two hypothetical keywords: `odds` and `evens`, which
24+
validate the odd and even indexed values in an array, respectively.
25+
26+
First, create a schema that just defines the new keywords.
27+
28+
```json
29+
{
30+
"$schema": "https://json-schema.org/1",
31+
"$id": "https://example.com/schema/odds-evens",
32+
33+
"properties": {
34+
"odds": { "$dynamicRef": "#meta" },
35+
"evens": { "$dynamicRef": "#meta" }
36+
}
37+
}
38+
```
39+
40+
Second, create a new meta-schema that
41+
42+
- references the above schema
43+
- references the JSON Schema meta-schema
44+
- includes an extension point in a `$defs`
45+
46+
```json
47+
{
48+
"$schema": "https://json-schema.org/1",
49+
"$id": "https://example.com/schema/odds-evens-extension",
50+
51+
"$ref": "https://json-schema.org/1",
52+
53+
"$defs": {
54+
"extension": {
55+
"$dynamicAnchor": "extension",
56+
"$ref": "https://example.com/schema/odds-evens"
57+
}
58+
}
59+
}
60+
```
61+
62+
Now you can use `https://example.com/schema/odds-evens-extension` in your
63+
schemas to make use of the new `odds` and `evens` keywords.
64+
65+
```json
66+
{
67+
"$schema": "https://example.com/schema/odds-evens-extension",
68+
"$id": "https://example.com/schema/model",
69+
70+
"type": "array",
71+
"odds": { "type": "string" },
72+
"evens": { "type": "number" }
73+
}
74+
```
75+
76+
This schema will validate arrays whose items alternate between strings and
77+
numbers.

specs/meta/meta.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@
152152
"propertyNames": {
153153
"pattern": "^[^$]|^\\$(id|schema|ref|anchor|dynamicRef|dynamicAnchor|comment|defs)$"
154154
},
155+
"$dynamicRef": "#extension",
156+
"unevaluatedProperties": false,
155157
"$defs": {
158+
"extension": {
159+
"$dynamicAnchor": "extension"
160+
},
156161
"anchorString": {
157162
"type": "string",
158163
"pattern": "^[A-Za-z_][-A-Za-z0-9._]*$"

0 commit comments

Comments
 (0)