You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's pretend we're interacting with a JSON based product catalog. This catalog has a product which has an *id*, a *name*, a *price*, and an optional set of *tags*.
6
+
Let's pretend we're interacting with a JSON based product catalog. This catalog has a product which has:
7
7
8
-
### Example JSON data for a product API
8
+
* An identifier: `productId`
9
+
* A product name: `name`
10
+
* A selling cost for the consumer: `price`
11
+
* An optional set of tags: `tags`.
9
12
10
-
An example product in this API is:
13
+
For example:
11
14
12
15
```json
13
-
{% include example1/instance.json %}
16
+
{
17
+
"productId": 1,
18
+
"name": "A green door",
19
+
"price": 12.50,
20
+
"tags": [ "home", "green" ]
21
+
}
14
22
```
15
23
16
-
While generally straightforward, that example leaves some open questions. For example, one may ask:
24
+
While generally straightforward, the example leaves some open questions. For example, one may ask:
17
25
18
-
-What is id?
19
-
-Is name required?
20
-
-Can price be 0?
21
-
-Are all tags strings?
26
+
*What is `productId`?
27
+
*Is name `required`?
28
+
*Can the `price` be zero (0)?
29
+
*Are all of the `tags` string values?
22
30
23
-
When you're talking about a data format, you want to have metadata about what fields mean, and what valid inputs for those fields are. JSON schema is a specification for standardizing how to answer those questions for JSON data.
31
+
When you're talking about a data format, you want to have metadata about what keys mean, including the valid inputs for those keys. **JSON Schema** is a proposed IETF standard how to answer those questions for data.
24
32
25
-
Starting the schema
26
-
-------------------
33
+
## Starting the schema
27
34
28
-
To start a schema definition, let's begin with a basic JSON schema:
35
+
To start a schema definition, let's begin with a basic JSON schema.
36
+
37
+
We start with four properties called **keywords** which are expressed as [JSON](https://www.json.org/) keys.
38
+
39
+
> Yes. the standard uses a JSON data document to describe data documents, most often that are also JSON data documents but could be in any number of other content types like `text/xml`.
40
+
41
+
* The [`$schema`](http://json-schema.org/latest/json-schema-core.html#rfc.section.7) keyword states that this schema is written according to the a specific draft of the standard and used for a variety of reasons, primarily version control.
42
+
* The [`$id`](http://json-schema.org/latest/json-schema-core.html#rfc.section.8.2) keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against.
43
+
* The [`title`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.10.1) and [`description`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.10.1) annotation keywords are descriptive only. They do not add constraints to the data being validated. The intent of the schema is stated with these two keywords.
44
+
* The [`type`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1.1) validation keyword defines the first constraint on our JSON data and in this case it has to be a JSON Object.
The above schema has four properties called *keywords*. The *title* and *description* keywords are descriptive only, in that they do not add constraints to the data being validated. The intent of the schema is stated with these two keywords (that is, this schema describes a product).
56
+
## Defining the properties
35
57
36
-
The *type* keyword defines the first constraint on our JSON data: it has to be a JSON Object.
58
+
Next let's answer our previous questions about this API, starting with `productId`.
37
59
38
-
Finally, the *$schema* keyword states that this schema is written according to the draft-06 specification.
60
+
### What is productId?
39
61
40
-
Defining the properties
41
-
-----------------------
62
+
`productId` is a numeric value that uniquely identifies a product. Since this is the canonical identifier for a product, it doesn't make sense to have a product without one, so it is required.
42
63
43
-
Next let's answer our previous questions about this API, starting with id.
64
+
In JSON Schema terms, we update our schema to add:
44
65
45
-
### What is id?
66
+
* The [`properties`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.4) keyword.
67
+
* The `productId` key.
68
+
*`description` and `type` keywords for `productId`.
69
+
* The [`required`](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.3) keyword listing `productId`.
46
70
47
-
*id* is a numeric value that uniquely identifies a product. Since this is the canonical identifier for a product, it doesn't make sense to have a product without one, so it is required.
48
-
49
-
In JSON Schema terms, we can update our schema to:
"description": "The unique identifier for a product",
82
+
"type": "integer"
83
+
}
84
+
},
85
+
"required": [ "productId" ]
86
+
}
53
87
```
54
88
55
89
### Is name required?
56
90
57
-
*name* is a string value that describes a product. Since there isn't much to a product without a name, it also is required. Adding this gives us the schema:
91
+
Yes.
92
+
93
+
*`name` is a string value that describes a product. Since there isn't much to a product without a name it also is required.
94
+
* Since the `required` keyword is an array of strings we can note multiple keys as required, now including `name`.
"description": "The unique identifier for a product",
106
+
"type": "integer"
107
+
},
108
+
"name": {
109
+
"description": "Name of the product",
110
+
"type": "string"
111
+
}
112
+
},
113
+
"required": [ "productId", "name" ]
114
+
}
61
115
```
62
116
63
-
### Can price be 0?
117
+
### Can price be zero (0)?
118
+
119
+
No. According to the store owner there are no free products. ;)
64
120
65
-
According to Acme's docs, there are no free products. So we need to specify *exclusiveMinimum*. If we wanted to include 0 as a valid price, we would have specified *minimum* instead. Therefore, we can update our schema with *price*:
121
+
* We need to specify this using the [`exclusiveMinimum` validation keyword](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.5).
122
+
* If we wanted to include zero as a valid price we would have specified the [`minimum` validation keyword](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.4).
"description": "The unique identifier for a product",
171
+
"type": "integer"
172
+
},
173
+
"name": {
174
+
"description": "Name of the product",
175
+
"type": "string"
176
+
},
177
+
"price": {
178
+
"type": "number",
179
+
"exclusiveMinimum": 0
180
+
},
181
+
"tags": {
182
+
"type": "array",
183
+
"items": {
184
+
"type": "string"
185
+
},
186
+
"minItems": 1,
187
+
"uniqueItems": true
188
+
}
189
+
},
190
+
"required": ["productId", "name", "price"]
191
+
}
84
192
```
85
193
86
-
Summary
87
-
-------
194
+
## Summary
88
195
89
196
The above example is by no means definitive of all the types of data JSON schema can define. For more definitive information see the [full standard draft](#definitions).
90
197
@@ -95,11 +202,85 @@ And also, since JSON Schema defines a reference schema for a geographic ___location
0 commit comments