Celebrate is a robust Express.js middleware designed for integrating Joi validation seamlessly into web applications. It allows developers to define validation schemas for various parts of an incoming request, including `req.params`, `req.headers`, `req.query`, `req.body`, `req.cookies`, and `req.signedCookies`. The library is currently stable at version 15.0.3 and undergoes regular maintenance with notable major version updates introducing breaking changes (e.g., v15, v14, v13, v8, v4, v3, v2). A key differentiator is its formal dependency on `joi`, ensuring a consistent and up-to-date Joi version is always used and also exported for consumer compatibility. It aims to simplify input validation in Express routes, providing a consistent error handling mechanism before any route handler is executed.
npm install celebrateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic Express setup with `celebrate` middleware for input validation on a POST route. It validates `req.body` and `req.headers` using Joi schemas and includes the `errors()` middleware for consistent error responses. It also shows how to access the `Joi` instance exported by `celebrate` for custom schema definitions.
Change `import Joi from 'celebrate'` to `import { Joi } from 'celebrate';` or `const { Joi } = require('celebrate');` for CommonJS.If you require the previous behavior of collecting all validation errors before responding, explicitly set `opts.mode = Modes.FULL` in the `celebrate` middleware options. `import { celebrate, Modes } from 'celebrate'; celebrate(schema, null, { mode: Modes.FULL })`.Ensure `joi` is updated to a compatible version (v17+). Review error handling and `req.params` schema definitions. Migrate away from `celebrator` if used.
If importing Joi directly from `celebrate`, ensure you are using the correct named import: `import { Joi } from '@hapi/celebrate';` or `const { Joi } = require('@hapi/celebrate');`. In later versions, it reverted to `Joi` from `celebrate`.Upgrade Node.js to version 10 or newer. It is recommended to use an actively maintained Node.js LTS version.
Be aware that the `req` object can be modified. If you need the original request data, consider cloning it before `celebrate` middleware or using Joi's `stripUnknown` option if you only want to remove unvalidated properties.
Avoid sending bodies with GET requests. Use `req.query` or `req.params` for data in GET requests and define your schemas accordingly.
For ES Modules and TypeScript: `import { celebrate } from 'celebrate';`. For CommonJS: `const { celebrate } = require('celebrate');`. Ensure `package.json` has `"type": "module"` for ESM.Ensure the client is sending all required fields as defined in your Joi schema. Double-check field names and casing.
Add `app.use(bodyParser.json());` or `app.use(express.json());` before your `celebrate` middleware. Ensure it's active for the routes where `req.body` validation occurs.
Transition your project to ES Modules by adding `"type": "module"` to your `package.json` and updating all `require()` statements to `import` statements. Alternatively, investigate if a CommonJS-compatible version or wrapper is available.