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-json-validator-middleware

v4.0.0

Published

An Express middleware to validate requests against JSON Schemas

Downloads

109,842

Readme

Express JSON Validator Middleware

Express middleware for validating requests against JSON schemas with Ajv.

npm version npm monthly downloads npm license CI

Why validate with JSON schemas?

  • Expressive: JSON Schema is a portable way to describe data structures.
  • Separate validation: Route handlers can focus on business logic.
  • Rich errors: Ajv provides detailed error objects you can return or transform.
  • Flexible: You can validate body, params, query, or custom request properties.

Requirements

  • Node.js 24 or newer. Node 24 is the latest LTS line as of March 28, 2026.
  • Express 4.21.2+ or 5.2.1+.

Installation

Install with npm:

npm install express express-json-validator-middleware

Install with Bun:

bun add express express-json-validator-middleware

Upgrading from v3? Read the v3 to v4 upgrade guide.

Getting started

import express from "express";
import { Validator } from "express-json-validator-middleware";

const app = express();

app.use(express.json());

const addressSchema = {
	type: "object",
	required: ["street"],
	properties: {
		street: {
			type: "string"
		}
	}
};

const { validate } = new Validator({ allErrors: true });

app.post("/address", validate({ body: addressSchema }), (request, response) => {
	response.json({ street: request.body.street });
});

Coming from express-jsonschema? Read the migration notes.

Schemas in TypeScript

When authoring schemas in TypeScript, combine this package's AllowedSchema type with Ajv's JSONSchemaType helper:

import type { JSONSchemaType } from "ajv";
import { type AllowedSchema } from "express-json-validator-middleware";

type Address = {
	street: string;
};

const addressSchema: AllowedSchema & JSONSchemaType<Address> = {
	type: "object",
	required: ["street"],
	properties: {
		street: {
			type: "string"
		}
	}
};

Error handling

Validation failures are forwarded to Express with a ValidationError:

import express from "express";
import { ValidationError } from "express-json-validator-middleware";

const app = express();

app.use((error, request, response, next) => {
	if (error instanceof ValidationError) {
		response.status(400).json({
			name: error.name,
			validationErrors: error.validationErrors
		});
		return;
	}

	next(error);
});

Example error payload:

{
	name: "JsonSchemaValidationError",
	validationErrors: {
		body: [
			{
				instancePath: "/name",
				keyword: "type"
			}
		]
	}
}

Validating multiple request properties

const tokenSchema = {
	type: "object",
	required: ["token"],
	properties: {
		token: {
			type: "string",
			minLength: 36,
			maxLength: 36
		}
	}
};

const paramsSchema = {
	type: "object",
	required: ["uuid"],
	properties: {
		uuid: {
			type: "string",
			minLength: 36,
			maxLength: 36
		}
	}
};

app.post(
	"/address/:uuid",
	validate({
		body: addressSchema,
		params: paramsSchema,
		query: tokenSchema
	}),
	(request, response) => {
		response.send({});
	}
);

Using dynamic schemas

Instead of passing a schema object, you can pass a function that derives a schema from the current request:

function getSchema(request) {
	if (request.query.requireAge === "1") {
		return {
			type: "object",
			required: ["name", "age"],
			properties: {
				name: { type: "string" },
				age: { type: "number" }
			}
		};
	}

	return {
		type: "object",
		required: ["name"],
		properties: {
			name: { type: "string" }
		}
	};
}

app.post("/user", validate({ body: getSchema }), (request, response) => {
	response.json({ success: true });
});

Ajv instance

The underlying Ajv instance is available as validator.ajv and should be configured before you create middleware with validate():

import { Validator } from "express-json-validator-middleware";

const validator = new Validator({ allErrors: true });

validator.ajv;

If you use schema formats, remember to install and register ajv-formats.

Development

This repository now uses Bun for dependency management:

bun install

Run the full verification suite with either package manager:

bun run verify
npm run verify

npm run verify and bun run verify cover:

  • Node runtime tests
  • type-checking against the published API
  • coverage generation
  • packaging the library with npm pack
  • installing the packed tarball into /tmp/sample-express-app/npm
  • installing the packed tarball into /tmp/sample-express-app/bun

Tests

npm test
npm run test:types
npm run test:install:npm
npm run test:install:bun

More documentation on JSON Schema

Credits