API Reference

celebrate

Express middleware that validates requests with Joi schemas and returns standardized, structured validation errors.

Official Documentation ↗
CompatibilityThis documentation covers version v16.0.0. Check the official docs for the latest API surface.

Installation

npmnpm install celebrate joi
pnpmpnpm add celebrate joi

Methods

Overview

celebrate is Express middleware that validates requests with Joi schemas and turns failures into structured JSON errors. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • celebrate — Builds middleware that validates the selected request segments against Joi schemas.
  • errors — Error handler that formats celebrate validation failures into a consistent JSON response.
  • Segments — Constants that select which part of the request a schema applies to.

Example

const { celebrate, Joi, Segments } = require('celebrate');

app.post('/users',
  celebrate({
    [Segments.BODY]: Joi.object({ email: Joi.string().email().required() }),
  }),
  (req, res) => res.json(req.body));

Notes

Register the celebrate errors middleware after your routes so validation failures are formatted before the generic error handler.

Verification

Send a request with an invalid field and confirm the response is a 400 with a validation object naming that field.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/arb/celebrate@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:48:41.051Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

What error format does celebrate return by default?
A 400 response of { statusCode, error, message, validation: { source, keys } } describing the failed segment and fields.
Can I validate headers?
Yes. Target Segments.HEADERS with a Joi schema; celebrate lowercases header keys for validation.
How do I customize the error response?
Pass options to errors(), including a custom formatter function, to reshape the emitted structure.