Validation with schemas (TypeBox)
Seventh chapter of the "Fastify with TypeScript" series. The previous chapter ended with two problems: generics describe the data but don't check it ( limit + 1 gave us 51 ), and where a schema does check it, the schema and the TypeScript interface are two copies kept in sync by hand. In this chapter we describe the data once , with TypeBox, and get both the runtime validation and the types from that single description.
Fastify uses JSON Schema to validate requests and to serialize responses. TypeBox lets you write that JSON Schema with a small, typed API, and derives the matching TypeScript type from it. We already used it in chapter 4 for the configuration.…