express-yup-middleware
raw JSON → 2.0.0 verified Sat Apr 25 auth: no javascript
Express middleware that validates request body, query, and params using Yup schemas. Version 2.0.0 is the latest stable release. It integrates Yup validation into Express routes and returns structured error responses. Differentiators include custom error messages per field, validation options per schema part (e.g., abortEarly), and cross-validation via context. Requires Yup as a peer dependency.
Common errors
error Cannot find module 'express-yup-middleware' ↓
cause Package not installed or wrong import path.
fix
Run
npm install express-yup-middleware and use correct import: import { expressYupMiddleware } from 'express-yup-middleware'. error TypeError: schemaValidator.schema is undefined ↓
cause Missing `schema` property in the validator object.
fix
Provide
schema with at least one key (body, query, params) as shown in docs. error ValidationError: testBodyProperty is required ↓
cause Yup schema property is required and missing.
fix
Ensure request body includes the required field defined in schema.
Warnings
gotcha The `schemaValidator` object must have `schema` at the top level with `body`, `query`, or `params` properties; missing them will skip validation without error. ↓
fix Ensure `schemaValidator` includes at least one of `body`, `query`, `params` inside `schema`.
gotcha Error messages object keys must exactly match the custom message strings used in Yup's `.required()` etc. (e.g., 'name-required') – not the property path. ↓
fix Use the same string passed to Yup's validation message as the key in `errorMessages`.
gotcha Cross-validation via `this.options.context` requires the request object to be passed; the middleware provides `req` in context automatically. ↓
fix Access `this.options.context` in Yup `.test()` to get the request payload.
deprecated Yup v0.x vs v1.x: In Yup v1, `.shape()` may require different import path. express-yup-middleware 2.x supports Yup v1. ↓
fix Use Yup v1.x and import as `import * as Yup from 'yup'`.
Install
npm install express-yup-middleware yarn add express-yup-middleware pnpm add express-yup-middleware Imports
- expressYupMiddleware wrong
import expressYupMiddleware from 'express-yup-middleware'correctimport { expressYupMiddleware } from 'express-yup-middleware' - ExpressYupMiddlewareInterface
import { ExpressYupMiddlewareInterface } from 'express-yup-middleware' - expressYupMiddleware (CommonJS) wrong
const expressYupMiddleware = require('express-yup-middleware')correctconst { expressYupMiddleware } = require('express-yup-middleware')
Quickstart
import express from 'express';
import { expressYupMiddleware } from 'express-yup-middleware';
import * as Yup from 'yup';
const app = express();
app.use(express.json());
const schemaValidator = {
schema: {
body: {
yupSchema: Yup.object().shape({
name: Yup.string().required('name-required'),
}),
},
},
errorMessages: {
'name-required': {
key: 'name-required',
message: 'Name is required.',
},
},
};
app.post('/test', expressYupMiddleware({ schemaValidator }), (req, res) => {
res.json({ message: 'Success' });
});
app.listen(3000);