Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Fix learn schemas #378

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions learn/examples/address.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$id": "https://example.com/address.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "An address similar to http://microformats.org/wiki/h-card",
"type": "object",
"properties": {
Expand All @@ -27,7 +27,7 @@
}
},
"required": [ "locality", "region", "country-name" ],
"dependencies": {
"dependentRequired": {
"post-office-box": [ "street-address" ],
"extended-address": [ "street-address" ]
}
Expand Down
4 changes: 2 additions & 2 deletions learn/examples/calendar.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$id": "https://example.com/calendar.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of an event",
"type": "object",
"required": [ "dtstart", "summary" ],
Expand Down Expand Up @@ -41,7 +41,7 @@
"type": "string"
},
"geo": {
"$ref": "http://example.com/geographical-location.schema.json"
"$ref": "https://example.com/geographical-location.schema.json"
}
}
}
6 changes: 3 additions & 3 deletions learn/examples/card.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$id": "https://example.com/card.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"required": [ "familyName", "givenName" ],
Expand Down Expand Up @@ -61,8 +61,8 @@
}
}
},
"adr": { "$ref": "http://example.com/address.schema.json" },
"geo": { "$ref": "http://example.com/geographical-location.schema.json" },
"adr": { "$ref": "https://example.com/address.schema.json" },
"geo": { "$ref": "https://example.com/geographical-location.schema.json" },
"tz": {
"type": "string"
},
Expand Down
2 changes: 1 addition & 1 deletion learn/examples/geographical-location.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$id": "https://example.com/geographical-location.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Longitude and Latitude Values",
"description": "A geographical coordinate.",
"required": [ "latitude", "longitude" ],
Expand Down
94 changes: 47 additions & 47 deletions learn/file-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ We will start with a base JSON Schema expressing the following constraints:

Building out our JSON Schema from top to bottom:

