The JSON Schema and garden-schema

We've written a bunch of code over the years to handle data validation. Off the top of my head here are a few.

  1. The Gdn_Validation class is used for validating the input into models.
  2. The garden-cli library validates command line options.
  3. Infrastructure has its own API layer that can validate request data.
  4. We also write ad-hoc validation for much data. For example, validating addon info arrays has quite a bit of code in it.

When I went to write the core code to drive the API v2, I knew we'd need to have great, easy to use validation. This was the impetus for writing garden-schema, but there were some other considerations that made the package take shape the way it did:

  • Validation for API endpoints is especially important because it can help the security of the code. A layer that ensures the shape of the data coming into the API cuts off a whole host of threat vectors.
  • I wanted to write a validation package that wasn't tied to a specific use-case so that we could potentially use it elsewhere.
  • The OpenAPI specification uses JSON schema so implementing it in some way is necessary to support OpenAPI. Implementing an existing well thought out specification is also an order of magnitude easier than architecting a brand new specification.
  • I noticed how productive Tim was implementing API endpoints for infrastructure so I also added a short syntax inspired by his API layer.

Getting to know garden-schema

You can learn how to use garden-schema by reading its README. I really want devs to know how to use this package. It's not overly complex, and provides great functionality. R&D developers should also start understanding it enough to make PRs against it.

Getting to know JSON Schema

The Schema class represents its data internally as an array that is a subset of the JSON Schema spec. Devs should read through the JSON Schema validation spec at least to see what the schema is ultimately supposed to do and also get a sense of what features we can add to garden-schema if we really need them.

Comments

  • In the future, we should use garden-schema to generate front-end validation.