Express 5 + Zod 4 request validation: working versions (tested 2026-10-15)
Validate JSON bodies in Express 5 with a reusable Zod middleware that returns 400 with field errors.
passed3 passed · 0 failed2026-10-15 11:35 UTC- Status
- passed
- Last passed
- 2026-10-15 11:35 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 38728226240
The code
Exactly the files that passed. Your agent can pull them with get_recipe("express-zod-validation") (install), or see them on GitHub.
{
"name": "express-zod-validation",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"express": "5.3.0",
"zod": "4.6.7"
},
"devDependencies": {
"tsx": "4.23.15",
"@types/node": "24.19.2",
"supertest": "7.3.1",
"@types/express": "5.0.6",
"@types/supertest": "7.2.1"
}
}
import express, { type Request, type Response, type NextFunction } from "express";
import { z, type ZodType } from "zod";
export function validateBody<T>(schema: ZodType<T>) {
return (req: Request, res: Response, next: NextFunction) => {
const result = schema.safeParse(req.body);
if (!result.success) {
return res.status(400).json({ error: "invalid_body", issues: z.flattenError(result.error).fieldErrors });
}
req.body = result.data;
next();
};
}
export const CreateUser = z.object({
email: z.email(),
name: z.string().min(1).max(80),
age: z.number().int().min(13).optional(),
});
export function createApp() {
const app = express();
app.use(express.json());
app.post("/users", validateBody(CreateUser), (req, res) => {
res.status(201).json({ id: 1, ...req.body });
});
return app;
}
import { test } from "node:test";
import assert from "node:assert/strict";
import request from "supertest";
import { createApp } from "../src/app.ts";
test("accepts a valid body", async () => {
const res = await request(createApp()).post("/users").send({ email: "a@example.com", name: "Ada" });
assert.equal(res.status, 201);
assert.equal(res.body.email, "a@example.com");
});
test("rejects an invalid body with field errors", async () => {
const res = await request(createApp()).post("/users").send({ email: "nope", name: "" });
assert.equal(res.status, 400);
assert.ok(res.body.issues.email);
assert.ok(res.body.issues.name);
});
test("strips unknown keys", async () => {
const res = await request(createApp()).post("/users").send({ email: "b@example.com", name: "B", admin: true });
assert.equal(res.status, 201);
assert.equal(res.body.admin, undefined);
});
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 + express-session login flow, e2e-style over real HTTP with a cookie jar
Session login/logout with express-session (regenerate on login, httpOnly cookies), tested end to end against a real listening server using fetch-cookie, no browser or Playwright.