{"id":18363,"library":"express-validation","title":"Express Validation","description":"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.","status":"active","version":"4.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/andrewkeig/express-validation","tags":["javascript","express","validation","validate","joi","middleware","typescript"],"install":[{"cmd":"npm install express-validation","lang":"bash","label":"npm"},{"cmd":"yarn add express-validation","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-validation","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for defining validation schemas","package":"joi","optional":false}],"imports":[{"note":"ESM import; CommonJS destructure works too.","wrong":"const validate = require('express-validation').validate","symbol":"validate","correct":"import { validate } from 'express-validation'"},{"note":"Named export for error class.","wrong":"const { ValidationError } = require('express-validation')","symbol":"ValidationError","correct":"import { ValidationError } from 'express-validation'"},{"note":"Re-exported Joi from express-validation for convenience.","wrong":"import Joi from 'joi'","symbol":"Joi","correct":"import { Joi } from 'express-validation'"}],"quickstart":{"code":"import express from 'express';\nimport { validate, ValidationError, Joi } from 'express-validation';\n\nconst loginValidation = {\n  body: Joi.object({\n    email: Joi.string().email().required(),\n    password: Joi.string().regex(/[a-zA-Z0-9]{3,30}/).required(),\n  }),\n};\n\nconst app = express();\napp.use(express.json());\n\napp.post('/login', validate(loginValidation, {}, {}), (req, res) => {\n  res.json(200);\n});\n\napp.use((err, req, res, next) => {\n  if (err instanceof ValidationError) {\n    return res.status(err.statusCode).json(err);\n  }\n  return res.status(500).json(err);\n});\n\napp.listen(3000);","lang":"typescript","description":"Sets up an Express app with POST /login route that validates email and password using Joi schemas via express-validation middleware and handles errors with a custom error handler."},"warnings":[{"fix":"Update error handling code to access err.details instead of err.errors.","message":"In v4, the response structure changed from { status, message, errors } to include 'details' object keyed by parameter type.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use import { Joi } from 'express-validation' instead of import Joi from 'joi'.","message":"The old pattern of using Joi directly from 'joi' package is deprecated; express-validation re-exports Joi to ensure version compatibility.","severity":"deprecated","affected_versions":"*"},{"fix":"Always pass at least empty objects: validate(schema, {}, {})","message":"The validate() function's second and third parameters (options and joiOptions) are optional but must be passed as objects; passing nothing causes runtime errors.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"If you need array format, use the keyByField option or process the details object.","message":"In v3, errors were returned as an array; v4 changed to an object keyed by parameter.","severity":"breaking","affected_versions":">=4.0.0 <5"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install express-validation' and verify import statement.","cause":"Package not installed or import path incorrect.","error":"Cannot find module 'express-validation'"},{"fix":"Use import { Joi } from 'express-validation' instead of import Joi from 'joi'.","cause":"Joi imported from wrong source or version mismatch.","error":"TypeError: Joi.object is not a function"},{"fix":"Add import { ValidationError } from 'express-validation' to error handler.","cause":"Forgot to import ValidationError or used wrong import pattern.","error":"ValidationError is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}