Fastify uses JSON Schema at the center of its design — schemas validate requests, optimize response serialization via fast-json-stringify, and drive OpenAPI documentation generation. TypeBox and @fastify/type-provider-typebox extend this to compile-time TypeScript type safety.
0 / 5 completed
1 / 5
A developer adds a JSON Schema to a Fastify route via schema: { body: { ... } }. What additional benefit does this provide beyond validation?
Fastify uses route schemas for both validation and serialization. For response schemas, Fastify uses fast-json-stringify to serialize responses 2-10x faster than JSON.stringify() by using the schema to generate optimized serialization code.
2 / 5
What is the purpose of Fastify's $ref support in route schemas?
Fastify supports JSON Schema $ref to reference schemas registered with fastify.addSchema(id, schema). This allows defining shared schemas (like error responses or pagination objects) once and referencing them across multiple route schemas with { $ref: 'schemaId#' }.
3 / 5
A developer uses @fastify/swagger with Fastify's route schemas. What does this plugin automatically generate?
@fastify/swagger reads Fastify route schemas and generates OpenAPI 2.0 or 3.0 documentation automatically. You can expose the spec at /documentation/json and add @fastify/swagger-ui for an interactive Swagger UI, all driven by the same schemas used for validation.
4 / 5
Which Fastify plugin enables adding TypeScript types to request and reply objects using JSON Schema?
@fastify/type-provider-typebox integrates TypeBox (a JSON Schema builder with TypeScript types) with Fastify's type provider system. Routes using TypeBox schemas get full TypeScript inference for request.body, request.params, and reply.send() without separate type assertions.
5 / 5
A Fastify route has a response schema defined for status 200 but the handler calls reply.send({ unexpectedField: true }). What happens?
fast-json-stringify used by Fastify for response serialization only includes fields defined in the response schema. Extra fields like unexpectedField are silently omitted from the response. This is a security feature preventing accidental data leakage, but it can cause confusion during development.