Skip to content

fix: check if validation schema is undefined #4620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,26 @@ function compileSchemasForValidation (context, compile, isCustom) {
})
}
context[headersSchema] = compile({ schema: headersSchemaLowerCase, method, url, httpPart: 'headers' })
} else if (Object.hasOwnProperty.call(schema, 'headers')) {
throw new Error('headers schema is undefined')
}

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

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

if (schema.params) {
context[paramsSchema] = compile({ schema: schema.params, method, url, httpPart: 'params' })
} else if (Object.hasOwnProperty.call(schema, 'params')) {
throw new Error('params schema is undefined')
}
}

Expand Down
85 changes: 85 additions & 0 deletions test/schema-feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,91 @@ test('Should not change the input schemas', t => {
})
})

test('Should throw if the schema body is undefined', t => {
t.plan(2)
const fastify = Fastify()

fastify.get('/:id', {
handler: echoParams,
schema: {
body: undefined
}
})

fastify.ready(err => {
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error body schema is undefined')
})
})

test('Should throw if the schema headers is undefined', t => {
t.plan(2)
const fastify = Fastify()

fastify.get('/:id', {
handler: echoParams,
schema: {
headers: undefined
}
})

fastify.ready(err => {
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error headers schema is undefined')
})
})

test('Should throw if the schema params is undefined', t => {
t.plan(2)
const fastify = Fastify()

fastify.get('/:id', {
handler: echoParams,
schema: {
params: undefined
}
})

fastify.ready(err => {
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error params schema is undefined')
})
})

test('Should throw if the schema query is undefined', t => {
t.plan(2)
const fastify = Fastify()

fastify.get('/:id', {
handler: echoParams,
schema: {
querystring: undefined
}
})

fastify.ready(err => {
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error querystring schema is undefined')
})
})

test('Should throw if the schema query is undefined', t => {
t.plan(2)
const fastify = Fastify()

fastify.get('/:id', {
handler: echoParams,
schema: {
querystring: undefined
}
})

fastify.ready(err => {
t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error querystring schema is undefined')
})
})

test('First level $ref', t => {
t.plan(2)
const fastify = Fastify()
Expand Down