Skip to content

Commit

Permalink
Additional test and readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Pastor committed Nov 16, 2020
1 parent 1d83cbb commit 297cd92
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -569,6 +569,21 @@ Determines whether the validator should validate responses. Also accepts respons
}
```

**onError:**

A function that will be invoked on response validation error, instead of the default handling. Useful if you want to log an error or emit a metric, but don't want to actually fail the request. Receives the validation error and offending response body.

For example:

```
validateResponses: {
onError: (error, body) => {
console.log(`Response body fails validation: `, error);
console.debug(body);
}
}
```

### 鈻笍 validateSecurity (optional)

Determines whether the validator should validate securities e.g. apikey, basic, oauth2, openid, etc
Expand Down
21 changes: 19 additions & 2 deletions test/response.validation.on.error.spec.ts
Expand Up @@ -16,8 +16,11 @@ describe(packageJson.name, () => {
{
apiSpec: apiSpecPath,
validateResponses: {
onError: function() {
onError: function(_err, body) {
onErrorArgs = Array.from(arguments);
if (body[0].id === 'bad_id_throw') {
throw new Error('error in onError handler');
}
}
},
},
Expand All @@ -29,8 +32,10 @@ describe(packageJson.name, () => {
});
app.get(`${app.basePath}/pets`, (req, res) => {
let json = {};
if ((req.query.mode = 'bad_type')) {
if (req.query.mode === 'bad_type') {
json = [{ id: 'bad_id', name: 'name', tag: 'tag' }];
} else if (req.query.mode === 'bad_type_throw') {
json = [{ id: 'bad_id_throw', name: 'name', tag: 'tag' }];
}
return res.json(json);
});
Expand Down Expand Up @@ -73,4 +78,16 @@ describe(packageJson.name, () => {
expect(r.body).is.an('array').with.length(3);
expect(onErrorArgs).to.equal(null);
}));

it('returns error if custom error handler throws', async () =>
request(app)
.get(`${app.basePath}/pets?mode=bad_type_throw`)
.expect(500)
.then((r: any) => {
const data = [{ id: 'bad_id_throw', name: 'name', tag: 'tag' }];
expect(r.body.message).to.equal('error in onError handler');
expect(onErrorArgs.length).to.equal(2);
expect(onErrorArgs[0].message).to.equal('.response[0].id should be integer');
expect(onErrorArgs[1]).to.eql(data);
}));
});

0 comments on commit 297cd92

Please sign in to comment.