Skip to content

Commit

Permalink
updated joi to version 12 (arb#39)
Browse files Browse the repository at this point in the history
* updated joi to version 12

* Force Joi to escape html by default

* Add test for the escapeHTML default option for full coverage

* Not using default function parameters, Node 4 compatible

* Update README to document difference between joi and celebrate behavior

* Fixed README mistake

* Simplified default assignment for options
  • Loading branch information
giltho authored and arb committed Oct 31, 2017
1 parent 7a89dbb commit 80d81bc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ app.use(Celebrate.errors());
The single exported function from `celebrate`. 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](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.
- `[options]` - `joi` [options](https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback) that are passed directly into the `validate` function.
- `[options]` - `joi` [options](https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback) that are passed directly into the `validate` function. Defaults to `{ escapeHtml: true }`. This is differs from the Joi defaults since version 12.

### `Celebrate.errors()`

Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const Celebrate = (schema, options) => {
Assert.ifError(result.error);
const rules = new Map();

const defaults = {
escapeHtml: true
};
options = Object.assign(defaults, options);

const keys = Object.keys(schema);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"escape-html": "1.0.3",
"fastseries": "1.7.2",
"joi": "11.x.x"
"joi": "12.x.x"
},
"devDependencies": {
"@types/express": "4.x.x",
Expand Down
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@ describe('Celebrate Middleware', () => {
});
});

it('honors the escapeHtml Joi option', (done) => {
const middleware = Celebrate({
headers: {
accept: Joi.string().regex(/xml/)
}
}, { escapeHtml: false });

middleware({
headers: {
accept: 'application/json'
}
}, null, (err) => {
expect(err).to.exist();
expect(err.isJoi).to.be.true();
expect(err.details[0].message).to.equal('"accept" with value "application/json" fails to match the required pattern: /xml/');
done();
});
});

describe('errors()', () => {
it('responds with a joi error', (done) => {
const handler = Celebrate.errors();
Expand Down

0 comments on commit 80d81bc

Please sign in to comment.