@@ -40,10 +40,12 @@ const zIdentifier = compiler.identifier({ text: 'z' });
40
40
const nameTransformer = ( name : string ) => `z-${ name } ` ;
41
41
42
42
const arrayTypeToZodSchema = ( {
43
+ config,
43
44
context,
44
45
result,
45
46
schema,
46
47
} : {
48
+ config : Omit < Config , 'name' > ;
47
49
context : IR . Context ;
48
50
result : Result ;
49
51
schema : SchemaWithType < 'array' > ;
@@ -73,6 +75,7 @@ const arrayTypeToZodSchema = ({
73
75
// at least one item is guaranteed
74
76
const itemExpressions = schema . items ! . map ( ( item ) =>
75
77
schemaToZodSchema ( {
78
+ config,
76
79
context,
77
80
result,
78
81
schema : item ,
@@ -331,10 +334,12 @@ const numberTypeToZodSchema = ({
331
334
} ;
332
335
333
336
const objectTypeToZodSchema = ( {
337
+ config,
334
338
context,
335
339
result,
336
340
schema,
337
341
} : {
342
+ config : Omit < Config , 'name' > ;
338
343
context : IR . Context ;
339
344
result : Result ;
340
345
schema : SchemaWithType < 'object' > ;
@@ -353,6 +358,7 @@ const objectTypeToZodSchema = ({
353
358
const isRequired = required . includes ( name ) ;
354
359
355
360
const propertyExpression = schemaToZodSchema ( {
361
+ config,
356
362
context,
357
363
optional : ! isRequired ,
358
364
result,
@@ -432,8 +438,10 @@ const objectTypeToZodSchema = ({
432
438
} ;
433
439
434
440
const stringTypeToZodSchema = ( {
441
+ config,
435
442
schema,
436
443
} : {
444
+ config : Omit < Config , 'name' > ;
437
445
context : IR . Context ;
438
446
schema : SchemaWithType < 'string' > ;
439
447
} ) => {
@@ -463,6 +471,11 @@ const stringTypeToZodSchema = ({
463
471
expression : stringExpression ,
464
472
name : compiler . identifier ( { text : 'datetime' } ) ,
465
473
} ) ,
474
+ parameters : [
475
+ config . dateTimeOptions
476
+ ? JSON . stringify ( config . dateTimeOptions )
477
+ : undefined ,
478
+ ] ,
466
479
} ) ;
467
480
break ;
468
481
case 'ipv4' :
@@ -642,17 +655,20 @@ const voidTypeToZodSchema = ({
642
655
} ;
643
656
644
657
const schemaTypeToZodSchema = ( {
658
+ config,
645
659
context,
646
660
result,
647
661
schema,
648
662
} : {
663
+ config : Omit < Config , 'name' > ;
649
664
context : IR . Context ;
650
665
result : Result ;
651
666
schema : IR . SchemaObject ;
652
667
} ) : ts . Expression => {
653
668
switch ( schema . type as Required < IR . SchemaObject > [ 'type' ] ) {
654
669
case 'array' :
655
670
return arrayTypeToZodSchema ( {
671
+ config,
656
672
context,
657
673
result,
658
674
schema : schema as SchemaWithType < 'array' > ,
@@ -685,12 +701,14 @@ const schemaTypeToZodSchema = ({
685
701
} ) ;
686
702
case 'object' :
687
703
return objectTypeToZodSchema ( {
704
+ config,
688
705
context,
689
706
result,
690
707
schema : schema as SchemaWithType < 'object' > ,
691
708
} ) ;
692
709
case 'string' :
693
710
return stringTypeToZodSchema ( {
711
+ config,
694
712
context,
695
713
schema : schema as SchemaWithType < 'string' > ,
696
714
} ) ;
@@ -718,10 +736,12 @@ const schemaTypeToZodSchema = ({
718
736
} ;
719
737
720
738
const operationToZodSchema = ( {
739
+ config,
721
740
context,
722
741
operation,
723
742
result,
724
743
} : {
744
+ config : Omit < Config , 'name' > ;
725
745
context : IR . Context ;
726
746
operation : IR . OperationObject ;
727
747
result : Result ;
@@ -736,6 +756,7 @@ const operationToZodSchema = ({
736
756
id : operation . id ,
737
757
type : 'response' ,
738
758
} ) ,
759
+ config,
739
760
context,
740
761
result,
741
762
schema : response ,
@@ -746,6 +767,7 @@ const operationToZodSchema = ({
746
767
747
768
const schemaToZodSchema = ( {
748
769
$ref,
770
+ config,
749
771
context,
750
772
optional,
751
773
result,
@@ -755,6 +777,7 @@ const schemaToZodSchema = ({
755
777
* When $ref is supplied, a node will be emitted to the file.
756
778
*/
757
779
$ref ?: string ;
780
+ config : Omit < Config , 'name' > ;
758
781
context : IR . Context ;
759
782
/**
760
783
* Accept `optional` to handle optional object properties. We can't handle
@@ -798,6 +821,7 @@ const schemaToZodSchema = ({
798
821
if ( ! identifierRef . name ) {
799
822
const ref = context . resolveIrRef < IR . SchemaObject > ( schema . $ref ) ;
800
823
expression = schemaToZodSchema ( {
824
+ config,
801
825
context,
802
826
result,
803
827
schema : ref ,
@@ -836,6 +860,7 @@ const schemaToZodSchema = ({
836
860
}
837
861
} else if ( schema . type ) {
838
862
expression = schemaTypeToZodSchema ( {
863
+ config,
839
864
context,
840
865
result,
841
866
schema,
@@ -846,6 +871,7 @@ const schemaToZodSchema = ({
846
871
if ( schema . items ) {
847
872
const itemTypes = schema . items . map ( ( item ) =>
848
873
schemaToZodSchema ( {
874
+ config,
849
875
context,
850
876
result,
851
877
schema : item ,
@@ -895,6 +921,7 @@ const schemaToZodSchema = ({
895
921
}
896
922
} else {
897
923
expression = schemaToZodSchema ( {
924
+ config,
898
925
context,
899
926
result,
900
927
schema,
@@ -903,6 +930,7 @@ const schemaToZodSchema = ({
903
930
} else {
904
931
// catch-all fallback for failed schemas
905
932
expression = schemaTypeToZodSchema ( {
933
+ config,
906
934
context,
907
935
result,
908
936
schema : {
@@ -982,13 +1010,18 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
982
1010
name : 'z' ,
983
1011
} ) ;
984
1012
1013
+ const config : Omit < Config , 'name' > = {
1014
+ dateTimeOptions : plugin . dateTimeOptions ,
1015
+ } ;
1016
+
985
1017
context . subscribe ( 'operation' , ( { operation } ) => {
986
1018
const result : Result = {
987
1019
circularReferenceTracker : new Set ( ) ,
988
1020
hasCircularReference : false ,
989
1021
} ;
990
1022
991
1023
operationToZodSchema ( {
1024
+ config,
992
1025
context,
993
1026
operation,
994
1027
result,
@@ -1003,6 +1036,7 @@ export const handler: Plugin.Handler<Config> = ({ context, plugin }) => {
1003
1036
1004
1037
schemaToZodSchema ( {
1005
1038
$ref,
1039
+ config,
1006
1040
context,
1007
1041
result,
1008
1042
schema,
0 commit comments