express-contracts
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "express-contracts", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Express.js plugin for checking request and response with rho-contracts", | ||
@@ -5,0 +5,0 @@ "license": "BSD-2-Clause", |
@@ -67,7 +67,21 @@ express-contracts | ||
Finally, there is an asymmetry between the `requestContract`, which is run over | ||
the whole request (but only `body` and `query` actually checked), and the | ||
`responseBodyContract`, which is only run over the payload that eventually | ||
becomes the `res.body`. | ||
the whole request (but only `body`, `query`, and `params` are actually | ||
checked), and the `responseBodyContract`, which is only run over the payload | ||
that eventually becomes the `res.body`. | ||
As of 2.2.0, the exported `ValidationError` says which of the three checked | ||
fields caused the (first seen) error, under the key `problemField`, e.g. | ||
```js | ||
if (err.problemField === 'body') { | ||
// do something | ||
} else if (err.problemField === 'query') { | ||
// do something else | ||
} else if (err.problemField === 'params') { | ||
// do yet something else | ||
} else { | ||
// should not happen unless we start checking more fields | ||
} | ||
``` | ||
Installation | ||
@@ -74,0 +88,0 @@ ------------ |
@@ -1,4 +0,5 @@ | ||
var ValidationError = function (message) { | ||
var ValidationError = function (message, problemField) { | ||
this.name = 'ValidationError'; | ||
this.message = message; | ||
this.problemField = problemField; | ||
this.stack = (new Error()).stack; | ||
@@ -5,0 +6,0 @@ }; |
@@ -59,3 +59,3 @@ var c = require('rho-contracts-fork'); | ||
var prefix = 'Validation error in ' + relevantKeyDescriptions[key] + ':\n'; | ||
return next(new errors.ValidationError(prefix + e.message)); | ||
return next(new errors.ValidationError(prefix + e.message, key)); | ||
} | ||
@@ -62,0 +62,0 @@ }; |
@@ -24,5 +24,6 @@ var middleware = require('./middleware'), | ||
cc.errorBody = c.object({ | ||
cc.errorWithProblemFieldBody = c.object({ | ||
error: c.string, | ||
}).strict().rename('errorBody'); | ||
problemField: c.optional(c.string), | ||
}).strict().rename('errorWithProblemFieldBody'); | ||
@@ -40,3 +41,3 @@ // Each test should set appLogic (lexical variable) to customize | ||
if (err instanceof errors.ValidationError) { | ||
res.status(400).checkedJson({ error: err.message }); | ||
res.status(400).checkedJson({ error: err.message, problemField: err.problemField }); | ||
} else if (err instanceof c.ContractError) { | ||
@@ -52,3 +53,3 @@ res.status(500).checkedJson({ error: 'Internal Contract Violation' }); | ||
require('body-parser').json(), // populates req.body | ||
middleware.useContracts(cc.request, c.or(cc.responseBody, cc.errorBody)), | ||
middleware.useContracts(cc.request, c.or(cc.responseBody, cc.errorWithProblemFieldBody)), | ||
appLogicLazy, | ||
@@ -85,3 +86,4 @@ exampleHandleError | ||
should(err).equal(null); | ||
cc.errorBody.check(res.body); // sanity check | ||
cc.errorWithProblemFieldBody.check(res.body); // sanity check | ||
res.body.problemField.should.equal('body'); | ||
// Should not dump entire `req` into error message | ||
@@ -105,3 +107,4 @@ res.body.error.should.equal('Validation error in request body:\nField `foo` required, got {}\n'); | ||
should(err).equal(null); | ||
cc.errorBody.check(res.body); // sanity check | ||
cc.errorWithProblemFieldBody.check(res.body); // sanity check | ||
should.not.exist(res.body.problemField); // not for the 500's | ||
res.body.error.should.equal('Internal Contract Violation'); | ||
@@ -108,0 +111,0 @@ done(); |
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
14901
220
113