|
| 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. |
0 commit comments