express-joi-validations is an Express.js middleware package designed to streamline request validation using the Joi schema validation library. Currently at version 0.1.0, it provides a flexible mechanism to validate various parts of an incoming HTTP request, including headers, URL parameters, query strings, and the request body. While its last publish was over two years ago (as of May 2024), suggesting a maintenance-only status rather than active development, it offers both a consolidated validation middleware for multiple request parts and individual helper functions (e.g., `validateBody`, `validateParams`) for more granular control. Key differentiators include its explicit handling of validated data through `request.validationValues` and the ability to optionally overwrite the original request data or throw errors for integration with asynchronous error handling. This allows developers to strictly enforce data integrity at the API layer.
npm install express-joi-validationsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up an Express server with multiple Joi validations for a PUT request, including headers, parameters, and body, and integrates a basic error handling middleware.
Initialize `expressJoiValidations({ throwErrors: true })` globally or pass `{ throwErrors: true }` as a Joi option to individual validation helper functions. Consider using `express-async-errors` alongside this if using asynchronous routes.Either set `overwriteRequest: true` in the middleware configuration if you want the original request properties to be mutated, or consistently access validated data from `req.validationValues`.
Exercise caution when upgrading `express` or `joi` dependencies. Thoroughly test existing validations after dependency updates. Consider alternative, more actively maintained Joi-Express validation libraries if long-term stability with latest versions is critical.
Ensure that the client sends a non-empty request body if required by the schema, or update the Joi schema to allow optional fields or an empty object using `.optional()` or `.allow({})`.Verify that `throwErrors: true` is configured where validation is performed, allowing the `express-joi-validations` middleware to throw Joi-specific errors that your error handler can then catch and process.
Ensure that the schema is correctly defined using `Joi.object({})`, `Joi.string()`, etc., and is a valid Joi schema object before passing it to the validation middleware.