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.

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.
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'`.
npm install express-yup-middleware
yarn add express-yup-middleware
pnpm add express-yup-middleware

Basic Express route with body validation using Yup schema and custom error messages.

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);