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

@esmj/schema-express-middleware

v0.4.0

Published

Middleware for express that uses @esmj/schema to make requests type-safe.

Readme

@esmj/schema-express-middleware

Express middleware for validating request body, query, params, and headers using @esmj/schema. Inspired by popular validation middlewares like zod-express-middleware and express-joi-validation, but using @esmj/schema for fast, type-safe validation.

Installation

npm install @esmj/schema-express-middleware

Usage

Basic Example

import express from 'express';
import { s, validateBody } from '@esmj/schema-express-middleware';

const app = express();
app.use(express.json());

const userSchema = s.object({
  name: s.string({ message: 'Name is required' }).refine((value) => value.length > 0, { message: 'Name must not be empty' }),
  age: s.string({ message: 'Age is required' })
    .transform((value) => Number.parseInt(value))
    .pipe(s.number())
    .refine((value) => Number.isInteger(value) && value >= 0, { message: 'Age must be a non-negative integer' }),
});

app.post('/users', validateBody(userSchema), (req, res) => {
  // req.schema.body is now validated and typed
  res.json({ user: req.schema.body });
});

app.listen(3000);

API

validate(schemas, errorHandler?)

Arguments:

  • schema: { body?, query?, params?, headers? } — An object where each property is an optional @esmj/schema SchemaInterface. Each provided schema will be used to validate the corresponding part of the request (req.body, req.query, req.params, req.headers).
  • errorHandler (optional): (opts) => unknown — A function called on validation error. Receives an object with { req, res, next, part, error, result }.
    • part: One of 'body' | 'query' | 'params' | 'headers' indicating which part failed.
    • error: The validation error (with message and optional cause).
    • result: The failed parse result (with success: false).

Returns: Express RequestHandler middleware.

import { validate, s } from '@esmj/schema-express-middleware';

app.post(
  '/login',
  validate({
    body: s.object({ username: s.string(), password: s.string() }),
    query: s.object({ next: s.string().optional() }),
  }),
  (req, res) => {
    // req.schema.body and req.schema.query are validated and typed
    res.send('ok');
  }
);

Custom Error Handler

You can provide a custom error handler to control the response:

import { validate } from '@esmj/schema-express-middleware';

function myErrorHandler({ res, error, part }) {
  res.status(422).json({ part, message: error.message });
}

app.post('/users', validate({ body: userSchema }, myErrorHandler), handler);

validateBody(schema, errorHandler?)

Arguments:

  • schema: [@esmj/schema SchemaInterface] — The schema to validate req.body.
  • errorHandler (optional): Same as above.

Returns: Express RequestHandler middleware.

app.post('/users', validateBody(userSchema), handler);

validateQuery(schema, errorHandler?)

Arguments:

  • schema: [@esmj/schema SchemaInterface] — The schema to validate req.query.
  • errorHandler (optional): Same as above.

Returns: Express RequestHandler middleware.

app.get('/search', validateQuery(s.object({ q: s.string() })), handler);

validateParams(schema, errorHandler?)

Arguments:

  • schema: [@esmj/schema SchemaInterface] — The schema to validate req.params.
  • errorHandler (optional): Same as above.

Returns: Express RequestHandler middleware.

app.get('/users/:id', validateParams(s.object({ id: s.string() })), handler);

validateHeaders(schema, errorHandler?)

Arguments:

  • schema: [@esmj/schema SchemaInterface] — The schema to validate req.headers.
  • errorHandler (optional): Same as above.

Returns: Express RequestHandler middleware.

app.get('/secure', validateHeaders(s.object({ authorization: s.string() })), handler);

Types

  • All helpers are fully typed. After validation, the validated and transformed data is available on the req.schema property:
    • req.schema.body — validated request body (if body schema provided)
    • req.schema.query — validated query parameters (if query schema provided)
    • req.schema.params — validated route parameters (if params schema provided)
    • req.schema.headers — validated headers (if headers schema provided)
  • All schemas must be @esmj/schema.

License

MIT