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
<p>The above schema has a <em>description</em> saying we are going to describe a product, a <em>type</em> saying our product is a JSON object (versus a simple datapoint like a string/number), and a <em>properties</em>section for describing the data in that object.</p>
44
58
@@ -48,22 +62,98 @@ <h4>What is id?</h4>
48
62
<p><em>id</em> 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.</p>
49
63
50
64
<p>In JSON Schema terms, we can update our schema to:</p>
"description": "The unique identifier for a product",
72
+
"type": "integer"
73
+
}
74
+
},
75
+
"required": ["id"]
76
+
}
77
+
</pre>
52
78
53
79
<h4>Is name required?</h4>
54
80
<p><em>name</em> is a string value that describes a product. Since there isn't much to a product, it also is required. Adding this gives us the schema:</p>
"description": "The unique identifier for a product",
89
+
"type": "integer"
90
+
},
91
+
"name": {
92
+
"description": "Name of the product",
93
+
"type": "string"
94
+
}
95
+
},
96
+
"required": ["id", "name"]
97
+
}
98
+
</pre>
57
99
58
100
<h4>Can price be 0?</h4>
59
101
<p>According to Acme's docs, there are no free products. In JSON schema a number can have a minimum. By default this minimum is inclusive, so we need to specify <em>exclusiveMinimum</em>. Therefore we can update our schema with <em>price</em>:</p>
"description": "The unique identifier for a product",
110
+
"type": "integer"
111
+
},
112
+
"name": {
113
+
"description": "Name of the product",
114
+
"type": "string"
115
+
},
116
+
"price": {
117
+
"type": "number",
118
+
"minimum": 0,
119
+
"exclusiveMinimum": true
120
+
}
121
+
},
122
+
"required": ["id", "name", "price"]
123
+
}
124
+
</pre>
62
125
63
126
<h4>Are all tags strings?</h4>
64
127
<p>Finally, we come to the tags property. Unlike the previous properties, tags have many values, and is represented as a JSON array. According to Acme's docs, all tags must be strings, but you aren't required to specify tags. We simply leave <em>tags</em> out of the list of required properties, giving us:</p>
"description": "The unique identifier for a product",
136
+
"type": "integer"
137
+
},
138
+
"name": {
139
+
"description": "Name of the product",
140
+
"type": "string"
141
+
},
142
+
"price": {
143
+
"type": "number",
144
+
"minimum": 0,
145
+
"exclusiveMinimum": true
146
+
},
147
+
"tags": {
148
+
"type": "array",
149
+
"items": {
150
+
"type": "string"
151
+
}
152
+
}
153
+
},
154
+
"required": ["id", "name", "price"]
155
+
}
156
+
</pre>
67
157
68
158
<h3>Example summary</h3>
69
159
<p>The above example is by no means definitive of all the types of data JSON schema can define. For more definitive information see the <ahref="#definitions">full standard draft</a>.</p>
@@ -73,15 +163,91 @@ <h3>Example summary</h3>
73
163
<p>And also, since JSON Schema defines a reference schema for a geographic location, instead of coming up with our own, we'll reference the <ahref="http://json-schema.org/geo">canonical one</a>.</p>
0 commit comments