@@ -9,13 +9,24 @@ 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 {
16
+ BadRequestException ,
17
+ HttpException ,
18
+ UnprocessableEntityException ,
19
+ } from '../../exceptions' ;
15
20
import { ArgumentMetadata } from '../../interfaces' ;
16
21
import { ValidationPipe } from '../../pipes/validation.pipe' ;
17
22
chai . use ( chaiAsPromised ) ;
18
23
24
+ class CustomTestError extends HttpException {
25
+ constructor ( errors : any ) {
26
+ super ( errors , 418 ) ;
27
+ }
28
+ }
29
+
19
30
@Exclude ( )
20
31
class TestModelInternal {
21
32
constructor ( ) { }
@@ -585,4 +596,191 @@ describe('ValidationPipe', () => {
585
596
expect ( await target . transform ( testObj , m ) ) . to . deep . equal ( testObj ) ;
586
597
} ) ;
587
598
} ) ;
599
+
600
+ describe ( 'option: "exceptionFactory"' , ( ) => {
601
+ describe ( 'when validation fails' , ( ) => {
602
+ beforeEach ( ( ) => {
603
+ target = new ValidationPipe ( {
604
+ exceptionFactory : errors => new CustomTestError ( errors ) ,
605
+ } ) ;
606
+ } ) ;
607
+ it ( 'should throw a CustomTestError exception' , async ( ) => {
608
+ const testObj = { prop1 : 'value1' } ;
609
+ try {
610
+ await target . transform ( testObj , metadata ) ;
611
+ } catch ( err ) {
612
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
613
+ }
614
+ } ) ;
615
+ } ) ;
616
+ } ) ;
617
+
618
+ describe ( 'option: "disableFlattenErrorMessages"' , ( ) => {
619
+ describe ( 'when disableFlattenErrorMessages is true' , ( ) => {
620
+ beforeEach ( ( ) => {
621
+ target = new ValidationPipe ( {
622
+ disableFlattenErrorMessages : true ,
623
+ } ) ;
624
+ } ) ;
625
+ it ( 'should throw an exception without flatten errors' , async ( ) => {
626
+ const testObj = { prop1 : 'value1' } ;
627
+ try {
628
+ await target . transform ( testObj , metadata ) ;
629
+ } catch ( err ) {
630
+ const message = err . getResponse ( ) . message
631
+ expect ( err ) . to . be . instanceOf ( BadRequestException ) ;
632
+ expect ( message ) . to . be . an ( 'array' ) ;
633
+ message . forEach ( ( error : any ) => {
634
+ expect ( error ) . to . be . instanceOf ( ValidationError ) ;
635
+ } ) ;
636
+ }
637
+ } ) ;
638
+ } ) ;
639
+
640
+ describe ( 'when disableFlattenErrorMessages is false' , ( ) => {
641
+ beforeEach ( ( ) => {
642
+ target = new ValidationPipe ( {
643
+ disableFlattenErrorMessages : false ,
644
+ } ) ;
645
+ } ) ;
646
+ it ( 'should throw an exception with flatten errors' , async ( ) => {
647
+ const testObj = { prop1 : 'value1' } ;
648
+ try {
649
+ await target . transform ( testObj , metadata ) ;
650
+ } catch ( err ) {
651
+ const message = err . getResponse ( ) . message
652
+ expect ( err ) . to . be . instanceOf ( BadRequestException ) ;
653
+ expect ( message ) . to . be . an ( 'array' ) ;
654
+ message . forEach ( ( error : any ) => {
655
+ expect ( error ) . to . be . a ( 'string' ) ;
656
+ } ) ;
657
+ }
658
+ } ) ;
659
+ } ) ;
660
+
661
+ describe ( 'when disableFlattenErrorMessages is not set' , ( ) => {
662
+ beforeEach ( ( ) => {
663
+ target = new ValidationPipe ( { } ) ;
664
+ } ) ;
665
+ it ( 'should throw an exception with flatten errors' , async ( ) => {
666
+ const testObj = { prop1 : 'value1' } ;
667
+ try {
668
+ await target . transform ( testObj , metadata ) ;
669
+ } catch ( err ) {
670
+ const message = err . getResponse ( ) . message
671
+ expect ( err ) . to . be . instanceOf ( BadRequestException ) ;
672
+ expect ( message ) . to . be . an ( 'array' ) ;
673
+ message . forEach ( ( error : any ) => {
674
+ expect ( error ) . to . be . a ( 'string' ) ;
675
+ } ) ;
676
+ }
677
+ } ) ;
678
+ } ) ;
679
+ } ) ;
680
+
681
+ describe ( 'option: "flatExceptionFactoryMessage"' , ( ) => {
682
+ describe ( 'when flatExceptionFactoryMessage is true' , ( ) => {
683
+ beforeEach ( ( ) => {
684
+ target = new ValidationPipe ( {
685
+ flatExceptionFactoryMessage : true ,
686
+ exceptionFactory : errors => new CustomTestError ( errors ) ,
687
+ } ) ;
688
+ } ) ;
689
+ it ( 'should throw a CustomTestError with flatten errors' , async ( ) => {
690
+ const testObj = { prop1 : 'value1' } ;
691
+ try {
692
+ await target . transform ( testObj , metadata ) ;
693
+ } catch ( err ) {
694
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
695
+ expect ( err . getResponse ( ) ) . to . be . an ( 'array' ) ;
696
+ err . getResponse ( ) . forEach ( ( error : any ) => {
697
+ expect ( error ) . to . be . a ( 'string' ) ;
698
+ } ) ;
699
+ }
700
+ } ) ;
701
+ } ) ;
702
+
703
+ describe ( 'when flatExceptionFactoryMessage is false' , ( ) => {
704
+ beforeEach ( ( ) => {
705
+ target = new ValidationPipe ( {
706
+ flatExceptionFactoryMessage : false ,
707
+ exceptionFactory : errors => new CustomTestError ( errors ) ,
708
+ } ) ;
709
+ } ) ;
710
+ it ( 'should throw a CustomTestError without flatten errors' , async ( ) => {
711
+ const testObj = { prop1 : 'value1' } ;
712
+ try {
713
+ await target . transform ( testObj , metadata ) ;
714
+ } catch ( err ) {
715
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
716
+ expect ( err . getResponse ( ) ) . to . be . an ( 'array' ) ;
717
+ err . getResponse ( ) . forEach ( ( error : any ) => {
718
+ expect ( error ) . to . be . instanceOf ( ValidationError ) ;
719
+ } ) ;
720
+ }
721
+ } ) ;
722
+ } ) ;
723
+
724
+ describe ( 'when flatExceptionFactoryMessage is not set' , ( ) => {
725
+ beforeEach ( ( ) => {
726
+ target = new ValidationPipe ( {
727
+ exceptionFactory : errors => new CustomTestError ( errors ) ,
728
+ } ) ;
729
+ } ) ;
730
+ it ( 'should throw a CustomTestError without flatten errors' , async ( ) => {
731
+ const testObj = { prop1 : 'value1' } ;
732
+ try {
733
+ await target . transform ( testObj , metadata ) ;
734
+ } catch ( err ) {
735
+ expect ( err ) . to . be . instanceOf ( CustomTestError ) ;
736
+ expect ( err . getResponse ( ) ) . to . be . an ( 'array' ) ;
737
+ err . getResponse ( ) . forEach ( ( error : any ) => {
738
+ expect ( error ) . to . be . instanceOf ( ValidationError ) ;
739
+ } ) ;
740
+ }
741
+ } ) ;
742
+ } ) ;
743
+
744
+ describe ( 'when flatExceptionFactoryMessage is false without exceptionFactory' , ( ) => {
745
+ beforeEach ( ( ) => {
746
+ target = new ValidationPipe ( {
747
+ flatExceptionFactoryMessage : false ,
748
+ } ) ;
749
+ } ) ;
750
+ it ( 'should throw an exception with flatten errors' , async ( ) => {
751
+ const testObj = { prop1 : 'value1' } ;
752
+ try {
753
+ await target . transform ( testObj , metadata ) ;
754
+ } catch ( err ) {
755
+ const message = err . getResponse ( ) . message
756
+ expect ( err ) . to . be . instanceOf ( BadRequestException ) ;
757
+ expect ( message ) . to . be . an ( 'array' ) ;
758
+ message . forEach ( ( error : any ) => {
759
+ expect ( error ) . to . be . a ( 'string' ) ;
760
+ } ) ;
761
+ }
762
+ } ) ;
763
+ } ) ;
764
+
765
+ describe ( 'when flatExceptionFactoryMessage is true without exceptionFactory' , ( ) => {
766
+ beforeEach ( ( ) => {
767
+ target = new ValidationPipe ( {
768
+ flatExceptionFactoryMessage : true ,
769
+ } ) ;
770
+ } ) ;
771
+ it ( 'should throw an exception with flatten errors' , async ( ) => {
772
+ const testObj = { prop1 : 'value1' } ;
773
+ try {
774
+ await target . transform ( testObj , metadata ) ;
775
+ } catch ( err ) {
776
+ const message = err . getResponse ( ) . message
777
+ expect ( err ) . to . be . instanceOf ( BadRequestException ) ;
778
+ expect ( message ) . to . be . an ( 'array' ) ;
779
+ message . forEach ( ( error : any ) => {
780
+ expect ( error ) . to . be . a ( 'string' ) ;
781
+ } ) ;
782
+ }
783
+ } ) ;
784
+ } ) ;
785
+ } ) ;
588
786
} ) ;
0 commit comments