Created
July 1, 2013 11:15
-
-
Save zakgrant/5900025 to your computer and use it in GitHub Desktop.
This is how to represent an object that can have multiple types within a JSON Schema
This file contains 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
{ | |
"$schema": "http://json-schema.org/draft-03/schema#", | |
"type": [ | |
{ | |
"type": "number", | |
"minimum": 0, | |
"exclusiveMinimum": true | |
}, | |
{ | |
"type": "string", | |
"pattern": "^.+@.+\\..+$" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using "oneOf" also gives more of a hint for automated processors, as well as human readers. For example, if you paste this schema:
into this page (selecting the "Editable" option), then the interface gives you a nice drop-down for the different schema options.
However, if you replace "oneOf" with "anyOf", then the interface doesn't know the schemas are exclusive, so it presents a multiple-select instead (hidden behind a button), which isn't quite as intuitive.