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

celebrate

Package Overview
Dependencies
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

celebrate - npm Package Compare versions

Comparing version 6.0.1 to 7.0.0

25

lib/index.d.ts
import { ErrorRequestHandler, RequestHandler } from 'express';
/**
* Creates a Celebrate middleware function.
* @param {object} schema object where each key is one of ["params", "headers", "query", "body"] and the value is
* a Joi schema.
* @param {object} config optional configuration options that will be passed directly into Joi.
*/
declare function Celebrate (schema: {
params?: object,
headers?: object,
query?: object,
body?: object,
}, config?: object): RequestHandler;
declare namespace Celebrate {
/**
* Creates a Celebrate middleware function.
* @param {object} schema object where each key is one of ["params", "headers", "query", "body"] and the value is
* a Joi schema.
* @param {object} config optional configuration options that will be passed directly into Joi.
*/
function celebrate(schema: {
params?: object,
headers?: object,
query?: object,
body?: object,
}, config?: object): RequestHandler
/**
* Creates a Celebrate error handler middleware function.

@@ -19,0 +18,0 @@ */

@@ -9,15 +9,50 @@ 'use strict';

const validations = require('./schema');
const isCelebrate = Symbol('isCelebrate');
const DEFAULTS = {
escapeHtml: true
};
const Celebrate = (schema, options) => {
const validateSource = (source) => {
return (config, next) => {
const req = config.req;
const options = config.options;
const rules = config.rules;
const spec = rules.get(source);
if (!spec) {
return next(null);
}
Joi.validate(req[source], spec, options, (err, value) => {
if (value !== undefined) {
const descriptor = Object.getOwnPropertyDescriptor(req, source);
/* istanbul ignore next */
if (descriptor && descriptor.writable) {
req[source] = value;
} else {
Object.defineProperty(req, source, {
get () { return value; }
});
}
}
if (err) {
err[isCelebrate] = true;
err._meta = { source };
return next(err);
}
return next(null);
});
};
};
const validateHeaders = validateSource('headers');
const validateParams = validateSource('params');
const validateQuery = validateSource('query');
const validateBody = validateSource('body');
const celebrate = (schema, options) => {
const result = Joi.validate(schema || {}, validations.schema);
Assert.ifError(result.error);
const rules = new Map();
options = Object.assign({}, DEFAULTS, options);
const defaults = {
escapeHtml: true
};
options = Object.assign(defaults, options);
const keys = Object.keys(schema);

@@ -28,40 +63,19 @@ for (let i = 0; i < keys.length; i++) {

}
const validateSource = (source) => {
return (req, callback) => {
const spec = rules.get(source);
if (!spec) {
return callback(null);
}
Joi.validate(req[source], spec, options, (err, value) => {
if (value !== undefined) {
// Apply any Joi transforms back to the request
req[source] = value;
}
if (err) {
err[isCelebrate] = true;
err._meta = { source };
return callback(err);
}
return callback(null);
});
};
};
const validateBody = validateSource('body');
const middleware = (req, res, next) => {
Series(null, [
validateSource('headers'),
validateSource('params'),
validateSource('query'),
function (req, callback) {
const method = req.method.toLowerCase();
validateHeaders,
validateParams,
validateQuery,
(config, callback) => {
const method = config.req.method.toLowerCase();
if (method === 'get' || method === 'head') {
return callback(null);
}
validateBody(req, callback);
validateBody(config, callback);
}
], req, next);
], {
req,
options,
rules
}, next);
};

@@ -74,3 +88,3 @@

Celebrate.errors = () => {
const errors = () => {
return (err, req, res, next) => {

@@ -103,4 +117,7 @@ if (err[isCelebrate]) {

Celebrate.Joi = Joi;
module.exports = Celebrate;
module.exports = {
celebrate,
Joi,
errors
};
{
"name": "celebrate",
"version": "6.0.1",
"version": "7.0.0",
"description": "A joi validation middleware for Express.",

@@ -34,4 +34,7 @@ "main": "lib/index.js",

"@types/express": "4.x.x",
"artificial": "0.1.x",
"belly-button": "4.x.x",
"body-parser": "1.18.2",
"expect": "21.2.x",
"express": "5.0.0-alpha.6",
"jest": "21.2.x"

@@ -38,0 +41,0 @@ },

@@ -19,2 +19,5 @@ ![Celebrate](https://github.com/continuationlabs/celebrate/raw/master/images/logo.png)

## Express Compatibility
Celebrate is tested and has full compatibility with express 4 and 5. It should work correctly with express 3, but including it in the testing was proving to be more trouble than it's worth. This is primarily because express 3 stores exposes route parameters as an array, rather than an object.
## Usage

@@ -26,4 +29,3 @@

const BodyParser = require('body-parser');
const Celebrate = require('celebrate');
const { Joi } = Celebrate;
const { celebrate, Joi, errors } = require('celebrate');

@@ -33,3 +35,3 @@ const app = express();

app.post('/signup', Celebrate({
app.post('/signup', celebrate({
body: Joi.object().keys({

@@ -47,3 +49,3 @@ name: Joi.string().required(),

});
app.use(Celebrate.errors());
app.use(errors());
```

@@ -54,4 +56,3 @@

const express = require('express');
const Celebrate = require('celebrate');
const { Joi } = Celebrate;
const { celebrate, Joi, errors } = require('celebrate');
const app = express();

@@ -68,3 +69,3 @@

app.get('/foo', (req, res) => { res.send('a foo request'); });
app.use(Celebrate.errors());
app.use(errors());
```

@@ -74,5 +75,5 @@

### `Celebrate(schema, [options])`
### `celebrate(schema, [options])`
The single exported function from `celebrate`. Returns a `function` with the middleware signature (`(req, res, next)`).
Returns a `function` with the middleware signature (`(req, res, next)`).

@@ -82,3 +83,3 @@ - `schema` - a object where `key` can be one of `'params', 'headers', 'query', and 'body'` and the `value` is a [joi](https://github.com/hapijs/joi/blob/master/API.md) validation schema. Only the `key`s specified will be validated against the incoming `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.

### `Celebrate.errors()`
### `errors()`

@@ -89,3 +90,3 @@ Returns a `function` with the error handler signature (`(err, req, res, next)`). This should be placed with any other error handling middleware to catch Joi validation errors. If the incoming `err` object is a Joi error, `errors()` will respond with a 400 status code and the Joi validation message. Otherwise, it will call `next(err)` and will pass the error along and need to be processed by another error handler.

### `Celebrate.Joi`
### `Joi`

@@ -92,0 +93,0 @@ `celebrate` exports the version of joi it is using internally. For maximum compatibility, you should use this version when passing in any validation schemas.

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