express-json-schema-validation
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -7,3 +7,3 @@ 'use strict'; | ||
class ValidationError extends Error { | ||
constructor(message, errors) { | ||
constructor(pointer, message) { | ||
super(); | ||
@@ -14,5 +14,5 @@ Error.captureStackTrace(this, this.constructor); | ||
this.message = message; | ||
this.errors = errors; | ||
this.pointer = pointer; | ||
} | ||
} | ||
exports.default = ValidationError; |
@@ -18,5 +18,5 @@ 'use strict'; | ||
return schema => (req, res, next) => { | ||
const error = validator.validate(schema, req.body); | ||
if (error) { | ||
return next(error); | ||
const errors = validator.validate(schema, req.body); | ||
if (errors) { | ||
return next(errors); | ||
} | ||
@@ -23,0 +23,0 @@ next(); |
@@ -21,3 +21,3 @@ 'use strict'; | ||
function getErrorMessage(err) { | ||
function getErrorData(err) { | ||
switch (err.keyword) { | ||
@@ -51,2 +51,7 @@ case 'required': | ||
function getError(err) { | ||
err = getErrorData(err); | ||
return new _validationError2.default(err.pointer, err.message); | ||
} | ||
class Validator { | ||
@@ -64,6 +69,5 @@ constructor(schemas) { | ||
const errors = _lodash2.default.map(this.ajv.errors, getErrorMessage); | ||
return new _validationError2.default('Validation error', errors); | ||
return _lodash2.default.map(this.ajv.errors, getError); | ||
} | ||
} | ||
exports.default = Validator; |
{ | ||
"name": "express-json-schema-validation", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Express JSON Schema validation middleware", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -41,7 +41,4 @@ # express-json-schema-validation | ||
``` | ||
function handleError (err, req, res, next) { | ||
switch(err.code) { | ||
'EVALIDATION': | ||
res.status(422).json(err) | ||
} | ||
function handleErrors (errors, req, res, next) { | ||
res.status(422).json(errors) | ||
} | ||
@@ -53,5 +50,5 @@ ``` | ||
const validator = new Validator({ model }) | ||
const error = validator.validate('model', object) | ||
if (error) { | ||
// ValidationError | ||
const errors = validator.validate('model', object) | ||
if (errors) { | ||
// ValidationErrors | ||
} | ||
@@ -64,7 +61,5 @@ ``` | ||
code: 'EVALIDATION', | ||
errors: [{ | ||
pointer: '/id' // invalid property pointer, | ||
message: 'validation error' // validation message | ||
}] | ||
pointer: '/id' // invalid property pointer, | ||
message: 'validation error' // validation message | ||
}) | ||
``` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9289
12
96
0
63