express-joi-validator

raw JSON →
2.0.1 verified Sat Apr 25 auth: no javascript maintenance

Express middleware for request validation using the Joi validation library. Version 2.0.1 is the current stable release with no recent updates; it requires joi 6.x.x as a peer dependency. It validates req.query, req.body, and req.params based on Joi schemas, returning Boom errors on failure. Known for its simplicity but limited to Joi v6 and common pitfalls around error handling and required dependencies.

error Cannot find module 'joi'
cause joi is a peer dependency and not automatically installed.
fix
Run: npm install joi@6.x.x
error err.isBoom is not a function
cause Error object is a Boom error from joi, but code tries to call it as a function.
fix
Check err.isBoom as a boolean property, not a method: if (err.isBoom) { ... }
error req.body is undefined
cause Missing body-parser middleware before validation.
fix
Add app.use(bodyParser.json()) before routes.
breaking Joining two Joi versions causes validation failures
fix Ensure joi peer dependency is 6.x.x exactly; using joi v7+ will break validation.
deprecated Joi v6 is outdated and unmaintained; many security fixes in later versions.
fix Consider migrating to express-validation or use joi v6 with caution.
gotcha Missing body-parser or express.json() causes empty req.body
fix Add body-parser.json() middleware before using express-joi-validator.
gotcha Not catching Boom errors crashes the server on validation failure
fix Add an express error middleware that checks err.isBoom.
gotcha Schema structure must be { body, query, params }
fix Ensure object keys are exactly 'body' or 'query' or 'params'
npm install express-joi-validator
yarn add express-joi-validator
pnpm add express-joi-validator

Express server with GET and POST routes using express-joi-validator for query and body validation.

const express = require('express');
const bodyParser = require('body-parser');
const expressJoi = require('express-joi-validator');
const Joi = require('joi');

const app = express();
app.use(bodyParser.json());

const querySchema = {
  query: {
    limit: Joi.number().default(10).min(10).max(100),
    offset: Joi.number().default(10).min(10).max(100)
  }
};

app.get('/', expressJoi(querySchema), (req, res) => {
  res.json({ limit: req.query.limit, offset: req.query.offset });
});

const bodySchema = {
  body: {
    name: Joi.string().required()
  }
};

app.post('/', expressJoi(bodySchema), (req, res) => {
  res.json({ name: req.body.name });
});

app.use((err, req, res, next) => {
  if (err.isBoom) {
    return res.status(err.output.statusCode).json(err.output.payload);
  }
  next(err);
});

app.listen(8080);