What is WoT: JSON Schema in practice – advanced topics
by the Web of Things Community Group
In the previous video, we talked about JSON Schema Basics. In this video, we will continue with generic validation keywords, Schema composition, and Schema conditionality. The corresponding web page of this video is at https://w3c.github.io/wot-cg/tutorials/whatiswot/docs/preliminary/json-schema/practice-advanced.
Video
Transcript
In the previous video, we talked about JSON Schema Basics, in this video, we will continue with generic keywords, Schema composition, and Schema Conditionality.
Some of the annotation keywords used in JSON Schema are title, description, and default.
None of these annotation words are required but enhance good practice for self-documenting Schemas.
The title and description keywords must be strings.
The enum keyword is used to restrict a value to a fixed set of values.
It must be an array with at least one element, where each element is unique.
The const keyword is used to restrict a value to a single value.
JSON Schema includes a few keywords for combining Schemas together, the most used ones are allOf, anyOf, oneOf, and not.
To validate allOf, the given data must be valid against all of the given sub schemas.
To validate anyOf, the given data must be valid against any (one or more) of the given sub schemas.
To validate oneOf, the given data must be valid against exactly one of the given sub schemas.
The not keyword declares that an instance validates if it doesn’t validate against the given sub schema.
The if, then and else keywords allow the application of a sub schema based on the outcome of another Schema, as it is in other programming languages. If it is valid, then must also be valid (and else is ignored.) If it is invalid, else must also be valid (and then ignored).
You can proceed to the next tutorial by clicking on the card or the video description.