Skip to content

Commit 13b1bef

Browse files
authored
Merge pull request #3 from GitbookIO/validate-value-as-string
Accept string value as array
2 parents 4bead5c + abc3ed6 commit 13b1bef

7 files changed

+23612
-19935
lines changed

bun.lockb

0 Bytes
Binary file not shown.

src/compileOperation.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ 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

146+
const isArrayType = 'type' in parameter.schema && parameter.schema.type === 'array';
147+
145148
// Assign the query parameter to a variable
146149
nodes.push(
147-
builders.variableDeclaration('const', [
150+
builders.variableDeclaration('let', [
148151
builders.variableDeclarator(
149152
paramValueIdentifier,
150153
builders.memberExpression(
@@ -179,6 +182,29 @@ export function compileOperation(
179182
: [],
180183
),
181184
builders.blockStatement([
185+
// If expected type is array, convert to array if it's a string
186+
...(isArrayType
187+
? [
188+
builders.ifStatement(
189+
// typeof paramValue === 'string'
190+
builders.binaryExpression(
191+
'===',
192+
builders.unaryExpression('typeof', paramValueIdentifier),
193+
builders.literal('string'),
194+
),
195+
builders.blockStatement([
196+
// paramValue = [paramValue]
197+
builders.expressionStatement(
198+
builders.assignmentExpression(
199+
'=',
200+
paramValueIdentifier,
201+
builders.arrayExpression([paramValueIdentifier]),
202+
),
203+
),
204+
]),
205+
),
206+
]
207+
: []),
182208
// Validate the value
183209
builders.variableDeclaration('const', [
184210
builders.variableDeclarator(

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)