* The [`$id`](http://json-schema.org/latest/json-schema-core.html#rfc.section.8.2) keyword.
* The [`$schema`](http://json-schema.org/latest/json-schema-core.html#rfc.section.7) keyword.
* The [`type`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1.1) validation keyword.
* The [`required`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.3) validation keyword.
* The [`properties`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.4) validation keyword.
* The [`$id`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.2.1) keyword.
* The [`$schema`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.1.1) keyword.
* The [`type`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.1.1) validation keyword.
* The [`required`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.5.3) validation keyword.
* The [`properties`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.2.1) validation keyword.
* The `/` key is empty now; We will fill it out later.
* The [`patternProperties`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.5) validation keyword.
* The [`patternProperties`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.2.2) validation keyword.
* This matches other property names via a regular expression. Note: it does not match `/`.
* The `^(/[^/]+)+$` key is empty now; We will fill it out later.
* The [`additionalProperties`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.6) validation keyword.
* The [`additionalProperties`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.2.3) validation keyword.
* The value here is `false` to constrain object properties to be either `/` or to match the regular expression.

> You will notice that the regular expression is explicitly anchored (with `^` and `$`): in JSON Schema, regular expressions (in `patternProperties` and in `pattern`) are not anchored by default.

```json
{
"$id": "http://example.com/fstab",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/fstab",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": [ "/" ],
"properties": {
Expand All @@ -104,32 +104,32 @@ We saw these keywords in the prior exercise: `$id`, `$schema`, `type`, `required

To this we add:

* The [`description`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.10.1) annotation keyword.
* The [`oneOf`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7.3) keyword.
* The [`$ref`](http://json-schema.org/latest/json-schema-core.html#rfc.section.8.3) keyword.
* The [`description`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.9.1) annotation keyword.
* The [`oneOf`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.2.1.3) keyword.
* The [`$ref`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.2.3.1) keyword.
* In this case, all references used are local to the schema using a relative fragment URI (`#/...`).
* The [`definitions`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.9) keyword.
* The [`$defs`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.2.4) keyword.
* Including several key names which we will define later.

```json
{
"$id": "http://example.com/entry-schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/entry-schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "JSON Schema for an fstab entry",
"type": "object",
"required": [ "storage" ],
"properties": {
"storage": {
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/diskDevice" },
{ "$ref": "#/definitions/diskUUID" },
{ "$ref": "#/definitions/nfs" },
{ "$ref": "#/definitions/tmpfs" }
{ "$ref": "#/$defs/diskDevice" },
{ "$ref": "#/$defs/diskUUID" },
{ "$ref": "#/$defs/nfs" },
{ "$ref": "#/$defs/tmpfs" }
]
}
},
"definitions": {
"$defs": {
"diskDevice": {},
"diskUUID": {},
"nfs": {},
Expand All @@ -142,32 +142,32 @@ To this we add:

Let's now extend this skeleton to add constraints to some of the properties.

* Our `fstype` key uses the [`enum`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1.2) validation keyword.
* Our `fstype` key uses the [`enum`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.1.2) validation keyword.
* Our `options` key uses the following:
* The `type` validation keyword (see above).
* The [`minItems`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4.4) validation keyword.
* The [`items`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4.1) validation keyword.
* The [`uniqueItems`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4.5) validation keyword.
* The [`minItems`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.2) validation keyword.
* The [`items`](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.1.2) validation keyword.
* The [`uniqueItems`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.3) validation keyword.
* Together these say: `options` must be an array, and the items therein must be strings, there must be at least one item, and all items should be unique.
* We have a `readonly` key.

With these added constraints, the schema now looks like this:

```json
{
"$id": "http://example.com/entry-schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/entry-schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "JSON Schema for an fstab entry",
"type": "object",
"required": [ "storage" ],
"properties": {
"storage": {
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/diskDevice" },
{ "$ref": "#/definitions/diskUUID" },
{ "$ref": "#/definitions/nfs" },
{ "$ref": "#/definitions/tmpfs" }
{ "$ref": "#/$defs/diskDevice" },
{ "$ref": "#/$defs/diskUUID" },
{ "$ref": "#/$defs/nfs" },
{ "$ref": "#/$defs/tmpfs" }
]
},
"fstype": {
Expand All @@ -185,7 +185,7 @@ With these added constraints, the schema now looks like this:
"type": "boolean"
}
},
"definitions": {
"$defs": {
"diskDevice": {},
"diskUUID": {},
"nfs": {},
Expand All @@ -198,7 +198,7 @@ With these added constraints, the schema now looks like this:

One new keyword is introduced here:

* The [`pattern`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.3.3) validation keyword notes the `device` key must be an absolute path starting with */dev*.
* The [`pattern`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.3.3) validation keyword notes the `device` key must be an absolute path starting with */dev*.

```json
{
Expand Down Expand Up @@ -246,7 +246,7 @@ We do have a new key: `label` and the `pattern` validation keyword states it mus

We find another new keyword:

* The [`format`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.7) annotation and assertion keyword.
* The [`format`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.7) annotation and assertion keyword.

```json
{
Expand Down Expand Up @@ -276,8 +276,8 @@ We find another new keyword:

Our last definition introduces two new keywords:

* The [`minimum`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.4) validation keyword.
* The [`maximum`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.2) validation keword.
* The [`minimum`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.4) validation keyword.
* The [`maximum`](http://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.2) validation keword.
* Together these require the size be between 16 and 512, inclusive.

```json
Expand All @@ -303,19 +303,19 @@ The resulting schema is quite large:

```json
{
"$id": "http://example.com/entry-schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/entry-schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "JSON Schema for an fstab entry",
"type": "object",
"required": [ "storage" ],
"properties": {
"storage": {
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/diskDevice" },
{ "$ref": "#/definitions/diskUUID" },
{ "$ref": "#/definitions/nfs" },
{ "$ref": "#/definitions/tmpfs" }
{ "$ref": "#/$defs/diskDevice" },
{ "$ref": "#/$defs/diskUUID" },
{ "$ref": "#/$defs/nfs" },
{ "$ref": "#/$defs/tmpfs" }
]
},
"fstype": {
Expand All @@ -333,7 +333,7 @@ The resulting schema is quite large:
"type": "boolean"
}
},
"definitions": {
"$defs": {
"diskDevice": {
"properties": {
"type": {
Expand Down Expand Up @@ -404,16 +404,16 @@ Coming full circle we use the `$ref` keyword to add our entry schema into the ke

```json
{
"$id": "http://example.com/fstab",
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/fstab",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": [ "/" ],
"properties": {
"/": { "$ref": "http://example.com/entry-schema" }
"/": { "$ref": "https://example.com/entry-schema" }
},
"patternProperties": {
"^(/[^/]+)+$": { "$ref": "http://example.com/entry-schema" }
"^(/[^/]+)+$": { "$ref": "https://example.com/entry-schema" }
},
"additionalProperties": false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot on that trailing comma!

"additionalProperties": false
}
```
Loading