Using Celebrate with node.JS

Murphy Ekwere
3 min readApr 21, 2022

What is Celebrate ? and how do we make use of it in Node.JS?

Celebrate is an express middleware function that wraps the joi validation library. This allows you to use this middleware in any single route, or globally, ensuring that all inputs used are correct before any handler function.

Before going further what are Middleware functions?

They are functions that have access to the request object ( req ), the response object ( res ), and the next function in the application’s request-response cycle.

They carry out the following tasks :

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware in the stack.

Celebrate as a middleware function has been tested and found to have full compatibility with express 4 and 5. It likely works correctly with express 3ss, but including it in the test matrix causes more trouble than it's worth, so i don’t advice it. This is primarily because express 3 exposes route parameters as an array rather than an object.

The middleware allows you to validate

req.params, req.headers, req.query.

as well as

req.body- provided you make use ofbody-parser

req.cookies,- provided you make use of cookie-parser

req.signedcookies,- provided you make use of cookie-parser

Joi is listed as a formal dependency in Celebrate. This means that celebrate as a middleware function will always use a predictable known version of joi during the validation and compilation steps.

There are a few reasons why and they are as follows:

(1) Celebrate will be given access to the latest version of Joi as soon as it is published

(2) Compatibility of the Joi version so that it can be exported to the user and with that maximizing compatibility with the user

An Example of using celebrate on a single POST route to validate req.body.

const express = require('express');
const BodyParser = require('body-parser');
const { celebrate, Joi, errors, Segments } = require('celebrate');
const app = express();
app.use(BodyParser.json());
app.post('/signup', celebrate({
[Segments.BODY]: Joi.object().keys({
name: Joi.string().required(),
age: Joi.number().integer(),
role: Joi.string().default('admin')
}),
[Segments.QUERY]: {
token: Joi.string().token().required()
}
}), (req, res) => {
// At this point, req.body has been validated and
// req.body.role is equal to req.body.role if provided in the POST or set to 'admin' by joi
});
app.use(errors());

Example of using celebrate to validate all incoming requests to ensure the token header is present and matches the supplied regular expression.

const express = require('express');
const { celebrate, Joi, errors, Segments } = require('celebrate');
const app = express();
// validate all incoming request headers for the token header
// if missing or not the correct format, respond with an error
app.use(celebrate({
[Segments.HEADERS]: Joi.object({
token: Joi.string().required().regex(/abc\d{3}/)
}).unknown()
}));
app.get('/', (req, res) => { res.send('hello world'); });
app.get('/foo', (req, res) => { res.send('a foo request'); });
app.use(errors());

In conclusion according the the HTTP spec, GET requests should not include a body in the request payload. For that reason, celebrate does not validate the body on GET requests.

Congratulations you now know what Celebrate is and how to use it.

--

--

Murphy Ekwere

I am a Software Developer with a passion for solving UI/UX problems with javascript and CSS. I also enjoy sharing knowledge about the tools I work with