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

yup-express-middleware

v1.0.2

Published

Express middleware for yup validation

Readme

Yup express middleware

Offers Yup schema validation for Express with first class typescript support.

npm i yup-express-middleware

or

yarn add yup-express-middleware

Features

  1. req.body and/or req.query can be validated
  2. Yup transformations supported
  3. req.body and/or req.query will be of the type of your schema in your controller!
  4. No messy try-catch validation handling in your controllers, this library will automatically call express' error route handler (optionally with a custom error you can define).

Usage

import { validation } from "yup-express-middleware";
import * as yup from "yup";
import express from "express";

const app = express();

const schema = yup.object().shape({
    name: yup.string().required(),
    age: yup
        .number()
        .required()
        .transform(val => val + 2),
});

const schema2 = yup.object({ bla: yup.object({ bloo: yup.string() }) });

app.post("/", validation({ body: schema, query: schema2 }), (req, res, next) => {
    const { name, age } = req.body;
    //  string^   ^number => validated, transformed (if the schema did so) and typed correctly

    const {
        bla: { bloo }, // bloo is typed correctly as string
    } = req.query;
});

When validation fails

class HttpError extends Error {
    constructor(message: string, public status: number) {
        super(message);
    }
}

class BadRequestError extends HttpError {
    constructor(message: string) {
        super(message, 400);
    }
}

app.post(
    "/",
    validation(
        { body: schema },
        { yupOptions: { abortEarly: false }, ErrorClass: BadRequestError }
    ),
    (req, res, next) => {
        // If validation fails, this won't get called
        // the middleware will call `next(new BadRequestError("the message provided by yup"))`
        // and you will have to catch that in your express error handler.
    }
);

// for example

app.use(((err, req, res, next) => {
    if (res.headersSent) next(err);
    if (err.name === "BadRequestError") {
        res.status(err.status).json({
            reason: "Validation Error",
            error: err.message,
        });
    }
}) as ErrorRequestHandler);

API


validation(schema, options);

schema:
    body: (optional) yup schema
    query: (optional) yup schema

options:

    yupOptions: Options to pass to yup.validate()

    ErrorClass: The error class to throw when validation fails (defaults to `Error`)

    finallyFunction: the function to call regardless of whether validation succeeds or fails)