-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change AJV allErrors default and support user setting (#955)
* Support setting allErrors for AJV validation AJV recommends setting option `allErrors` to `false` in production. pdate `createAjv()` to respect the user's setting. Avoid introducing a breaking change by defaulting to `true` when not defined by the user. Add tests: 1. Make sure `AjvOptions` sets the value appropriately based on whether the end user defined `allErrors` or not. 2. When validating requests, make sure the number of errors reported (when multiple occur) is 1 when `allErrors` is `false`. The `allErrors` configuration for OpenAPISchemaValidator is not changed by this commit since that validation is for trusted content. Fixes #954 * (Revisions) Support setting allErrors for AJV validation - Do not set allErrors by default **breaking change** * (Revisions) Support setting allErrors for AJV validation - Allow allErrors to be set on requests and responses independently
- Loading branch information
1 parent
826ba62
commit 392f1dd
Showing
17 changed files
with
336 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as path from 'path'; | ||
import * as request from 'supertest'; | ||
import { expect } from 'chai'; | ||
import { createApp } from './common/app'; | ||
|
||
describe('request body validation with and without allErrors', () => { | ||
let allErrorsApp; | ||
let notAllErrorsApp; | ||
|
||
const defineRoutes = (app) => { | ||
app.post(`${app.basePath}/persons`, (req, res) => { | ||
res.send({ success: true }); | ||
}); | ||
app.get(`${app.basePath}/persons`, (req, res) => { | ||
res.send({ bname: req.query.bname }); | ||
}); | ||
}; | ||
|
||
before(async () => { | ||
const apiSpec = path.join('test', 'resources', 'multiple-validations.yaml'); | ||
|
||
allErrorsApp = await createApp( | ||
{ | ||
apiSpec, | ||
formats: { | ||
'starts-with-b': (v) => /^b/i.test(v), | ||
}, | ||
validateRequests: { | ||
allErrors: true, | ||
}, | ||
validateResponses: { | ||
allErrors: true, | ||
}, | ||
}, | ||
3005, | ||
defineRoutes, | ||
true, | ||
); | ||
|
||
notAllErrorsApp = await createApp( | ||
{ | ||
apiSpec, | ||
formats: { | ||
'starts-with-b': (v) => /^b/i.test(v), | ||
}, | ||
validateResponses: true, | ||
}, | ||
3006, | ||
defineRoutes, | ||
true, | ||
); | ||
}); | ||
|
||
after(() => { | ||
allErrorsApp.server.close(); | ||
notAllErrorsApp.server.close(); | ||
}); | ||
|
||
it('should return 200 if short b-name is posted', async () => | ||
request(allErrorsApp) | ||
.post(`${allErrorsApp.basePath}/persons`) | ||
.set('content-type', 'application/json') | ||
.send({ bname: 'Bob' }) | ||
.expect(200)); | ||
|
||
it('should return 200 if short b-name is fetched', async () => | ||
request(allErrorsApp) | ||
.get(`${allErrorsApp.basePath}/persons?bname=Bob`) | ||
.expect(200)); | ||
|
||
it('should include all request validation errors when allErrors=true', async () => | ||
request(allErrorsApp) | ||
.post(`${allErrorsApp.basePath}/persons`) | ||
.set('content-type', 'application/json') | ||
.send({ bname: 'Maximillian' }) | ||
.expect(400) | ||
.then((res) => { | ||
expect(res.body.errors).to.have.lengthOf(2); | ||
})); | ||
|
||
it('should include only first request validation error when allErrors=false', async () => | ||
request(notAllErrorsApp) | ||
.post(`${notAllErrorsApp.basePath}/persons`) | ||
.set('content-type', 'application/json') | ||
.send({ bname: 'Maximillian' }) | ||
.expect(400) | ||
.then((res) => { | ||
expect(res.body.errors).to.have.lengthOf(1); | ||
})); | ||
|
||
it('should include all response validation errors when allErrors=true', async () => | ||
request(allErrorsApp) | ||
.get(`${allErrorsApp.basePath}/persons?bname=Maximillian`) | ||
.expect(500) | ||
.then((res) => { | ||
expect(res.body.errors).to.have.lengthOf(2); | ||
})); | ||
|
||
it('should include only first response validation error when allErrors=false', async () => | ||
request(notAllErrorsApp) | ||
.get(`${notAllErrorsApp.basePath}/persons?bname=Maximillian`) | ||
.expect(500) | ||
.then((res) => { | ||
expect(res.body.errors).to.have.lengthOf(1); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.