Skip to content

Commit e493be7

Browse files
authored
fix(fastify): prevent error when schema members are undefined (#80)
related to fastify/fastify#4634
1 parent 0cac0c1 commit e493be7

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

packages/bautajs-fastify/src/expose-routes.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,27 @@ async function exposeRoutes(
123123
const method: fastify.HTTPMethods = (operation.route?.method.toUpperCase() ||
124124
'GET') as fastify.HTTPMethods;
125125
const { url = '' } = operation.route || {};
126-
const requestSchema =
127-
operation.route?.schema && operation.requestValidationEnabled
128-
? {
129-
body: operation.route?.schema.body,
130-
params: operation.route?.schema.params,
131-
headers: operation.route?.schema.headers,
132-
querystring: operation.route?.schema.querystring
133-
}
134-
: {};
126+
const requestSchema: any = {};
127+
// Avoid defining the request schema member as undefined
128+
// to prevent https://github.com/fastify/fastify/issues/4634
129+
if (operation.route?.schema && operation.requestValidationEnabled) {
130+
if (operation.route?.schema.body) {
131+
requestSchema.body = operation.route?.schema.body;
132+
}
133+
134+
if (operation.route?.schema.params) {
135+
requestSchema.params = operation.route?.schema.params;
136+
}
137+
138+
if (operation.route?.schema.headers) {
139+
requestSchema.headers = operation.route?.schema.headers;
140+
}
141+
142+
if (operation.route?.schema.querystring) {
143+
requestSchema.querystring = operation.route?.schema.querystring;
144+
}
145+
}
146+
135147
const responseSchema = operation.route?.schema
136148
? { response: operation.route?.schema.response }
137149
: {};

0 commit comments

Comments
 (0)