Comparing version 1.0.3 to 2.0.0
{ | ||
"name": "celebrate", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A joi validation middleware for Express.", | ||
@@ -27,8 +27,11 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"insync": "2.x.x", | ||
"joi": "8.4.x" | ||
"insync": "2.x.x" | ||
}, | ||
"peerDependencies": { | ||
"joi": "9.x.x" | ||
}, | ||
"devDependencies": { | ||
"belly-button": "2.x.x", | ||
"code": "3.x.x", | ||
"joi": "9.x.x", | ||
"lab": "10.x.x" | ||
@@ -35,0 +38,0 @@ }, |
@@ -7,3 +7,2 @@ ![Celebrate](https://github.com/continuationlabs/celebrate/raw/master/images/logo.png) | ||
[![Build Status](https://travis-ci.org/continuationlabs/celebrate.svg?branch=master)](https://travis-ci.org/continuationlabs/celebrate) | ||
[![Dependencies](https://img.shields.io/david/continuationlabs/celebrate.svg)](https://github.com/continuationlabs/celebrate) | ||
@@ -14,4 +13,7 @@ [![belly-button-style](https://cdn.rawgit.com/continuationlabs/belly-button/master/badge.svg)](https://github.com/continuationlabs/belly-button) | ||
`celebrate` uses ["peerDependencies"](https://docs.npmjs.com/files/package.json#peerdependencies) to manage the required version of `joi` it will use. This means that if you're using npm@3, *you must* install a compatible version of `joi` (currently *9.x.x*) as a top level dependency for `celebrate` to work correctly. `celebrate` does *not* install its own copy of `joi` when using npm@3. This is to maximize compatibility and to keep the number of `joi` version mismatch bugs to a minimum. | ||
## Usage | ||
Example of using `celebrate` on a single POST route to validate `req.body`. | ||
```js | ||
@@ -25,3 +27,2 @@ const express = require('express'); | ||
app.use(BodyParser.json()); | ||
app.use(Logger()); | ||
@@ -38,3 +39,4 @@ app.post('/signup', Celebrate({ | ||
}), (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 | ||
// At this point, req.body has been validated and | ||
// req.body.name is equal to req.body.name if provided in the POST or set to 'admin' by joi | ||
}); | ||
@@ -51,2 +53,26 @@ | ||
Example of using `celebrate` to validate all incoming requests to ensure the `token` header is present and mathes the supplied regular expression. | ||
```js | ||
const express = require('express'); | ||
const Joi = require('joi'); | ||
const Celebrate = require('celebrate'); | ||
const app = express(); | ||
// valide all incoming request headers for the token header | ||
// if missing or not the correct format, respond with an error | ||
app.use(Celebrate({ | ||
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((err, req, res) => { | ||
if (err.isJoi) { | ||
return res.status(400).send(err); | ||
} | ||
res.status(500).send('Some other error'); | ||
}); | ||
``` | ||
## API | ||
@@ -73,2 +99,2 @@ | ||
*Before* opening issues on this repo, make sure your joi schema is correct and workingas you intend. The bulk of this code is just exposing the joi API as Express middleware. All of the heavy lifting still happens inside joi. | ||
*Before* opening issues on this repo, make sure your joi schema is correct and working as you intended. The bulk of this code is just exposing the joi API as Express middleware. All of the heavy lifting still happens inside joi. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6591
94
4
+ Addeditems@2.2.1(transitive)
+ Addedjoi@9.2.0(transitive)
- Removedjoi@8.4.x
- Removedjoi@8.4.2(transitive)