forked from cdimascio/express-openapi-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 cdimascio#954
- Loading branch information
1 parent
f20b1c9
commit 91d3fc0
Showing
6 changed files
with
136 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 }); | ||
}); | ||
}; | ||
|
||
before(async () => { | ||
const apiSpec = path.join('test', 'resources', 'multiple-validations.yaml'); | ||
|
||
allErrorsApp = await createApp( | ||
{ | ||
apiSpec, | ||
formats: { 'starts-with-b': (v) => /^b/i.test(v) }, | ||
// allErrors is set to true when undefined | ||
}, | ||
3005, | ||
defineRoutes, | ||
true, | ||
); | ||
|
||
notAllErrorsApp = await createApp( | ||
{ | ||
apiSpec, | ||
formats: { 'starts-with-b': (v) => /^b/i.test(v) }, | ||
allErrors: false, | ||
}, | ||
3006, | ||
defineRoutes, | ||
true, | ||
); | ||
}); | ||
|
||
after(() => { | ||
allErrorsApp.server.close(); | ||
notAllErrorsApp.server.close(); | ||
}); | ||
|
||
it('should return 200 if short b-name is provided', async () => | ||
request(allErrorsApp) | ||
.post(`${allErrorsApp.basePath}/persons`) | ||
.set('content-type', 'application/json') | ||
.send({ bname: 'Bob' }) | ||
.expect(200)); | ||
|
||
it('should include all 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.length).to.equal(2); | ||
})); | ||
|
||
it('should include only first 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.length).to.equal(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: Multiple validations for allErrors check | ||
version: 1.0.0 | ||
servers: | ||
- url: /v1 | ||
paths: | ||
/persons: | ||
post: | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/Person" | ||
responses: | ||
200: | ||
description: Success | ||
|
||
components: | ||
schemas: | ||
Person: | ||
required: | ||
- bname | ||
type: object | ||
properties: | ||
bname: | ||
type: string | ||
format: starts-with-b | ||
maxLength: 10 |