Skip to content

Commit

Permalink
Joi 13 (#74)
Browse files Browse the repository at this point in the history
Closes #65

Updated depenedencies to work with latest version of Joi. Updated style to have
fewer overrides to the base AirBnb style. Updated travis to drop node 4 and 6.
Fixed typo in type def file.
  • Loading branch information
arb authored May 11, 2018
1 parent 686e84c commit 86119b7
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 151 deletions.
5 changes: 0 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
'use strict';

module.exports = {
extends: 'airbnb-base',
env: {
node: true
},
rules: {
'prefer-destructuring': 'off',
'no-underscore-dangle': 'off',
'comma-dangle': ['error', 'never'],
'strict': 'off',
}
};
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
sudo: false
language: node_js
node_js:
- "4"
- "6"
- "8"
- "node"

Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare namespace Celebrate {
/**
* The Joi version Celebrate uses internally.
*/
export const Joi: joi
export const Joi: joi;

/**
* Examines an error object to determine if it originated from the celebrate middleware.
Expand Down
43 changes: 21 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const Series = require('fastseries')({ results: true });
const Assert = require('assert');
const Joi = require('joi');
Expand All @@ -8,22 +6,26 @@ const validations = require('./schema');

const CELEBRATED = Symbol('isCelebrate');
const DEFAULTS = {
escapeHtml: true
escapeHtml: true,
};

const validateSource = source =>
(config, next) => {
const req = config.req;
const options = config.options;
const rules = config.rules;
const {
req,
options,
rules,
} = config;
const spec = rules.get(source);

if (!spec) {
return next(null);
}
const result = Joi.validate(req[source], spec, options);
const value = result.value;
const err = result.error;
const {
value,
error: err,
} = result;

if (value !== undefined) {
const descriptor = Object.getOwnPropertyDescriptor(req, source);
Expand All @@ -32,7 +34,7 @@ const validateSource = source =>
req[source] = value;
} else {
Object.defineProperty(req, source, {
get() { return value; }
get() { return value; },
});
}
}
Expand Down Expand Up @@ -62,11 +64,11 @@ const REQ_VALIDATIONS = [
validateHeaders,
validateParams,
validateQuery,
maybeValidateBody
maybeValidateBody,
];

const isCelebrate = (err) => {
if (err != null && typeof err === 'object') { // eslint-disable-line eqeqeq
if (err != null && typeof err === 'object') {
return err[CELEBRATED] || false;
}
return false;
Expand All @@ -78,16 +80,14 @@ const celebrate = (schema, options) => {
const rules = new Map();
const joiOpts = Object.assign({}, DEFAULTS, options);

const keys = Object.keys(schema);
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
Object.keys(schema).forEach((key) => {
rules.set(key, Joi.compile(schema[key]));
}
});
const middleware = (req, res, next) => {
Series(null, REQ_VALIDATIONS, {
req,
options: joiOpts,
rules
rules,
}, next);
};

Expand All @@ -104,27 +104,26 @@ const errors = () => (err, req, res, next) => {
message: err.message,
validation: {
source: err._meta.source,
keys: []
}
keys: [],
},
};

if (err.details) {
for (let i = 0; i < err.details.length; i += 1) {
/* istanbul ignore next */
const path = Array.isArray(err.details[i].path) ? err.details[i].path.join('.') : err.details[i].path;
const path = err.details[i].path.join('.');
error.validation.keys.push(EscapeHtml(path));
}
}
return res.status(400).send(error);
}

// If this isn't a Joi error, send it to the next error handler
// If this isn't a Celebrate error, send it to the next error handler
return next(err);
};

module.exports = {
celebrate,
Joi,
errors,
isCelebrate
isCelebrate,
};
4 changes: 1 addition & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const Joi = require('joi');

module.exports.schema = Joi.object().keys({
headers: Joi.any(),
params: Joi.any(),
query: Joi.any(),
body: Joi.any()
body: Joi.any(),
}).min(1);
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"escape-html": "1.0.3",
"fastseries": "1.7.2",
"joi": "12.x.x"
"joi": "13.x.x"
},
"devDependencies": {
"@types/express": "4.x.x",
Expand All @@ -39,11 +39,11 @@
"eslint": "4.19.x",
"eslint-config-airbnb-base": "12.1.x",
"eslint-plugin-import": "2.11.x",
"expect": "21.2.x",
"expect": "22.x.x",
"express": "5.0.0-alpha.6",
"jest": "21.2.x"
"jest": "22.x.x"
},
"engines": {
"node": ">=4.0.0"
"node": ">=8.9.0"
}
}
Loading

0 comments on commit 86119b7

Please sign in to comment.