chapter nineteen

19 Improving input validation with advanced JSON Schema

 

This chapter covers

  • JSON schema keywords for validation
  • Enhancing PetSitter schemas with those

The journey of the PetSitter team started with a domain model. Building upon the domain model, they created a set of common, reusable schemas in their OpenAPI definition. Then, they referenced those schemas in the requests and responses of their API operation. As we joined them in this process, we learned in chapter 13 that we can autogenerate backend code that automatically does input validation based on the schemas. We also used Prism, an OpenAPI-powered mocking and testing tool, to detect whether the API responses follow the schemas and our API backend upholds the contract described with OpenAPI.

In the third part of the book, our focus is on improving the developer experience, with the goal of making the API easier and more delightful to use as soon as other developers apart from Max start consuming it. At the end of the sprint, which is our frame of understanding for this part of the book, an external contractor will work with the API to develop a mobile application for PetSitter. And the ultimate goal that we’re working towards is releasing the API into the wild.

19.1 The problem

19.2 Supported validations

19.2.1 Read-only and write-only properties

19.2.2 Enforcing number constraints

19.2.3 Enforcing string formats

19.2.4 Enforcing array constraints

19.2.5 Defining enumerations

19.2.6 Listing required and optional properties

19.2.7 Setting defaults

19.3 Updating PetSitter schemas

19.3.1 User

19.3.2 Job

19.3.3 Job Application

19.3.4 Pet, Dog, and Cat

19.4 Summary