Skip to content

Commit 048c19c

Browse files
authored
Merge pull request #545 from trojs/feature/optional-response-validation
Adds option to disable response validation
2 parents 0376a4a + 65916aa commit 048c19c

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

src/api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { setupRouter } from './router.js'
2828
* @property {Handler=} unauthorizedHandler
2929
* @property {boolean=} swagger
3030
* @property {boolean=} apiDocs
31+
* @property {boolean=} validateResponse
3132
* @property {AjvOpts=} ajvOptions
3233
* @property {AjvCustomizer=} customizeAjv
3334
* @property {any[]=} middleware
@@ -57,6 +58,7 @@ export class Api {
5758
unauthorizedHandler,
5859
swagger,
5960
apiDocs,
61+
validateResponse,
6062
ajvOptions,
6163
customizeAjv,
6264
middleware = []
@@ -73,6 +75,7 @@ export class Api {
7375
this.unauthorizedHandler = unauthorizedHandler || undefined
7476
this.swagger = swagger ?? true
7577
this.apiDocs = apiDocs ?? true
78+
this.validateResponse = validateResponse ?? true
7679
this.ajvOptions = ajvOptions ?? { allErrors: false }
7780
this.customizeAjv = customizeAjv
7881
this.middleware = middleware
@@ -120,6 +123,7 @@ export class Api {
120123
meta: this.meta,
121124
securityHandlers: this.securityHandlers,
122125
unauthorizedHandler: this.unauthorizedHandler,
126+
validateResponse: this.validateResponse,
123127
ajvOptions: this.ajvOptions,
124128
customizeAjv: this.customizeAjv
125129
})

src/router.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { unauthorized } from './handlers/unauthorized.js'
2727
* @param {object=} params.meta
2828
* @param {SecurityHandler[]=} params.securityHandlers
2929
* @param {Handler=} params.unauthorizedHandler
30+
* @param {boolean=} params.validateResponse
3031
* @param {AjvOpts=} params.ajvOptions
3132
* @param {AjvCustomizer=} params.customizeAjv
3233
* @param {boolean=} params.mock
@@ -42,6 +43,7 @@ export const setupRouter = ({
4243
meta,
4344
securityHandlers = [],
4445
unauthorizedHandler,
46+
validateResponse = true,
4547
ajvOptions = {},
4648
customizeAjv,
4749
mock
@@ -58,12 +60,17 @@ export const setupRouter = ({
5860
customizeAjv: customizeAjv || ajvWithExtraFormats
5961
})
6062

61-
api.register({
63+
const handlers = {
6264
unauthorizedHandler: unauthorizedHandler || unauthorized,
6365
validationFail: requestValidation,
64-
notFound,
65-
postResponseHandler: makeResponseValidation(logger)
66-
})
66+
notFound
67+
}
68+
69+
if (validateResponse) {
70+
handlers.postResponseHandler = makeResponseValidation(logger)
71+
}
72+
73+
api.register(handlers)
6774

6875
operationIds({ specification: openAPISpecification }).forEach(
6976
(operationId) => {

src/router.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,68 @@ test('Test the router', async (t) => {
9090
)
9191
}
9292
)
93+
94+
await t.test(
95+
'It should not register postResponseHandler when validateResponse is false',
96+
async () => {
97+
const controllers = {
98+
getMessages: () => ({
99+
test: 'ok'
100+
})
101+
}
102+
103+
const { api } = setupRouter({
104+
secret: envExample.SECRET,
105+
openAPISpecification,
106+
controllers,
107+
validateResponse: false,
108+
logger
109+
})
110+
111+
assert.strictEqual(api.handlers.postResponseHandler, undefined)
112+
}
113+
)
114+
115+
await t.test(
116+
'It should register postResponseHandler when validateResponse is true',
117+
async () => {
118+
const controllers = {
119+
getMessages: () => ({
120+
test: 'ok'
121+
})
122+
}
123+
124+
const { api } = setupRouter({
125+
secret: envExample.SECRET,
126+
openAPISpecification,
127+
controllers,
128+
validateResponse: true,
129+
logger
130+
})
131+
132+
assert.notStrictEqual(api.handlers.postResponseHandler, undefined)
133+
assert.strictEqual(typeof api.handlers.postResponseHandler, 'function')
134+
}
135+
)
136+
137+
await t.test(
138+
'It should register postResponseHandler by default (when validateResponse is not specified)',
139+
async () => {
140+
const controllers = {
141+
getMessages: () => ({
142+
test: 'ok'
143+
})
144+
}
145+
146+
const { api } = setupRouter({
147+
secret: envExample.SECRET,
148+
openAPISpecification,
149+
controllers,
150+
logger
151+
})
152+
153+
assert.notStrictEqual(api.handlers.postResponseHandler, undefined)
154+
assert.strictEqual(typeof api.handlers.postResponseHandler, 'function')
155+
}
156+
)
93157
})

0 commit comments

Comments
 (0)