express-joi-middleware is an Express.js middleware designed to integrate Hapi.js's Joi validation library seamlessly into Express applications. Currently at its first stable release, version 1.0.0, this package provides robust request body, query, parameters, and headers validation against user-defined Joi schemas. Its key differentiators include extensive customization options, such as the ability to supply a specific Joi instance (useful for managing Joi versions or custom extensions), fine-grained control over error response handling (either automatically sending a 400 Bad Request or `next`ing the Joi error for custom handling), and the choice to override the `req.body` or create a new `req.validated` key with the processed data. The project is new, so a specific release cadence has not yet been established, but it focuses on providing a flexible and configurable validation layer.
npm install express-joi-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `express-joi-middleware` in an Express application to validate request bodies and path parameters using Joi schemas, including explicit Joi import and an error handling middleware.
Always include `const Joi = require('joi');` at the top of your file when defining Joi schemas for `express-joi-middleware`.Either set `wantResponse: true` in the middleware options for automatic 400 Bad Request responses, or implement an Express error-handling middleware (`app.use((err, req, res, next) => { ... })`) to specifically handle `err.isJoi` validation errors.Use `const expressJoiMiddleware = require('express-joi-middleware');` for importing the middleware.Add `const Joi = require('joi');` at the top of the file where your Joi schemas are defined.Ensure you are calling `expressJoiMiddleware(schemaObject, optionsObject)` to return the middleware function, e.g., `app.post('/path', expressJoiMiddleware(mySchema), (req, res) => ...);`.Review the Joi schema definition and the input data being sent to ensure they match the expected validation rules. The error message from Joi often provides specific details on what failed.