Menu

Validation with schemas (TypeBox) — Fastify with TypeScript (part 7/18)
📰

Validation with schemas (TypeBox) — Fastify with TypeScript (part 7/18)

DEV Community: fastify·Antonio Tripodi·11 days ago
#sOte69rl
#devtofastify#devto#dev#type#schema
Long article (2253 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.