Skip to content

Commit d988e87

Browse files
committed
Fix validation of request with optional body
1 parent 2ef2a68 commit d988e87

File tree

2 files changed

+76
-60
lines changed

2 files changed

+76
-60
lines changed

src/compileOperation.ts

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -201,67 +201,66 @@ export function compileOperation(
201201

202202
// Validate the body against the schema
203203
if (operation.requestBody) {
204-
if (operation.requestBody.required) {
205-
nodes.push(
206-
builders.ifStatement(
207-
builders.binaryExpression(
208-
'===',
209-
builders.memberExpression(
210-
requestIdentifier,
211-
builders.identifier('body'),
212-
),
213-
builders.identifier('undefined'),
214-
),
215-
builders.blockStatement([
216-
builders.returnStatement(buildRequestError(400, 'body is required')),
217-
]),
218-
),
219-
);
220-
}
221-
222204
const contentTypeSchema = operation.requestBody.content?.['application/json']?.schema;
223-
if (contentTypeSchema) {
224-
const bodyFn = compileValueSchema(compiler, contentTypeSchema);
225-
const bodyResult = builders.identifier('body');
205+
const bodyFn = contentTypeSchema
206+
? compileValueSchema(compiler, contentTypeSchema)
207+
: null;
208+
const bodyResult = builders.identifier('body');
226209

227-
nodes.push(
228-
builders.variableDeclaration('const', [
229-
builders.variableDeclarator(
230-
bodyResult,
231-
builders.callExpression(bodyFn, [
232-
builders.arrayExpression([builders.literal('body')]),
233-
builders.memberExpression(
234-
requestIdentifier,
235-
builders.identifier('body'),
236-
),
237-
]),
238-
),
239-
]),
240-
);
241-
242-
nodes.push(
243-
builders.ifStatement(
244-
builders.binaryExpression(
245-
'instanceof',
246-
bodyResult,
247-
ValidationErrorIdentifier,
248-
),
249-
builders.blockStatement([builders.returnStatement(bodyResult)]),
250-
builders.blockStatement([
251-
builders.expressionStatement(
252-
builders.assignmentExpression(
253-
'=',
254-
builders.memberExpression(
255-
requestIdentifier,
256-
builders.identifier('body'),
257-
),
258-
bodyResult,
259-
),
260-
),
261-
]),
210+
nodes.push(
211+
builders.ifStatement(
212+
builders.binaryExpression(
213+
'===',
214+
builders.memberExpression(requestIdentifier, builders.identifier('body')),
215+
builders.identifier('undefined'),
262216
),
263-
);
264-
}
217+
builders.blockStatement(
218+
operation.requestBody.required
219+
? [builders.returnStatement(buildRequestError(400, 'body is required'))]
220+
: [],
221+
),
222+
builders.blockStatement(
223+
bodyFn
224+
? [
225+
builders.variableDeclaration('const', [
226+
builders.variableDeclarator(
227+
bodyResult,
228+
builders.callExpression(bodyFn, [
229+
builders.arrayExpression([builders.literal('body')]),
230+
builders.memberExpression(
231+
requestIdentifier,
232+
builders.identifier('body'),
233+
),
234+
]),
235+
),
236+
]),
237+
builders.ifStatement(
238+
builders.binaryExpression(
239+
'instanceof',
240+
bodyResult,
241+
ValidationErrorIdentifier,
242+
),
243+
builders.blockStatement([
244+
builders.returnStatement(bodyResult),
245+
]),
246+
builders.blockStatement([
247+
builders.expressionStatement(
248+
builders.assignmentExpression(
249+
'=',
250+
builders.memberExpression(
251+
requestIdentifier,
252+
builders.identifier('body'),
253+
),
254+
bodyResult,
255+
),
256+
),
257+
]),
258+
),
259+
]
260+
: [],
261+
),
262+
),
263+
);
265264
} else {
266265
nodes.push(
267266
builders.ifStatement(

tests/gitbook.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ test('GET orgs/microsoft/collections?limit=invalid', () => {
5252
limit: 'invalid',
5353
},
5454
});
55-
expect(result instanceof ValidationError).toBeTruthy();
56-
expect(result.path).toEqual(['query', 'limit']);
55+
expect(result instanceof ValidationError ? result.path : null).toEqual(['query', 'limit']);
56+
});
57+
58+
test('POST orgs/appleId/custom-fields', () => {
59+
const result = validateRequest({
60+
path: '/orgs/appleId/spaces',
61+
method: 'post',
62+
headers: {
63+
'content-type': 'application/json',
64+
},
65+
query: {},
66+
body: undefined,
67+
});
68+
console.log(result);
69+
expect(result).toMatchObject({
70+
params: {
71+
organizationId: 'appleId',
72+
},
73+
});
5774
});

0 commit comments

Comments
 (0)