@@ -9,13 +9,20 @@ import {
9
9
IsOptional ,
10
10
IsString ,
11
11
ValidateNested ,
12
+ ValidationError ,
12
13
} from 'class-validator' ;
13
14
import { HttpStatus } from '../../enums' ;
14
- import { UnprocessableEntityException } from '../../exceptions' ;
15
+ import { HttpException , UnprocessableEntityException } from '../../exceptions' ;
15
16
import { ArgumentMetadata } from '../../interfaces' ;
16
17
import { ValidationPipe } from '../../pipes/validation.pipe' ;
17
18
chai . use ( chaiAsPromised ) ;
18
19
20
+ class CustomTestError extends HttpException {
21
+ constructor ( errors : any ) {
22
+ super ( errors , 418 ) ;
23
+ }
24
+ }
25
+
19
26
@Exclude ( )
20
27
class TestModelInternal {
21
28
constructor ( ) { }
@@ -585,4 +592,87 @@ describe('ValidationPipe', () => {
585
592
expect ( await target . transform ( testObj , m ) ) . to . deep . equal ( testObj ) ;
586
593
} ) ;
587
594
} ) ;
595
+
596
+ describe ( 'option: "exceptionFactory"' , ( ) => {
597
+ describe ( 'when validation fails' , ( ) => {
598
+ beforeEach ( ( ) => {
599
+ target = new ValidationPipe ( {
600
+ exceptionFactory : ( errors ) => new CustomTestError ( errors ) ,
601
+ } ) ;
602
+ } ) ;
603
+ it ( 'should throw a CustomTestError exception' , async ( ) => {
604
+ const testObj = { prop1 : 'value1' } ;
605
+ try {
606
+ await target . transform ( testObj , metadata ) ;
607
+ } catch ( err ) {
608
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
609
+ }
610
+ } ) ;
611
+ } ) ;
612
+ } ) ;
613
+
614
+ describe ( 'option: "disableFlattenErrorMessages"' , ( ) => {
615
+ describe ( 'when disableFlattenErrorMessages is true' , ( ) => {
616
+ beforeEach ( ( ) => {
617
+ target = new ValidationPipe ( {
618
+ disableFlattenErrorMessages : true ,
619
+ exceptionFactory : ( errors ) => new CustomTestError ( errors ) ,
620
+ } ) ;
621
+ } ) ;
622
+ it ( 'should throw a CustomTestError without flatten errors' , async ( ) => {
623
+ const testObj = { prop1 : 'value1' } ;
624
+ try {
625
+ await target . transform ( testObj , metadata ) ;
626
+ } catch ( err ) {
627
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
628
+ expect ( err . getResponse ( ) ) . to . be . an ( 'array' )
629
+ err . getResponse ( ) . forEach ( ( error : any ) => {
630
+ expect ( error ) . to . be . instanceOf ( ValidationError ) ;
631
+ } ) ;
632
+ }
633
+ } ) ;
634
+ } ) ;
635
+
636
+ describe ( 'when disableFlattenErrorMessages is false' , ( ) => {
637
+ beforeEach ( ( ) => {
638
+ target = new ValidationPipe ( {
639
+ disableFlattenErrorMessages : false ,
640
+ exceptionFactory : ( errors ) => new CustomTestError ( errors ) ,
641
+ } ) ;
642
+ } ) ;
643
+ it ( 'should throw a CustomTestError with flatten errors' , async ( ) => {
644
+ const testObj = { prop1 : 'value1' } ;
645
+ try {
646
+ await target . transform ( testObj , metadata ) ;
647
+ } catch ( err ) {
648
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
649
+ expect ( err . getResponse ( ) ) . to . be . an ( 'array' )
650
+ err . getResponse ( ) . forEach ( ( error : any ) => {
651
+ expect ( error ) . to . be . a ( 'string' ) ;
652
+ } ) ;
653
+ }
654
+ } ) ;
655
+ } ) ;
656
+
657
+ describe ( 'when disableFlattenErrorMessages is not set' , ( ) => {
658
+ beforeEach ( ( ) => {
659
+ target = new ValidationPipe ( {
660
+ disableFlattenErrorMessages : false ,
661
+ exceptionFactory : ( errors ) => new CustomTestError ( errors ) ,
662
+ } ) ;
663
+ } ) ;
664
+ it ( 'should throw a CustomTestError with flatten errors' , async ( ) => {
665
+ const testObj = { prop1 : 'value1' } ;
666
+ try {
667
+ await target . transform ( testObj , metadata ) ;
668
+ } catch ( err ) {
669
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
670
+ expect ( err . getResponse ( ) ) . to . be . an ( 'array' )
671
+ err . getResponse ( ) . forEach ( ( error : any ) => {
672
+ expect ( error ) . to . be . a ( 'string' ) ;
673
+ } ) ;
674
+ }
675
+ } ) ;
676
+ } ) ;
677
+ } ) ;
588
678
} ) ;
0 commit comments