1
1
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' ;
3
3
import express from 'express' ;
4
4
import FormData from 'form-data' ;
5
5
import fs from 'fs' ;
@@ -29,6 +29,7 @@ import { createExpressServer, getMetadataArgsStorage } from '../../src/index';
29
29
import { SessionMiddleware } from '../fakes/global-options/SessionMiddleware' ;
30
30
import { axios } from '../utilities/axios' ;
31
31
import DoneCallback = jest . DoneCallback ;
32
+ import { Type } from 'class-transformer' ;
32
33
33
34
describe ( `` , ( ) => {
34
35
let expressServer : HttpServer ;
@@ -116,6 +117,20 @@ describe(``, () => {
116
117
117
118
@ValidateNested ( )
118
119
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 [ ] ;
119
134
}
120
135
121
136
@Controller ( )
@@ -493,20 +508,71 @@ describe(``, () => {
493
508
*/
494
509
495
510
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
+ ) ;
498
525
expect ( response . status ) . toEqual ( HttpStatusCodes . OK ) ;
499
526
expect ( response . headers [ 'content-type' ] ) . toEqual ( 'text/html; charset=utf-8' ) ;
500
527
expect ( queryParams1 . sortBy ) . toEqual ( 'name' ) ;
501
528
expect ( queryParams1 . count ) . toEqual ( '2' ) ;
502
529
expect ( queryParams1 . limit ) . toEqual ( 10 ) ;
503
530
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
+ ] ) ;
504
537
} ) ;
505
538
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 ( ) => {
507
540
expect . assertions ( 9 ) ;
508
541
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'
510
576
) ;
511
577
expect ( response . status ) . toEqual ( HttpStatusCodes . OK ) ;
512
578
expect ( response . headers [ 'content-type' ] ) . toEqual ( 'text/html; charset=utf-8' ) ;
@@ -517,6 +583,9 @@ describe(``, () => {
517
583
expect ( queryParams1 . myObject . num ) . toEqual ( 5 ) ;
518
584
expect ( queryParams1 . myObject . str ) . toEqual ( 'five' ) ;
519
585
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' ) ] ) ;
520
589
} ) ;
521
590
522
591
it ( "@QueryParams should not validate request query parameters when it's turned off in validator options" , async ( ) => {
0 commit comments