@@ -541,6 +541,25 @@ test("action with errors set via `returnValidationErrors` gives back an object w
541
541
542
542
// `throwValidationErrors` tests.
543
543
544
+ // test without `throwValidationErrors` set at the instance level, just set at the action level.
545
+ test ( "action with validation errors and `throwValidationErrors` option set to true at the action level throws" , async ( ) => {
546
+ const schema = z . object ( {
547
+ username : z . string ( ) . min ( 3 ) ,
548
+ password : z . string ( ) . min ( 3 ) ,
549
+ } ) ;
550
+
551
+ const action = dac . schema ( schema ) . action (
552
+ async ( ) => {
553
+ return {
554
+ ok : true ,
555
+ } ;
556
+ } ,
557
+ { throwValidationErrors : true }
558
+ ) ;
559
+
560
+ assert . rejects ( async ( ) => await action ( { username : "12" , password : "34" } ) ) ;
561
+ } ) ;
562
+
544
563
const tveac = createSafeActionClient ( {
545
564
validationAdapter : zodAdapter ( ) ,
546
565
throwValidationErrors : true ,
@@ -580,3 +599,78 @@ test("action with server validation errors and `throwValidationErrors` option se
580
599
581
600
assert . rejects ( async ( ) => await action ( { username : "1234" , password : "5678" } ) ) ;
582
601
} ) ;
602
+
603
+ test ( "action with validation errors and `throwValidationErrors` option set to true both in client and action throws" , async ( ) => {
604
+ const schema = z . object ( {
605
+ username : z . string ( ) . min ( 3 ) ,
606
+ password : z . string ( ) . min ( 3 ) ,
607
+ } ) ;
608
+
609
+ const action = tveac . schema ( schema ) . action (
610
+ async ( ) => {
611
+ return {
612
+ ok : true ,
613
+ } ;
614
+ } ,
615
+ { throwValidationErrors : true }
616
+ ) ;
617
+
618
+ assert . rejects ( async ( ) => await action ( { username : "12" , password : "34" } ) ) ;
619
+ } ) ;
620
+
621
+ test ( "action with validation errors and overridden `throwValidationErrors` set to false at the action level doesn't throw" , async ( ) => {
622
+ const schema = z . object ( {
623
+ user : z . object ( {
624
+ id : z . string ( ) . min ( 36 ) . uuid ( ) ,
625
+ } ) ,
626
+ store : z . object ( {
627
+ id : z . string ( ) . min ( 36 ) . uuid ( ) ,
628
+ product : z . object ( {
629
+ id : z . string ( ) . min ( 36 ) . uuid ( ) ,
630
+ } ) ,
631
+ } ) ,
632
+ } ) ;
633
+
634
+ const action = tveac . schema ( schema ) . action (
635
+ async ( ) => {
636
+ return {
637
+ ok : true ,
638
+ } ;
639
+ } ,
640
+ { throwValidationErrors : false }
641
+ ) ;
642
+
643
+ const actualResult = await action ( {
644
+ user : {
645
+ id : "invalid_uuid" ,
646
+ } ,
647
+ store : {
648
+ id : "invalid_uuid" ,
649
+ product : {
650
+ id : "invalid_uuid" ,
651
+ } ,
652
+ } ,
653
+ } ) ;
654
+
655
+ const expectedResult = {
656
+ validationErrors : {
657
+ user : {
658
+ id : {
659
+ _errors : [ "String must contain at least 36 character(s)" , "Invalid uuid" ] ,
660
+ } ,
661
+ } ,
662
+ store : {
663
+ id : {
664
+ _errors : [ "String must contain at least 36 character(s)" , "Invalid uuid" ] ,
665
+ } ,
666
+ product : {
667
+ id : {
668
+ _errors : [ "String must contain at least 36 character(s)" , "Invalid uuid" ] ,
669
+ } ,
670
+ } ,
671
+ } ,
672
+ } ,
673
+ } ;
674
+
675
+ assert . deepStrictEqual ( actualResult , expectedResult ) ;
676
+ } ) ;
0 commit comments