Skip to content

Commit 6b5957d

Browse files
authored
fix: check if validation schema is undefined (fastify#4620)
1 parent 71abc48 commit 6b5957d

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/validation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,26 @@ function compileSchemasForValidation (context, compile, isCustom) {
8080
})
8181
}
8282
context[headersSchema] = compile({ schema: headersSchemaLowerCase, method, url, httpPart: 'headers' })
83+
} else if (Object.hasOwnProperty.call(schema, 'headers')) {
84+
throw new Error('headers schema is undefined')
8385
}
8486

8587
if (schema.body) {
8688
context[bodySchema] = compile({ schema: schema.body, method, url, httpPart: 'body' })
89+
} else if (Object.hasOwnProperty.call(schema, 'body')) {
90+
throw new Error('body schema is undefined')
8791
}
8892

8993
if (schema.querystring) {
9094
context[querystringSchema] = compile({ schema: schema.querystring, method, url, httpPart: 'querystring' })
95+
} else if (Object.hasOwnProperty.call(schema, 'querystring')) {
96+
throw new Error('querystring schema is undefined')
9197
}
9298

9399
if (schema.params) {
94100
context[paramsSchema] = compile({ schema: schema.params, method, url, httpPart: 'params' })
101+
} else if (Object.hasOwnProperty.call(schema, 'params')) {
102+
throw new Error('params schema is undefined')
95103
}
96104
}
97105

test/schema-feature.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,91 @@ test('Should not change the input schemas', t => {
253253
})
254254
})
255255

256+
test('Should throw if the schema body is undefined', t => {
257+
t.plan(2)
258+
const fastify = Fastify()
259+
260+
fastify.get('/:id', {
261+
handler: echoParams,
262+
schema: {
263+
body: undefined
264+
}
265+
})
266+
267+
fastify.ready(err => {
268+
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
269+
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error body schema is undefined')
270+
})
271+
})
272+
273+
test('Should throw if the schema headers is undefined', t => {
274+
t.plan(2)
275+
const fastify = Fastify()
276+
277+
fastify.get('/:id', {
278+
handler: echoParams,
279+
schema: {
280+
headers: undefined
281+
}
282+
})
283+
284+
fastify.ready(err => {
285+
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
286+
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error headers schema is undefined')
287+
})
288+
})
289+
290+
test('Should throw if the schema params is undefined', t => {
291+
t.plan(2)
292+
const fastify = Fastify()
293+
294+
fastify.get('/:id', {
295+
handler: echoParams,
296+
schema: {
297+
params: undefined
298+
}
299+
})
300+
301+
fastify.ready(err => {
302+
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
303+
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error params schema is undefined')
304+
})
305+
})
306+
307+
test('Should throw if the schema query is undefined', t => {
308+
t.plan(2)
309+
const fastify = Fastify()
310+
311+
fastify.get('/:id', {
312+
handler: echoParams,
313+
schema: {
314+
querystring: undefined
315+
}
316+
})
317+
318+
fastify.ready(err => {
319+
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
320+
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error querystring schema is undefined')
321+
})
322+
})
323+
324+
test('Should throw if the schema query is undefined', t => {
325+
t.plan(2)
326+
const fastify = Fastify()
327+
328+
fastify.get('/:id', {
329+
handler: echoParams,
330+
schema: {
331+
querystring: undefined
332+
}
333+
})
334+
335+
fastify.ready(err => {
336+
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
337+
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error querystring schema is undefined')
338+
})
339+
})
340+
256341
test('First level $ref', t => {
257342
t.plan(2)
258343
const fastify = Fastify()

0 commit comments

Comments
 (0)