Skip to content

Commit db2b0c2

Browse files
committed
update the meta-schema to allow for extension; add readme with basic how-to
1 parent e929dbc commit db2b0c2

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

specs/meta/README.md

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