
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
express-request-schema-validator
Advanced tools
A flexible Express.js middleware for validating request bodies, query parameters, and headers using JSON Schema
A flexible Express.js middleware for validating request bodies, query parameters, and headers using JSON Schema (powered by AJV).
npm install express-request-schema-validator
const express = require("express");
const { createMiddleware } = require("express-request-schema-validator");
const app = express();
app.use(express.json());
// Define your JSON schemas
const userSchema = {
type: "object",
properties: {
email: { type: "string", format: "email" },
age: { type: "number", minimum: 0 },
name: { type: "string", minLength: 1 },
},
required: ["email", "name"],
};
// Apply validation middleware
app.post("/users", createMiddleware({ bodySchema: userSchema }), (req, res) => {
res.json({ message: "User created", user: req.body });
});
app.listen(3000);
import express from "express";
import { createMiddleware } from "express-request-schema-validator";
const app = express();
app.use(express.json());
// Define your JSON schemas
const userSchema = {
type: "object",
properties: {
email: { type: "string", format: "email" },
age: { type: "number", minimum: 0 },
name: { type: "string", minLength: 1 },
},
required: ["email", "name"],
};
// Apply validation middleware
app.post("/users", createMiddleware({ bodySchema: userSchema }), (req, res) => {
res.json({ message: "User created", user: req.body });
});
app.listen(3000);
import express, { Request, Response } from "express";
import { createMiddleware } from "express-request-schema-validator";
const app = express();
app.use(express.json());
const userSchema = {
type: "object",
properties: {
email: { type: "string", format: "email" },
age: { type: "number", minimum: 0 },
name: { type: "string", minLength: 1 },
},
required: ["email", "name"],
} as const;
app.post(
"/users",
createMiddleware({ bodySchema: userSchema }),
(req: Request, res: Response) => {
res.json({ message: "User created", user: req.body });
}
);
app.listen(3000);
createMiddleware(options)Creates an Express middleware that validates incoming requests.
bodySchema (optional): JSON Schema to validate request bodyquerySchema (optional): JSON Schema to validate query parametersheadersSchema (optional): JSON Schema to validate request headersconst middleware = createMiddleware({
bodySchema: {
type: "object",
properties: {
username: { type: "string" },
},
required: ["username"],
},
querySchema: {
type: "object",
properties: {
page: { type: "string", pattern: "^[0-9]+$" },
},
},
headersSchema: {
type: "object",
properties: {
"x-api-key": { type: "string", minLength: 20 },
},
required: ["x-api-key"],
},
});
app.post("/api/users", middleware, (req, res) => {
// Request is valid if we reach here
res.json({ success: true });
});
When validation fails, the middleware responds with a 400 status and a JSON error object:
{
"error": "Request body validation failed",
"schema": {
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" }
},
"required": ["email"]
},
"details": [
{
"field": "/email",
"message": "must match format \"email\"",
"keyword": "format",
"params": { "format": "email" }
}
]
}
error: Human-readable error messageschema: The JSON Schema that failed validationdetails: Array of specific validation errors
field: The property path that failedmessage: Error descriptionkeyword: The validation rule that failedparams: Additional validation parametersThanks to ajv-formats, the following string formats are supported:
date-timedatetimeemailhostnameipv4ipv6uriurluuidregexISC
FAQs
A flexible Express.js middleware for validating request bodies, query parameters, and headers using JSON Schema
We found that express-request-schema-validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.