This repository was archived by the owner on Nov 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 316
Main updates for Draft 7 #84
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1fa7006
Basic upgrading of the defaults to draft 7
mdboom 5fc2a94
Document new $comment keyword
mdboom 71e9fcd
Better errors from invalidation
mdboom e169b1d
Describe conditionals
mdboom 1d52aa7
Improve format index
mdboom d9e95f2
Rename metadata -> annotation
mdboom bb7eaae
Add section on Media / non-JSON-data
mdboom 715fb5a
Updates for new format types
mdboom 29ec90f
Small clarification
mdboom fdaeb17
Update version of jsonschema
mdboom 4c68a86
fixed "birthday" in from `date` to `date-time`
hongwei1 10a54b8
Update credits
mdboom 92074e2
Fix typos
mdboom 40ae42b
Disable LaTeX output for now
mdboom d386e5c
Reinstate LaTeX
mdboom e14a693
Describe $comment separately from description and title
mdboom 8606772
Clarify text vs. binary document types
mdboom 043dc0b
Be more specific about time format used.
mdboom f7c1090
Use `url-reference` rather than `url` for JSON schema fragment
mdboom 34693e2
Credit Henry Andrews
mdboom cb69070
Reinstate installation of PDF file
mdboom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
.. 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 | ||
==== ==== ==== ============ | ||
X X | ||
X | ||
X X X | ||
X | ||
X X | ||
X X X | ||
X X X X | ||
==== ==== ==== ============ | ||
|
||
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 | ||
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" | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
, oroneOf
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.
There was a problem hiding this comment.
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.