Skip to content

Commit 71149b4

Browse files
committed
feat: add cast array to expected array query params
1 parent f8e00f3 commit 71149b4

File tree

2 files changed

+82
-9
lines changed

2 files changed

+82
-9
lines changed

src/ActionParameterHandler.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ export class ActionParameterHandler<T extends BaseDriver> {
188188
*/
189189
protected parseValue(value: any, paramMetadata: ParamMetadata): any {
190190
if (typeof value === 'string') {
191-
try {
192-
return JSON.parse(value);
193-
} catch (error) {
194-
throw new ParameterParseJsonError(paramMetadata.name, value);
191+
if (paramMetadata.type === 'queries' && paramMetadata.targetName === 'array') {
192+
return [value];
193+
} else {
194+
try {
195+
return JSON.parse(value);
196+
} catch (error) {
197+
throw new ParameterParseJsonError(paramMetadata.name, value);
198+
}
195199
}
196200
}
197201
return value;

test/functional/action-params.spec.ts

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import bodyParser from 'body-parser';
2-
import { IsBoolean, IsString, MaxLength, Min, ValidateNested } from 'class-validator';
2+
import { IsBoolean, IsString, MaxLength, Min, ValidateNested, IsArray, IsNumber, IsDate } from 'class-validator';
33
import express from 'express';
44
import FormData from 'form-data';
55
import fs from 'fs';
@@ -29,6 +29,7 @@ import { createExpressServer, getMetadataArgsStorage } from '../../src/index';
2929
import { SessionMiddleware } from '../fakes/global-options/SessionMiddleware';
3030
import { axios } from '../utilities/axios';
3131
import DoneCallback = jest.DoneCallback;
32+
import { Type } from 'class-transformer';
3233

3334
describe(``, () => {
3435
let expressServer: HttpServer;
@@ -116,6 +117,20 @@ describe(``, () => {
116117

117118
@ValidateNested()
118119
myObject: NestedQueryClass;
120+
121+
@IsArray()
122+
@IsString({ each: true })
123+
multipleStringValues?: string[];
124+
125+
@IsArray()
126+
@IsNumber(undefined, { each: true })
127+
@Type(() => Number)
128+
multipleNumberValues?: number[];
129+
130+
@IsArray()
131+
@IsDate({ each: true })
132+
@Type(() => Date)
133+
multipleDateValues?: Date[];
119134
}
120135

121136
@Controller()
@@ -493,20 +508,71 @@ describe(``, () => {
493508
*/
494509

495510
it("@QueryParams should give a proper values from request's query parameters", async () => {
496-
expect.assertions(6);
497-
const response = await axios.get('/photos-params?sortBy=name&count=2&limit=10&showAll');
511+
expect.assertions(9);
512+
const response = await axios.get(
513+
'/photos-params?' +
514+
'sortBy=name&' +
515+
'count=2&' +
516+
'limit=10&' +
517+
'showAll&' +
518+
'multipleStringValues=a&' +
519+
'multipleStringValues=b&' +
520+
'multipleNumberValues=1&' +
521+
'multipleNumberValues=2.3&' +
522+
'multipleDateValues=2017-02-01T00:00:00Z&' +
523+
'multipleDateValues=2017-03-01T00:00:00Z'
524+
);
498525
expect(response.status).toEqual(HttpStatusCodes.OK);
499526
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
500527
expect(queryParams1.sortBy).toEqual('name');
501528
expect(queryParams1.count).toEqual('2');
502529
expect(queryParams1.limit).toEqual(10);
503530
expect(queryParams1.showAll).toEqual(true);
531+
expect(queryParams1.multipleStringValues).toEqual(['a', 'b']);
532+
expect(queryParams1.multipleNumberValues).toEqual([1, 2.3]);
533+
expect(queryParams1.multipleDateValues).toEqual([
534+
new Date('2017-02-01T00:00:00Z'),
535+
new Date('2017-03-01T00:00:00Z'),
536+
]);
504537
});
505538

506-
it("@QueryParams should give a proper values from request's query parameters with nested json", async () => {
539+
it("@QueryParams should give a proper values from request's query parameters and one multiple value", async () => {
507540
expect.assertions(9);
508541
const response = await axios.get(
509-
'/photos-params?sortBy=name&count=2&limit=10&showAll&myObject=%7B%22num%22%3A%205,%20%22str%22%3A%20%22five%22,%20%22isFive%22%3A%20true%7D'
542+
'/photos-params?' +
543+
'sortBy=name&' +
544+
'count=2&' +
545+
'limit=10&' +
546+
'showAll&' +
547+
'multipleStringValues=a&' +
548+
'multipleNumberValues=1&' +
549+
'multipleDateValues=2017-02-01T01:00:00Z'
550+
);
551+
expect(response.status).toEqual(HttpStatusCodes.OK);
552+
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
553+
expect(queryParams1.sortBy).toEqual('name');
554+
expect(queryParams1.count).toEqual('2');
555+
expect(queryParams1.limit).toEqual(10);
556+
expect(queryParams1.showAll).toEqual(true);
557+
expect(queryParams1.multipleStringValues).toEqual(['a']);
558+
expect(queryParams1.multipleNumberValues).toEqual([1]);
559+
expect(queryParams1.multipleDateValues).toEqual([new Date('2017-02-01T01:00:00Z')]);
560+
});
561+
562+
it("@QueryParams should give a proper values from request's query parameters with nested json", async () => {
563+
expect.assertions(12);
564+
const response = await axios.get(
565+
'/photos-params?' +
566+
'sortBy=name&' +
567+
'count=2&' +
568+
'limit=10&' +
569+
'showAll&' +
570+
'myObject=%7B%22num%22%3A%205,%20%22str%22%3A%20%22five%22,%20%22isFive%22%3A%20true%7D&' +
571+
'multipleStringValues=a&' +
572+
'multipleStringValues=b&' +
573+
'multipleNumberValues=1&' +
574+
'multipleNumberValues=2.3&' +
575+
'multipleDateValues=2017-02-01T00:00:00Z'
510576
);
511577
expect(response.status).toEqual(HttpStatusCodes.OK);
512578
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
@@ -517,6 +583,9 @@ describe(``, () => {
517583
expect(queryParams1.myObject.num).toEqual(5);
518584
expect(queryParams1.myObject.str).toEqual('five');
519585
expect(queryParams1.myObject.isFive).toEqual(true);
586+
expect(queryParams1.multipleStringValues).toEqual(['a', 'b']);
587+
expect(queryParams1.multipleNumberValues).toEqual([1, 2.3]);
588+
expect(queryParams1.multipleDateValues).toEqual([new Date('2017-02-01T00:00:00Z')]);
520589
});
521590

522591
it("@QueryParams should not validate request query parameters when it's turned off in validator options", async () => {

0 commit comments

Comments
 (0)