Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

celebrate

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

celebrate

A joi validation middleware for Express.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Celebrate

Logo design by chris.ruppert@gmail.com

Current Version Build Status Dependencies

belly-button-style

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, and ensure that all of your inputs are correct before any handler function. The middleware allows you to validate req.params, req.headers, req.query and req.body (provided you are using body-parser).

Usage

const express = require('express');
const BodyParser = require('body-parser');
const Joi = require('joi');
const Celebrate = require('celebrate');

const app = express();
app.use(BodyParser.json());
app.use(Logger());

app.post('/signup', Celebrate({
  body: Joi.object().keys({
    name: Joi.string().required(),
    age: Joi.number().integer(),
    role: Joi.string().default('admin')
  })
}), (req, res) => {
  // At this point, req.body has been validated and is equal to req.body.name if provided in the POST or set to 'admin' by joi
});

// By default, Express will try to send our errors back as HTML, if you want the JSON, add an error handler here
app.use((err, req, res) => {
  if (err.isJoi) {
    return res.status(400).send(err);
  }
  res.status(500).send('Some other error');
});

API

celebrate(schema)

Returns a function with the middleware signature ((req, res, next)).

  • schema - a object where key can be one of 'params', 'headers', 'query', and 'body' and the value is a joi validation schema. Only the keys specified will be validated against the incomming req object. If you omit a key, that part of the req object will not be validated. A schema must contain at least one of the valid keys.

Order

celebrate validates req values in the following order:

  1. req.headers
  2. req.params
  3. req.query
  4. req.body

If at any point, any of the validation fails, the entire request will be considered invalid and the rest of the validation will be short-circuited.

Issues

Before opening issues on this repo, make sure your joi schema is correct and working like it's supposed to. The bulk of this code is just exposing the joi API as Express middleware. All of the heavy lifting still happens inside joi.

Keywords

FAQs

Package last updated on 02 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc