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
Copy file name to clipboardExpand all lines: examples.html
+35-11Lines changed: 35 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -36,34 +36,58 @@ <h3>Example JSON data for a product API</h3>
36
36
37
37
<p>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.</p>
38
38
<h3>Example schema for the product API</h3>
39
-
<p>To start a schema definition, let's begin with a basic to JSON schema.</p>
39
+
<p>To start a schema definition, let's begin with a basic JSON schema:</p>
<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>
43
+
<p>The above schema has four properties called <em>keywords</em>.
44
+
45
+
The <em>title</em> and <em>description</em> keywords are descriptive only, in that they do not add
46
+
constraints to the data being validated. The intent of the schema is stated with these two keywords
47
+
(that is, this schema describes a product).</p>
48
+
49
+
<p>The <em>type</em> keyword defines the first constraint on our JSON data: it has to be a JSON
50
+
Object.</p>
51
+
52
+
<p>Finally, the <em>$schema</em> keyword states that this schema is written according to the draft
53
+
v4 specification.</p>
44
54
45
55
<p>Next let's answer our previous questions about this API, starting with id.</p>
46
56
47
57
<h4>What is id?</h4>
48
58
<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
59
50
60
<p>In JSON Schema terms, we can update our schema to:</p>
<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>
64
+
<p><em>name</em> is a string value that describes a product. Since there isn't
65
+
much to a product without a name, it also is required. Adding this gives us the schema:</p>
<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>
70
+
<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>
<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>
75
+
<p>Finally, we come to the <em>tags</em> property. Unlike the previous
76
+
properties, tags have many values, and is represented as a JSON array. According
77
+
to Acme's docs, all tags must be strings, but you aren't required to specify
78
+
tags. We simply leave <em>tags</em> out of the list of required properties.</p>
79
+
80
+
<p>However, Acme's docs add two constraints:</p>
81
+
82
+
<ul>
83
+
<li>there must be at least one tag,</li>
84
+
<li>all tags must be unique.</li>
85
+
</ul>
86
+
87
+
<p>The first constraint can be added with <em>minItems</em>, and the second one by
88
+
specifying <em>uniqueItems</em> as being true:</p>
<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>
0 commit comments