ProvenPin Install

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
pinned versions7
express@5.3.0zod@4.6.7tsx@4.23.15@types/node@24.19.2supertest@7.3.1@types/express@5.0.6@types/supertest@7.2.1
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("express-zod-validation") (install), or see them on GitHub.

package.json
{
  "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"
  }
}
src/app.ts
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;
}
test/app.test.ts
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.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.