An Introduction to JSON Schema: A Practical Look
HTML-код
- Опубликовано: 13 июл 2024
- This introduction to JSON Schema features Simon Plenderleith walking viewers through some examples of how to use JSON Schema for practical JSON validation. As an independent Node.js consultant, Simon frequently uses JSON Schema and shares with us some examples to get started with JSON Schema.
We start by introducing a basic JSON Schema for validating a request payload, using cooking recipes as an example. In the first example we introduce the JSON Schema concepts of $schema, keywords, types, and required properties.
The next step is to improve the schema for recipes by turning the ingredients into a JSON array. For this, we extend the basic JSON Schema to validate the updated property which now is an array. This demonstrates how to add a sub schema to validate the items in an array.
The final step is to introduce references withing the schema, turning the recipe model into one that recursively allows ingredients to be complete recipes. This demonstrates how in JSON Schema complete schema or specific parts of schemas can be reused.
All the materials shown in this video are available online, and here also are additional links to some of the things mentioned in the video:
- Code examples in this video: github.com/simonplend/talks/t...
- JSON Schema: json-schema.org/
- Ajv JSON schema validator: ajv.js.org/
- Fastify Node.js web framework: www.fastify.io/
- Understanding JSON Schema: json-schema.org/understanding...
- Tutorial: Get started with validation in Node.js: simonplend.com/get-started-wi...
- Simon's website: simonplend.com/
- Simon on Twitter: / simonplend
00:00 Introduction
00:56 JSON Schema usage as a developer
01:42 How complicated is it to get started with JSON Schema?
03:58 JSON Schema implementations in Node.js
05:40 Getting started with the examples
06:12 A simple JSON Schema for validating a recipe
14:12 Which types does JSON Schema have?
15:41 JSON Schema validation in action
20:15 Improving the JSON Schema by using an array
26:48 Using recursion in JSON Schema
33:09 Wrapping it up Наука
Thank you so much for the upload, great production quality and it is great that you put all links within the video description. Thank you both!
Glad you found it helpful! 😊
Thanks for doing a video about JSON Schema! Just one word of caution: In recent versions of AJV, it's necessary to set "strict mode" to false to get specification-compliant behavior. Much of what "strict mode" does can be useful for linting, but it will produce counter-intuitive behavior or just not work for plenty of correct and useful schemas. This can be set either in code or using the command line interface.
I think an important think that you touched on at the end is that a schema is not the validation tool, it's just the 'config' for a validation tool, this gives you great flexibility with what validation tools you want to use. The other good thing about it being just a config is that it can be used for building dynamic UI's as well. I do this with XSD (XML), JSON schema seems much easier to work with, thanks for the talk 👍
Yes, without a validator a JSON Schema doesn't make a lot of sense. It's the machinery that's needed to validate a document against a schema, and there are various implementations out there.
Hello @Erik, thank you for interrupting at 15:40 as I learned something new, subscribed
Thanks for the feedback!
Very nice content!
Awesome and clear content.
Thanks and all credits go to Simon, of course!
Are there tools to generate api documentation out of JSON schemas?
A JSON Schema isn't an API. It describes the payload that is accepted or produced by an API. So just having a JSON Schema is not enough to have an API, you still have to decide about the "API mechanics" such as resources, HTTP methods, status codes, and which header fields will be used.