Express Validation
raw JSON → 4.1.1 verified Sat Apr 25 auth: no javascript
express-validation v4.1.1 is an Express middleware that validates request parameters (body, query, headers, params, cookies, signedCookies) using Joi schemas. It returns structured error responses when validation fails and integrates with Express error handlers. The library has a hard dependency on Joi v17.x.x and ships with TypeScript definitions. It is actively maintained with regular releases, and provides multiple error format options including keyByField. Compared to alternatives like express-validator, it uses Joi directly rather than its own validator.
Common errors
error Cannot find module 'express-validation' ↓
cause Package not installed or import path incorrect.
fix
Run 'npm install express-validation' and verify import statement.
error TypeError: Joi.object is not a function ↓
cause Joi imported from wrong source or version mismatch.
fix
Use import { Joi } from 'express-validation' instead of import Joi from 'joi'.
error ValidationError is not defined ↓
cause Forgot to import ValidationError or used wrong import pattern.
fix
Add import { ValidationError } from 'express-validation' to error handler.
Warnings
breaking In v4, the response structure changed from { status, message, errors } to include 'details' object keyed by parameter type. ↓
fix Update error handling code to access err.details instead of err.errors.
deprecated The old pattern of using Joi directly from 'joi' package is deprecated; express-validation re-exports Joi to ensure version compatibility. ↓
fix Use import { Joi } from 'express-validation' instead of import Joi from 'joi'.
gotcha The validate() function's second and third parameters (options and joiOptions) are optional but must be passed as objects; passing nothing causes runtime errors. ↓
fix Always pass at least empty objects: validate(schema, {}, {})
breaking In v3, errors were returned as an array; v4 changed to an object keyed by parameter. ↓
fix If you need array format, use the keyByField option or process the details object.
Install
npm install express-validation yarn add express-validation pnpm add express-validation Imports
- validate wrong
const validate = require('express-validation').validatecorrectimport { validate } from 'express-validation' - ValidationError wrong
const { ValidationError } = require('express-validation')correctimport { ValidationError } from 'express-validation' - Joi wrong
import Joi from 'joi'correctimport { Joi } from 'express-validation'
Quickstart
import express from 'express';
import { validate, ValidationError, Joi } from 'express-validation';
const loginValidation = {
body: Joi.object({
email: Joi.string().email().required(),
password: Joi.string().regex(/[a-zA-Z0-9]{3,30}/).required(),
}),
};
const app = express();
app.use(express.json());
app.post('/login', validate(loginValidation, {}, {}), (req, res) => {
res.json(200);
});
app.use((err, req, res, next) => {
if (err instanceof ValidationError) {
return res.status(err.statusCode).json(err);
}
return res.status(500).json(err);
});
app.listen(3000);