Comparing version 10.0.1 to 10.1.0
import { ErrorRequestHandler, RequestHandler } from 'express'; | ||
import { Root as joi, ValidationOptions } from '@hapi/joi'; | ||
import { | ||
Root as joi, | ||
ValidationOptions, | ||
ValidationError, | ||
ValidationResult | ||
} from '@hapi/joi'; | ||
@@ -44,3 +49,3 @@ declare namespace Celebrate { | ||
*/ | ||
function errors(): () => ErrorRequestHandler; | ||
function errors(): ErrorRequestHandler; | ||
/** | ||
@@ -55,4 +60,16 @@ * The Joi version Celebrate uses internally. | ||
function isCelebrate(err: object): boolean; | ||
/** | ||
* Format a joi error into a standard object | ||
*/ | ||
function format(err: ValidationResult<object>, | ||
source: "params" | "headers" | "query" | "cookies" | "signedCookies" | "body", | ||
opts?: { celebrated: boolean }): { | ||
meta: { | ||
source: "params" | "headers" | "query" | "cookies" | "signedCookies" | "body" | ||
}, | ||
joi: ValidationError, | ||
} | ||
} | ||
export = Celebrate; |
const Assert = require('assert'); | ||
const Joi = require('@hapi/joi'); | ||
const EscapeHtml = require('escape-html'); | ||
const { middlewareSchema, celebrateSchema } = require('./schema'); | ||
const get = require('lodash.get'); | ||
const { | ||
middlewareSchema, celebrateSchema, sourceSchema, optSchema, | ||
} = require('./schema'); | ||
const CELEBRATED = Symbol('CELEBRATED'); | ||
const CELEBRATED = Symbol('celebrated'); | ||
const DEFAULT_JOI_OPTIONS = { | ||
escapeHtml: true, | ||
}; | ||
const DEFAULT_FORMAT_OPTIONS = { | ||
celebrated: true, | ||
}; | ||
const validateSource = source => ({ | ||
const _format = (error, source, opts) => ({ | ||
[CELEBRATED]: opts.celebrated, | ||
joi: error, | ||
meta: { source }, | ||
}); | ||
const format = (err, source, opts = { celebrated: false }) => { | ||
Assert.ok(get(err, 'error.isJoi', false)); | ||
let result = Joi.validate(source, sourceSchema); | ||
Assert.ifError(result.error); | ||
result = Joi.validate(opts, optSchema); | ||
Assert.ifError(result.error); | ||
const { | ||
error, | ||
} = err; | ||
return _format(error, source, opts); | ||
}; | ||
const validateSource = (source) => ({ | ||
celebrateOpts, | ||
@@ -35,7 +62,3 @@ joiOpts, | ||
if (error) { | ||
return { | ||
[CELEBRATED]: true, | ||
joi: error, | ||
meta: { source }, | ||
}; | ||
return _format(error, source, DEFAULT_FORMAT_OPTIONS); | ||
} | ||
@@ -83,3 +106,3 @@ return null; | ||
const rules = new Map(); | ||
const joiOpts = Object.assign({}, DEFAULT_JOI_OPTIONS, joiOptions); | ||
const joiOpts = { ...DEFAULT_JOI_OPTIONS, ...joiOptions }; | ||
@@ -142,5 +165,6 @@ Object.entries(schema).forEach(([key, value]) => rules.set(key, Joi.compile(value))); | ||
celebrate, | ||
errors, | ||
Joi, | ||
errors, | ||
isCelebrate, | ||
format, | ||
}; |
@@ -15,1 +15,7 @@ const Joi = require('@hapi/joi'); | ||
}); | ||
exports.sourceSchema = Joi.string().valid(['headers', 'params', 'query', 'cookies', 'signedCookies', 'body']); | ||
exports.optSchema = Joi.object({ | ||
celebrated: Joi.boolean().default(false), | ||
}); |
{ | ||
"name": "celebrate", | ||
"version": "10.0.1", | ||
"version": "10.1.0", | ||
"description": "A joi validation middleware for Express.", | ||
@@ -33,14 +33,16 @@ "main": "lib/index.js", | ||
"@hapi/joi": "15.x.x", | ||
"escape-html": "1.0.3" | ||
"escape-html": "1.0.3", | ||
"lodash.get": "4.4.x" | ||
}, | ||
"devDependencies": { | ||
"@hapi/teamwork": "3.3.x", | ||
"@types/express": "4.x.x", | ||
"@types/hapi__joi": "15.x.x", | ||
"artificial": "0.1.x", | ||
"artificial": "1.x.x", | ||
"benchmark": "2.1.x", | ||
"body-parser": "1.18.x", | ||
"body-parser": "1.19.x", | ||
"cookie-parser": "1.4.x", | ||
"cookie-signature": "1.1.x", | ||
"eslint": "5.x.x", | ||
"eslint-config-airbnb-base": "13.x.x", | ||
"eslint": "6.x.x", | ||
"eslint-config-airbnb-base": "14.x.x", | ||
"eslint-plugin-import": "2.x.x", | ||
@@ -47,0 +49,0 @@ "expect": "24.x.x", |
@@ -50,2 +50,3 @@ [![celebrate](https://github.com/arb/celebrate/raw/master/images/logo.svg?sanitize=1)](https://www.npmjs.org/package/celebrate) | ||
- [`isCelebrate(err)`](#iscelebrateerr) | ||
- [`format(err, source, [opts])`](#formaterr-source-opts) | ||
- [Validation Order](#validation-order) | ||
@@ -130,3 +131,3 @@ - [Issues](#issues) | ||
Errors origintating `celebrate()` are objects with the following keys: | ||
Errors origintating from `celebrate()` are objects with the following keys: | ||
- `joi` - The full [joi error object](https://github.com/hapijs/joi/blob/master/API.md#errors). | ||
@@ -146,2 +147,19 @@ - `meta` - On `object` with the following keys: | ||
### `format(err, source, [opts])` | ||
Formats the incomming values into the shape of celebrate [errors](#errors()) | ||
- `err` - a Joi validation error object | ||
- `source` - A `string` indicating the step where the validation failed. Will be one of `'params'`, `'headers'`, `'query'`, `'cookies'`, `'signedCookies'`, or `'body'` | ||
- `[opts]` - optional `object` with the following keys | ||
- `celebrated` - `bool` that, when `true`, adds `Symbol('celebrated'): true` to the result object. This indicates this error as originating from `celebrate`. You'd likely want to set this to `true` if you want the celebrate error handler to handle errors originating from the `format` function that you call in user-land code. Defaults to `false`. | ||
<details> | ||
<summary>Sample usage</summary> | ||
```js | ||
const result = Joi.validate(req.params.id, Joi.string().valid('foo'), { abortEarly: false }); | ||
const err = format(result, 'params'); | ||
``` | ||
</details> | ||
## Validation Order | ||
@@ -148,0 +166,0 @@ |
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
18599
227
178
3
18
+ Addedlodash.get@4.4.x
+ Addedlodash.get@4.4.2(transitive)