Fastify 5 routes with JSON Schema validation: working versions (tested 2026-10-15)
Fastify 5 route validation and response serialization with JSON Schema, tested via app.inject().
passed2 passed · 0 failed2026-10-15 11:35 UTC- Status
- passed
- Last passed
- 2026-10-15 11:35 UTC
- Tests
- 2 / 2
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
fastify@5.12.7tsx@4.23.15@types/node@24.19.2
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38728226240
The code
Exactly the files that passed. Your agent can pull them with get_recipe("fastify-json-schema") (install), or see them on GitHub.
package.json
{
"name": "fastify-json-schema",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"fastify": "5.12.7"
},
"devDependencies": {
"tsx": "4.23.15",
"@types/node": "24.19.2"
}
}
src/app.ts
import Fastify from "fastify";
export function build() {
const app = Fastify();
app.post("/items", {
schema: {
body: { type: "object", required: ["name", "qty"], additionalProperties: false,
properties: { name: { type: "string", minLength: 1 }, qty: { type: "integer", minimum: 1 } } },
response: { 201: { type: "object", properties: { id: { type: "integer" }, name: { type: "string" }, qty: { type: "integer" } } } },
},
}, async (req, reply) => {
const body = req.body as { name: string; qty: number };
return reply.code(201).send({ id: 1, ...body, secret: "never serialized" });
});
return app;
}
test/app.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { build } from "../src/app.ts";
test("valid body, response schema strips extra fields", async () => {
const res = await build().inject({ method: "POST", url: "/items", payload: { name: "bolt", qty: 3 } });
assert.equal(res.statusCode, 201);
assert.deepEqual(res.json(), { id: 1, name: "bolt", qty: 3 });
});
test("invalid body is 400", async () => {
const res = await build().inject({ method: "POST", url: "/items", payload: { name: "", qty: 0 } });
assert.equal(res.statusCode, 400);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "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
Fastify 5 + @fastify/jwt protected routes
Sign and verify JWTs in Fastify 5 with @fastify/jwt and an authenticate preHandler that guards routes.
Fastify · @fastify/jwt
Fastify 5 + Zod 4 type provider for typed routes
Use fastify-type-provider-zod so Zod 4 schemas validate requests, serialize responses and type handlers in Fastify 5.
Fastify · Zod · fastify-type-provider-zod