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

Main updates for Draft 7 #84

Merged
merged 21 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ branches:
install:
- pip install sphinx
- pip install sphinx_bootstrap_theme
- pip install git+https://github.com/Julian/jsonschema@97e42bfe2ec8767ffbb76ddb84625c06cbaf0184
- pip install git+https://github.com/Julian/jsonschema@8cc6a5af0b5ab343707c0c71021c477651aa479b
- bash -x ./install-texlive.sh
- export PATH=$PWD/texlive/bin/x86_64-linux:$PATH
script:
- make html latexpdf
- cp build/latex/*.pdf build/html
- make html
- touch build/html/.nojekyll
deploy:
provider: pages
Expand Down
2 changes: 1 addition & 1 deletion source/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ for now. They are explained in subsequent chapters.
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "date-time" },
"birthday": { "type": "string", "format": "date" },
"address": {
"type": "object",
"properties": {
Expand Down
6 changes: 3 additions & 3 deletions source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
sys.path.insert(0, os.path.abspath(os.path.dirname('__file__')))

# The standard of JSON Schema to test the examples against
jsonschema_standard = 6
jsonschema_standard = 7

rst_prolog = """
.. role:: new
Expand Down Expand Up @@ -63,9 +63,9 @@
# built documents.
#
# The short X.Y version.
version = '6.0'
version = '7.0'
# The full version, including alpha/beta/rc tags.
release = '6.0'
release = '7.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
12 changes: 6 additions & 6 deletions source/conventions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ Draft-specific notes
--------------------

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

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

|draft6|
|draft7|

.. draft_specific::

Expand Down
6 changes: 5 additions & 1 deletion source/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ Acknowledgments
Michael Droettboom wishes to thank the following contributors:

- Alexander Kjeldaas
- Alexander Lang
- Anders D. Johnson
- Armand Abric
- Ben Hutton
- Brandon Wright
- btubbs
- Brent Tubbs
- Chris Carpenter
- Christopher Mark Gore
- David Branner
Expand All @@ -18,8 +19,11 @@ Michael Droettboom wishes to thank the following contributors:
- Fenhl
- forevermatt
- goldaxe
- Hervé
- Hongwei
- Jesse Claven
- Koen Rouwhorst
- Mike Kobit
- Oliver Kurmis
- Sam Blackman
- Vincent Jacques
6 changes: 3 additions & 3 deletions source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ validator---just yet.

.. note::

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

**Where to begin?**

Expand Down
161 changes: 161 additions & 0 deletions source/reference/conditionals.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
.. index::
single: conditionals
single: conditionals; if
single: conditionals; then
single: conditionals; else
single: if
single: then
single: else

.. _conditionals:

Applying subschemas conditionally
=================================

|draft7| ``if``, ``then`` and ``else`` keywords

The ``if``, ``then`` and ``else`` keywords allow the application of a subschema
based on the outcome of another schema, much like the ``if/then/else``
constructs you've probably seen in traditional programming languages.

If ``if`` is valid, ``then`` must also be valid (and ``else`` is ignored.) If
``if`` is invalid, ``else`` must also be valid (and ``then`` is ignored).

We can put this in the form of a truth table, showing the combinations of when
``if``, ``then``, and ``else`` are valid and the resulting validity of the
entire schema:

==== ==== ==== ============
if then else whole schema
==== ==== ==== ============
✗ ✗ ✗ ✗
✗ ✗ ✓ ✓
✗ ✓ ✗ ✗
✗ ✓ ✓ ✓
✓ ✗ ✗ ✗
✓ ✗ ✓ ✗
✓ ✓ ✗ ✓
✓ ✓ ✓ ✓
==== ==== ==== ============

For example, let's say you wanted to write a schema to handle addresses in the
United States and Canada. These countries have different postal code formats,
and we want to select which format to validate against based on the country. If
the address is in the United States, the ``postal_code`` field is a "zipcode":
five numeric digits followed by an optional four digit suffix. If the address is
in Canada, the ``postal_code`` field is a six digit alphanumeric string where
letters and numbers alternate.

.. schema_example::

{
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"enum": ["United States of America", "Canada"]
}
},
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
},
"else": {
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
}
}
--
{
"street_address": "1600 Pennsylvania Avenue NW",
"country": "United States of America",
"postal_code": "20500"
}
--
{
"street_address": "24 Sussex Drive",
"country": "Canada",
"postal_code": "K1M 1M4"
}
--X
{
"street_address": "24 Sussex Drive",
"country": "Canada",
"postal_code": "10000"
}

Unfortunately, this approach above doesn't scale to more than two countries. You
can, however, wrap pairs of ``if`` and ``then`` inside an ``allOf`` to create

Choose a reason for hiding this comment

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

I can never figure out whether allOf, anyOf, or oneOf makes sense here. For mutually exclusive conditions, the effect is the same for all of them. For overlapping conditions, it makes a difference.

I don't think you need to change this, I'm just kind of musing on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. In any case allOf/anyOf/oneOf are discussed in detail elsewhere -- this is just intended to show the combination of conditionals with the set notation operators. I think I'll not confuse this here now, but we can always revisit later.

something that would scale. In this example, we'll use United States and
Canadian postal codes, but also add Netherlands postal codes, which are 4 digits
followed by two letters. It's left as an exercise to the reader to expand this
to the remaining postal codes of the world.

.. schema_example::

{
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"enum": ["United States of America", "Canada", "Netherlands"]
}
},
"allOf": [
{
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
}
},
{
"if": {
"properties": { "country": { "const": "Canada" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
}
},
{
"if": {
"properties": { "country": { "const": "Netherlands" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{4} [A-Z]{2}" } }
}
}
]
}
--
{
"street_address": "1600 Pennsylvania Avenue NW",
"country": "United States of America",
"postal_code": "20500"
}
--
{
"street_address": "24 Sussex Drive",
"country": "Canada",
"postal_code": "K1M 1M4"
}
--
{
"street_address": "Adriaan Goekooplaan",
"country": "Netherlands",
"postal_code": "2517 JX"
}
--X
{
"street_address": "24 Sussex Drive",
"country": "Canada",
"postal_code": "10000"
}


32 changes: 20 additions & 12 deletions source/reference/generic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ This chapter lists some miscellaneous properties that are available
for all JSON types.

.. index::
single: metadata
single: annotation
single: title
single: description
single: default
single: examples
single: $comment

.. _metadata:
.. _annotation:

Metadata
--------
Annotations
-----------

JSON Schema includes a few keywords, ``title``, ``description``, ``default``, and
``examples`` that aren't strictly used for validation, but are used to describe
parts of a schema.
JSON Schema includes a few keywords, ``title``, ``description``, ``default``,
``examples``, and ``$comment`` that aren't strictly used for validation, but are
used to describe parts of a schema.

The ``title`` and ``description`` keywords must be strings. A "title"
will preferably be short, whereas a "description" will provide a more
lengthy explanation about the purpose of the data described by the
schema. Neither are required, but they are encouraged for good
practice, and can make your schema "self-documenting".
|draft7| ``$comment``

The ``title``, ``description``, and ``$comment`` keywords must be strings. A

Choose a reason for hiding this comment

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

I wouldn't put $comment with title and description.

$comment, like $defs, is a placeholder keyword. It reserves that keyword for a particular syntax, but doesn't do anything like an assertion or annotation does. $comment is specifically reserved for strings that you're not allowed to do anything with, and $defs is reserved as a place to put re-usable schemas.

title and description are annotations and actually attach their values to the instance during processing (assuming you are collecting annotations).

Copy link
Member Author

Choose a reason for hiding this comment

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

Is $defs a thing from a newer version of the standard? I still see definitions here and that's what I've used in this doc.

That makes sense about $comment -- I'll move it to its own section and be clearer about how explicitly limited its use is.

Choose a reason for hiding this comment

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

Oh, yeah, $defs replaces definitions in the next draft. Although you can keep using definitions, it just didn't fit the core keyword naming. Less seriously, I also can't type it accurately half the time- "definitoins" is what I write more often than not 😛

Anyway, ignore $defs for draft-07, pretend I wrote definitions

"title" will preferably be short, whereas a "description" will provide a more
lengthy explanation about the purpose of the data described by the schema.
``$comment`` is intended for notes to schema maintainers, while ``title`` and
``description`` are meant for display to end users. This is somewhat analogous
to the distinction between comments and docstrings in source code.

None of these "annotation" keywords are required, but they are encouraged for
good practice, and can make your schema "self-documenting".

The ``default`` keyword specifies a default value for an item. JSON
processing tools may use this information to provide a default value
Expand Down
4 changes: 3 additions & 1 deletion source/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ JSON Schema Reference

type.rst
string.rst
regular_expressions.rst
numeric.rst
object.rst
array.rst
boolean.rst
null.rst
generic.rst
non_json_data.rst
combining.rst
conditionals.rst
schema.rst
regular_expressions.rst
Loading