19 Improving input validation with advanced JSON Schema

 

This chapter covers

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

The PetSitter team’s journey started with a domain model. Building upon that 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 operations. We learned in chapter 13 that we can autogenerate backend code that automatically performs 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 whether our API backend upholds the contract described with OpenAPI.

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 schema

19.3.2 Job schema

19.3.3 JobApplication schema

19.3.4 Pet, Dog, and Cat schemas

Summary