Fastify 5 + Zod 4 type provider for typed routes: working versions (tested 2026-10-15)
Use fastify-type-provider-zod so Zod 4 schemas validate requests, serialize responses and type handlers in Fastify 5.
passed3 passed · 0 failed2026-10-15 11:54 UTC- Status
- passed
- Last passed
- 2026-10-15 11:54 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
npm install --no-audit --no-fund --ignore-scripts=false npm test
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38730348195
The code
Exactly the files that passed. Your agent can pull them with get_recipe("fastify-zod-type-provider") (install), or see them on GitHub.
{
"name": "fastify-zod-type-provider",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"fastify": "5.12.7",
"zod": "4.6.7",
"fastify-type-provider-zod": "7.0.0"
},
"devDependencies": {
"tsx": "4.23.15",
"@types/node": "24.19.2"
}
}
import Fastify from "fastify";
import { serializerCompiler, validatorCompiler, type ZodTypeProvider } from "fastify-type-provider-zod";
import { z } from "zod";
export function buildApp() {
const app = Fastify();
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);
const typed = app.withTypeProvider<ZodTypeProvider>();
typed.post("/users", {
schema: {
body: z.object({ email: z.email(), age: z.number().int().min(13) }),
response: { 201: z.object({ id: z.number(), email: z.string() }) }, // extra fields are stripped
},
}, async (req, reply) => {
const { email } = req.body; // typed from the schema
return reply.code(201).send({ id: 1, email, passwordHash: "never-leaks" } as any);
});
typed.get("/search", { schema: { querystring: z.object({ q: z.string().min(1), page: z.coerce.number().int().default(1) }) } },
async (req) => ({ q: req.query.q, page: req.query.page }));
return app;
}
import { test } from "node:test";
import assert from "node:assert/strict";
import { buildApp } from "../src/app.ts";
test("valid body and response schema strips extra fields", async () => {
const app = buildApp();
const r = await app.inject({ method: "POST", url: "/users", payload: { email: "a@example.com", age: 30 } });
assert.equal(r.statusCode, 201);
assert.deepEqual(r.json(), { id: 1, email: "a@example.com" });
await app.close();
});
test("invalid body is 400", async () => {
const app = buildApp();
const r = await app.inject({ method: "POST", url: "/users", payload: { email: "nope", age: 5 } });
assert.equal(r.statusCode, 400);
await app.close();
});
test("querystring coercion and defaults", async () => {
const app = buildApp();
assert.deepEqual((await app.inject({ url: "/search?q=pin" })).json(), { q: "pin", page: 1 });
assert.deepEqual((await app.inject({ url: "/search?q=pin&page=3" })).json(), { q: "pin", page: 3 });
assert.equal((await app.inject({ url: "/search" })).statusCode, 400);
await app.close();
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.
Related stacks
Anthropic TypeScript SDK tool use loop, tested with a mocked fetch
Run a Claude tool_use loop with the official Anthropic SDK and Zod-validated tool inputs, tested offline with an injected fetch.
CSV import with PapaParse + Zod row validation
Parse CSV uploads with PapaParse (headers, BOM, quoted fields), validate each row with Zod and report row-numbered errors instead of failing the whole file.
Drizzle ORM + drizzle-zod validated inserts on SQLite
Generate Zod insert and select schemas from Drizzle tables with drizzle-zod, refine them, and validate input before inserting into SQLite.
Express 5 + Zod 4 request validation
Validate JSON bodies in Express 5 with a reusable Zod middleware that returns 400 with field errors.
All Fastify recipesAll Zod recipesAll fastify-type-provider-zod recipes