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

Commit dbd6b03

Browse files
authored
Merge pull request #84 from mdboom/draft7
Main updates for Draft 7
2 parents 76d938a + cb69070 commit dbd6b03

15 files changed

+435
-57
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ branches:
1111
install:
1212
- pip install sphinx
1313
- pip install sphinx_bootstrap_theme
14-
- pip install git+https://github.com/Julian/jsonschema@97e42bfe2ec8767ffbb76ddb84625c06cbaf0184
14+
- pip install git+https://github.com/Julian/jsonschema@8cc6a5af0b5ab343707c0c71021c477651aa479b
1515
- bash -x ./install-texlive.sh
1616
- export PATH=$PWD/texlive/bin/x86_64-linux:$PATH
1717
script:

install-texlive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ tlmgr install cmap ec fancybox titlesec framed fancyvrb threeparttable mdwtools
1919
changepage xcolor mdframed l3kernel l3packages etoolbox needspace \
2020
pgf times upquote helvetic fontaxes mweights ms float fncychap \
2121
tabulary capt-of eqparbox environ trimspaces latexmk ucs varwidth \
22-
overlock xkeyval
22+
overlock xkeyval niceframe-type1

source/about.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ for now. They are explained in subsequent chapters.
156156
"properties": {
157157
"first_name": { "type": "string" },
158158
"last_name": { "type": "string" },
159-
"birthday": { "type": "string", "format": "date-time" },
159+
"birthday": { "type": "string", "format": "date" },
160160
"address": {
161161
"type": "object",
162162
"properties": {

source/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
sys.path.insert(0, os.path.abspath(os.path.dirname('__file__')))
2121

2222
# The standard of JSON Schema to test the examples against
23-
jsonschema_standard = 6
23+
jsonschema_standard = 7
2424

2525
rst_prolog = """
2626
.. role:: new
@@ -63,9 +63,9 @@
6363
# built documents.
6464
#
6565
# The short X.Y version.
66-
version = '6.0'
66+
version = '7.0'
6767
# The full version, including alpha/beta/rc tags.
68-
release = '6.0'
68+
release = '7.0'
6969

7070
# The language for content autogenerated by Sphinx. Refer to documentation
7171
# for a list of supported languages.

source/conventions.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ Draft-specific notes
3939
--------------------
4040

4141
The JSON Schema standard has been through a number of revisions or "drafts". The
42-
most important are Draft 6, the most recent at the time of this writing, and
42+
most important are Draft 7, the most recent at the time of this writing, and
4343
Draft 4, on which a lot of production software was built, and the draft for
4444
which an earlier version of this book was written.
4545

46-
The text is written to encourage the use of Draft 6 and gives
47-
priority to the latest conventions and features, but where it differs from Draft
48-
4, those differences are highlighted in special call-outs. If you only wish to
49-
target Draft 6, you can safely ignore those sections.
46+
The text is written to encourage the use of Draft 7 and gives priority to the
47+
latest conventions and features, but where it differs from earlier drafts, those
48+
differences are highlighted in special call-outs. If you only wish to target
49+
Draft 7, you can safely ignore those sections.
5050

51-
|draft6|
51+
|draft7|
5252

5353
.. draft_specific::
5454

source/credits.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ Acknowledgments
44
Michael Droettboom wishes to thank the following contributors:
55

66
- Alexander Kjeldaas
7+
- Alexander Lang
78
- Anders D. Johnson
89
- Armand Abric
910
- Ben Hutton
1011
- Brandon Wright
11-
- btubbs
12+
- Brent Tubbs
1213
- Chris Carpenter
1314
- Christopher Mark Gore
1415
- David Branner
@@ -18,8 +19,12 @@ Michael Droettboom wishes to thank the following contributors:
1819
- Fenhl
1920
- forevermatt
2021
- goldaxe
22+
- Henry Andrews
23+
- Hervé
24+
- Hongwei
2125
- Jesse Claven
2226
- Koen Rouwhorst
2327
- Mike Kobit
2428
- Oliver Kurmis
29+
- Sam Blackman
2530
- Vincent Jacques

source/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ validator---just yet.
2424

2525
.. note::
2626

27-
This book describes JSON Schema draft 6. The most recent version is draft 7
28-
--- stay tuned, updates are coming! Earlier and later versions of JSON Schema
29-
are not completely compatible with the format described here.
27+
This book describes JSON Schema draft 7. Earlier versions of JSON Schema
28+
are not completely compatible with the format described here, but for the
29+
most part, those differences are noted in the text.
3030

3131
**Where to begin?**
3232

source/reference/conditionals.rst

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
.. index::
2+
single: conditionals
3+
single: conditionals; if
4+
single: conditionals; then
5+
single: conditionals; else
6+
single: if
7+
single: then
8+
single: else
9+
10+
.. _conditionals:
11+
12+
Applying subschemas conditionally
13+
=================================
14+
15+
|draft7| ``if``, ``then`` and ``else`` keywords
16+
17+
The ``if``, ``then`` and ``else`` keywords allow the application of a subschema
18+
based on the outcome of another schema, much like the ``if/then/else``
19+
constructs you've probably seen in traditional programming languages.
20+
21+
If ``if`` is valid, ``then`` must also be valid (and ``else`` is ignored.) If
22+
``if`` is invalid, ``else`` must also be valid (and ``then`` is ignored).
23+
24+
We can put this in the form of a truth table, showing the combinations of when
25+
``if``, ``then``, and ``else`` are valid and the resulting validity of the
26+
entire schema:
27+
28+
==== ==== ==== ============
29+
if then else whole schema
30+
==== ==== ==== ============
31+
X X
32+
X
33+
X X X
34+
X
35+
X X
36+
X X X
37+
X X X X
38+
==== ==== ==== ============
39+
40+
For example, let's say you wanted to write a schema to handle addresses in the
41+
United States and Canada. These countries have different postal code formats,
42+
and we want to select which format to validate against based on the country. If
43+
the address is in the United States, the ``postal_code`` field is a "zipcode":
44+
five numeric digits followed by an optional four digit suffix. If the address is
45+
in Canada, the ``postal_code`` field is a six digit alphanumeric string where
46+
letters and numbers alternate.
47+
48+
.. schema_example::
49+
50+
{
51+
"type": "object",
52+
"properties": {
53+
"street_address": {
54+
"type": "string"
55+
},
56+
"country": {
57+
"enum": ["United States of America", "Canada"]
58+
}
59+
},
60+
"if": {
61+
"properties": { "country": { "const": "United States of America" } }
62+
},
63+
"then": {
64+
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
65+
},
66+
"else": {
67+
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
68+
}
69+
}
70+
--
71+
{
72+
"street_address": "1600 Pennsylvania Avenue NW",
73+
"country": "United States of America",
74+
"postal_code": "20500"
75+
}
76+
--
77+
{
78+
"street_address": "24 Sussex Drive",
79+
"country": "Canada",
80+
"postal_code": "K1M 1M4"
81+
}
82+
--X
83+
{
84+
"street_address": "24 Sussex Drive",
85+
"country": "Canada",
86+
"postal_code": "10000"
87+
}
88+
89+
Unfortunately, this approach above doesn't scale to more than two countries. You
90+
can, however, wrap pairs of ``if`` and ``then`` inside an ``allOf`` to create
91+
something that would scale. In this example, we'll use United States and
92+
Canadian postal codes, but also add Netherlands postal codes, which are 4 digits
93+
followed by two letters. It's left as an exercise to the reader to expand this
94+
to the remaining postal codes of the world.
95+
96+
.. schema_example::
97+
98+
{
99+
"type": "object",
100+
"properties": {
101+
"street_address": {
102+
"type": "string"
103+
},
104+
"country": {
105+
"enum": ["United States of America", "Canada", "Netherlands"]
106+
}
107+
},
108+
"allOf": [
109+
{
110+
"if": {
111+
"properties": { "country": { "const": "United States of America" } }
112+
},
113+
"then": {
114+
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
115+
}
116+
},
117+
{
118+
"if": {
119+
"properties": { "country": { "const": "Canada" } }
120+
},
121+
"then": {
122+
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
123+
}
124+
},
125+
{
126+
"if": {
127+
"properties": { "country": { "const": "Netherlands" } }
128+
},
129+
"then": {
130+
"properties": { "postal_code": { "pattern": "[0-9]{4} [A-Z]{2}" } }
131+
}
132+
}
133+
]
134+
}
135+
--
136+
{
137+
"street_address": "1600 Pennsylvania Avenue NW",
138+
"country": "United States of America",
139+
"postal_code": "20500"
140+
}
141+
--
142+
{
143+
"street_address": "24 Sussex Drive",
144+
"country": "Canada",
145+
"postal_code": "K1M 1M4"
146+
}
147+
--
148+
{
149+
"street_address": "Adriaan Goekooplaan",
150+
"country": "Netherlands",
151+
"postal_code": "2517 JX"
152+
}
153+
--X
154+
{
155+
"street_address": "24 Sussex Drive",
156+
"country": "Canada",
157+
"postal_code": "10000"
158+
}
159+
160+

source/reference/generic.rst

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@ This chapter lists some miscellaneous properties that are available
55
for all JSON types.
66

77
.. index::
8-
single: metadata
8+
single: annotation
99
single: title
1010
single: description
11+
single: default
1112
single: examples
1213

13-
.. _metadata:
14+
.. _annotation:
1415

15-
Metadata
16-
--------
16+
Annotations
17+
-----------
1718

18-
JSON Schema includes a few keywords, ``title``, ``description``, ``default``, and
19+
JSON Schema includes a few keywords, ``title``, ``description``, ``default``,
1920
``examples`` that aren't strictly used for validation, but are used to describe
2021
parts of a schema.
2122

22-
The ``title`` and ``description`` keywords must be strings. A "title"
23-
will preferably be short, whereas a "description" will provide a more
24-
lengthy explanation about the purpose of the data described by the
25-
schema. Neither are required, but they are encouraged for good
26-
practice, and can make your schema "self-documenting".
23+
None of these "annotation" keywords are required, but they are encouraged for
24+
good practice, and can make your schema "self-documenting".
25+
26+
The ``title`` and ``description`` keywords must be strings. A "title" will
27+
preferably be short, whereas a "description" will provide a more lengthy
28+
explanation about the purpose of the data described by the schema.
2729

2830
The ``default`` keyword specifies a default value for an item. JSON
2931
processing tools may use this information to provide a default value
@@ -50,6 +52,25 @@ required. There is no need to duplicate the ``default`` value in the
5052
]
5153
}
5254

55+
.. index::
56+
single: comment
57+
single: $comment
58+
59+
.. _comments:
60+
61+
Comments
62+
--------
63+
64+
|draft7| ``$comment``
65+
66+
The ``$comment`` keyword is strictly intended for adding comments to the JSON
67+
schema source. Its value must always be a string. Unlike the annotations
68+
``title``, ``description`` and ``examples``, JSON schema implementations aren't
69+
allowed to attach any meaning or behavior to it whatsoever, and may even strip
70+
them at any time. Therefore, they are useful for leaving notes to future editors
71+
of a JSON schema, (which is quite likely your future self), but should not be
72+
used to communicate to users of the schema.
73+
5374
.. index::
5475
single: enum
5576
single: enumerated values

source/reference/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ JSON Schema Reference
88

99
type.rst
1010
string.rst
11+
regular_expressions.rst
1112
numeric.rst
1213
object.rst
1314
array.rst
1415
boolean.rst
1516
null.rst
1617
generic.rst
18+
non_json_data.rst
1719
combining.rst
20+
conditionals.rst
1821
schema.rst
19-
regular_expressions.rst

0 commit comments

Comments
 (0)