npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@express-route-validation/zod

v0.4.0

Published

Express middleware for request and response validation.

Downloads

23

Readme

@express-route-validation/zod

Express middleware for request and response validation.

  • 🔒 Request validation
  • ✨ Response validation
  • 💪 100% test coverage
  • 📦 Zero dependencies (except peer dependencies)
  • 🔥 TypeScript support

Ensure your Express routes are always receiving and returning the correct data with this express validation middleware. Seamlessly validate request and response objects, catching any instances where your route is returning unwanted data.

npm version coverage

Installation

npm install @express-route-validation/zod

Example

import { test } from "node:test";
import assert from "node:assert/strict";
import request from "supertest";
import {
  validateRequest,
  validateResponse,
} from "@express-route-validation/zod";
import { v4 as uuid } from "uuid";
import express from "express";
import z from "zod";

type User = {
  id: string;
  username: string;
  password: string;
};
const users: User[] = [];
const app = express();

app.use(express.json());

app.post(
  "/users",
  validateResponse({
    // filters out everything besides id and username
    201: z.object({
      id: z.string().uuid(),
      username: z.string(),
    }),
  }),
  validateRequest({
    // Automatically handles bad requests, returning relevant errors and status of 400
    body: z.object({
      username: z.string().transform((val) => val.replace(/\s+/g, "-")),
      password: z.string().min(6),
    }),
  }),
  (req, res): any => {
    const { username, password } = req.validated.body;

    const exists = users.find((user) => user.username);
    if (exists) return res.status(409).json({ message: "username taken" });

    const user = {
      id: uuid(),
      username,
      password,
    };
    users.push(user);
    res.status(201).json(user);
  },
);

// Test cases

test("valid response", async () => {
  const res = await request(app).post("/users").send({
    username: "my username",
    password: "123456",
  });

  assert.equal(res.statusCode, 201);
  assert.equal(res.body.username, "my-username");
  assert.notEqual(
    users.find((user) => user.id == res.body.id),
    undefined,
  );
});

test("invalid response", async () => {
  const res = await request(app).post("/users").send({
    username: "user",
    password: "1234",
  });

  assert.equal(res.statusCode, 400);
  assert.deepEqual(res.body, {
    errors: [
      {
        location: ["body", "password"],
        messages: ["string must contain at least 6 character(s)"],
      },
    ],
  });
});

Configure

Request

import { createRequestValidator } from "@express-route-validation/zod";
import z from "zod";

const validateRequest = createRequestValidator({
  badRequestHandler: (error: z.ZodIssue[], _req, res, _next) => {
    res.status(400).json({ error });
  },
  path: "validated",
});
  • badResponseHandler: called when a request fails validation
  • path: the location where validated data will be stored on the request object

Response

import { createResponseValidator } from "@express-route-validation/zod";

const validateResopnse = createResponseValidator({
  badResponseHandler: (_err, req, res) => {
    console.warn(
      `Bad ${res.statusCode} Response at (${req.method}) ${req.originalUrl}`,
    );
    res.sendStatus(500);
  },
  missingValidatorHandler: (_err, req, res) => {
    console.warn(
      `Missing ${res.statusCode} Response Validator at (${req.method}) ${req.originalUrl}`,
    );
    res.sendStatus(500);
  },
  requireValidator: true,
});
  • badResponseHandler: called when a response fails validation
  • missingValidatorHandler: called when a response has no validation schema
  • requireValidator: calls missingValidatorHandler when true