@@ -140,11 +140,12 @@ export function compileOperation(
140
140
const parameter = compiler . resolveMaybeRef ( refParameter ) ;
141
141
const paramValueIdentifier = builders . identifier ( `queryParam${ index } ` ) ;
142
142
const resultIdentifier = builders . identifier ( `queryParamResult${ index } ` ) ;
143
+
143
144
const schemaFn = compileValueSchema ( compiler , parameter . schema ) ;
144
145
145
146
// Assign the query parameter to a variable
146
147
nodes . push (
147
- builders . variableDeclaration ( 'const ' , [
148
+ builders . variableDeclaration ( 'let ' , [
148
149
builders . variableDeclarator (
149
150
paramValueIdentifier ,
150
151
builders . memberExpression (
@@ -180,7 +181,7 @@ export function compileOperation(
180
181
) ,
181
182
builders . blockStatement ( [
182
183
// Validate the value
183
- builders . variableDeclaration ( 'const ' , [
184
+ builders . variableDeclaration ( 'let ' , [
184
185
builders . variableDeclarator (
185
186
resultIdentifier ,
186
187
builders . callExpression ( schemaFn , [
@@ -193,14 +194,59 @@ export function compileOperation(
193
194
] ) ,
194
195
) ,
195
196
] ) ,
196
- // Fail if error
197
+ // If the result is an error, and try with an array
197
198
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
+ ) ,
202
213
) ,
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
+ ] ) ,
204
250
) ,
205
251
// Otherwise assign the value
206
252
builders . expressionStatement (
0 commit comments