Skip to content

Commit efb0040

Browse files
committed
feat: catch sync errors during validation
1 parent 7c7daed commit efb0040

7 files changed

Lines changed: 2702 additions & 955 deletions

File tree

.babelrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"plugins": [
3-
"transform-strict-mode",
4-
"transform-class-properties",
5-
"transform-object-rest-spread"
3+
"@babel/plugin-transform-strict-mode",
4+
"@babel/plugin-proposal-class-properties",
5+
["@babel/plugin-proposal-object-rest-spread", { "useBuiltIns": true }]
66
],
77
"env": {
88
"test": {

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"extends": "makeomatic"
2+
"extends": "makeomatic",
3+
"parser": "babel-eslint"
34
}

.nycrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"src/**/*.js"
44
],
55
"require": [
6-
"babel-register"
6+
"@babel/register"
77
],
88
"sourceMap": false,
99
"instrument": false,

package.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,26 @@
3030
},
3131
"homepage": "https://github.com/makeomatic/ms-validation#readme",
3232
"devDependencies": {
33-
"@makeomatic/deploy": "^6.1.2",
34-
"babel-cli": "^6.26.0",
35-
"babel-eslint": "^8.2.3",
36-
"babel-plugin-istanbul": "^4.1.6",
37-
"babel-plugin-transform-class-properties": "^6.11.5",
38-
"babel-plugin-transform-object-rest-spread": "^6.26.0",
39-
"babel-plugin-transform-strict-mode": "^6.11.3",
40-
"babel-register": "^6.26.0",
41-
"codecov": "^3.0.2",
42-
"cross-env": "^5.1.5",
43-
"eslint": "^4.19.1",
44-
"eslint-config-makeomatic": "^2.0.1",
45-
"eslint-plugin-import": "^2.12.0",
46-
"eslint-plugin-promise": "^3.7.0",
47-
"mocha": "^5.1.1",
48-
"nyc": "^11.8.0"
33+
"@makeomatic/deploy": "^7.0.1",
34+
"babel-eslint": "^8.2.6",
35+
"babel-plugin-istanbul": "^5.0.0",
36+
"@babel/cli": "^7.0.0-rc.1",
37+
"@babel/core": "^7.0.0-rc.1",
38+
"@babel/plugin-proposal-class-properties": "^7.0.0-rc.1",
39+
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-rc.1",
40+
"@babel/plugin-transform-strict-mode": "^7.0.0-rc.1",
41+
"@babel/register": "^7.0.0-rc.1",
42+
"codecov": "^3.0.4",
43+
"cross-env": "^5.2.0",
44+
"eslint": "^5.3.0",
45+
"eslint-config-makeomatic": "^3.0.0",
46+
"eslint-plugin-import": "^2.14.0",
47+
"eslint-plugin-promise": "^3.8.0",
48+
"mocha": "^5.2.0",
49+
"nyc": "^12.0.2"
4950
},
5051
"dependencies": {
51-
"ajv": "^6.5.0",
52+
"ajv": "^6.5.2",
5253
"ajv-keywords": "^3.2.0",
5354
"bluebird": "^3.5.1",
5455
"callsite": "^1.0.0",

src/index.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ const callsite = require('callsite');
66
const glob = require('glob');
77
const fs = require('fs');
88
const debug = require('debug')('ms-validation');
9-
const { ValidationError, io, NotFoundError } = require('common-errors');
9+
const {
10+
io,
11+
ValidationError,
12+
NotFoundError,
13+
InvalidOperationError,
14+
} = require('common-errors');
1015

1116
// this is taken from ajv, but removed
1217
// eslint-disable-next-line max-len
@@ -57,6 +62,16 @@ ValidationError.prototype.toJSON = function toJSON() {
5762
const json = filename => path.extname(filename) === '.json';
5863
const slashes = new RegExp(path.sep, 'g');
5964

65+
function safeValidate(validate, doc) {
66+
try {
67+
validate(doc);
68+
} catch (e) {
69+
return e;
70+
}
71+
72+
return true;
73+
}
74+
6075
/**
6176
* @namespace Validator
6277
*/
@@ -207,14 +222,17 @@ class Validator {
207222
return { error: new NotFoundError(`validator "${schema}" not found`) };
208223
}
209224

210-
validate(data);
225+
const isValidationCompleted = safeValidate(validate, data);
226+
if (isValidationCompleted !== true) {
227+
return { error: new InvalidOperationError('internal validation error', isValidationCompleted), doc: data };
228+
}
211229

212230
if (validate.errors) {
213231
const readable = this.$ajv.errorsText(validate.errors);
214232

215233
let onlyAdditionalProperties = true;
216234
const error = new ValidationError(`${schema} validation failed: ${readable}`);
217-
validate.errors.forEach((err) => {
235+
for (const err of validate.errors) {
218236
if (err.message !== 'should NOT have additional properties') {
219237
onlyAdditionalProperties = false;
220238
}
@@ -224,7 +242,7 @@ class Validator {
224242
: err.field;
225243

226244
error.addError(new ValidationError(err.message, 400, field));
227-
});
245+
}
228246

229247
error.code = onlyAdditionalProperties ? 417 : 400;
230248

@@ -251,7 +269,14 @@ class Validator {
251269
*/
252270
validate = (schema, data) => {
253271
const output = this.$validate(schema, data);
254-
if ('error' in output) {
272+
273+
if (hasOwnProperty.call(output, 'error') === true) {
274+
// so that it can be inspected later
275+
Object.defineProperty(output.error, '$orig', {
276+
value: output.doc,
277+
});
278+
279+
// reject
255280
return Promise.reject(output.error);
256281
}
257282

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
--require babel-register
1+
--require @babel/register
22
-R spec

0 commit comments

Comments
 (0)