Skip to content

Commit 2b930d0

Browse files
committed
Accept string value as array
1 parent 4bead5c commit 2b930d0

7 files changed

+23624
-19853
lines changed

bun.lockb

0 Bytes
Binary file not shown.

src/compileOperation.ts

+54-8
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,12 @@ export function compileOperation(
140140
const parameter = compiler.resolveMaybeRef(refParameter);
141141
const paramValueIdentifier = builders.identifier(`queryParam${index}`);
142142
const resultIdentifier = builders.identifier(`queryParamResult${index}`);
143+
143144
const schemaFn = compileValueSchema(compiler, parameter.schema);
144145

145146
// Assign the query parameter to a variable
146147
nodes.push(
147-
builders.variableDeclaration('const', [
148+
builders.variableDeclaration('let', [
148149
builders.variableDeclarator(
149150
paramValueIdentifier,
150151
builders.memberExpression(
@@ -180,7 +181,7 @@ export function compileOperation(
180181
),
181182
builders.blockStatement([
182183
// Validate the value
183-
builders.variableDeclaration('const', [
184+
builders.variableDeclaration('let', [
184185
builders.variableDeclarator(
185186
resultIdentifier,
186187
builders.callExpression(schemaFn, [
@@ -193,14 +194,59 @@ export function compileOperation(
193194
]),
194195
),
195196
]),
196-
// Fail if error
197+
// If the result is an error, and try with an array
197198
builders.ifStatement(
198-
builders.binaryExpression(
199-
'instanceof',
200-
resultIdentifier,
201-
ValidationErrorIdentifier,
199+
builders.logicalExpression(
200+
'&&',
201+
// "instanceof ValidationError"
202+
builders.binaryExpression(
203+
'instanceof',
204+
resultIdentifier,
205+
ValidationErrorIdentifier,
206+
),
207+
// typeof paramValue === 'string'
208+
builders.binaryExpression(
209+
'===',
210+
builders.unaryExpression('typeof', paramValueIdentifier),
211+
builders.literal('string'),
212+
),
202213
),
203-
builders.blockStatement([builders.returnStatement(resultIdentifier)]),
214+
builders.blockStatement([
215+
// paramValue = [paramValue]
216+
builders.expressionStatement(
217+
builders.assignmentExpression(
218+
'=',
219+
paramValueIdentifier,
220+
builders.arrayExpression([paramValueIdentifier]),
221+
),
222+
),
223+
// result = schemaFn(['query', parameter.name], asArray, context)
224+
builders.expressionStatement(
225+
builders.assignmentExpression(
226+
'=',
227+
resultIdentifier,
228+
builders.callExpression(schemaFn, [
229+
builders.arrayExpression([
230+
builders.literal('query'),
231+
builders.literal(parameter.name),
232+
]),
233+
paramValueIdentifier,
234+
contextIdentifier,
235+
]),
236+
),
237+
),
238+
// If result is an error, return it
239+
builders.ifStatement(
240+
builders.binaryExpression(
241+
'instanceof',
242+
resultIdentifier,
243+
ValidationErrorIdentifier,
244+
),
245+
builders.blockStatement([
246+
builders.returnStatement(resultIdentifier),
247+
]),
248+
),
249+
]),
204250
),
205251
// Otherwise assign the value
206252
builders.expressionStatement(

src/compileValidateRequest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { annotateWithJSDocComment } from './comments';
99

1010
const COMMENT = `
1111
Validate a request against the OpenAPI spec
12-
@param {{ method: string; path: string; body?: any; query: Record<string, string>; headers: Record<string, string>; }} request - Input request to validate
12+
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
1313
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | null } }} [context] - Context object to pass to validation functions
14-
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string>; body?: any; headers: Record<string, string>; }}
14+
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
1515
`;
1616

1717
/**

0 commit comments

Comments
 (0)