@@ -73,6 +73,7 @@ const COERCEABLE_TYPES = ['string', 'number', 'boolean', 'bigint', 'date'];
73
73
74
74
export const generateZodValidationSchemaDefinition = (
75
75
schema : SchemaObject | undefined ,
76
+ context : ContextSpecs ,
76
77
_required : boolean | undefined ,
77
78
name : string ,
78
79
strict : boolean ,
@@ -118,6 +119,7 @@ export const generateZodValidationSchemaDefinition = (
118
119
'array' ,
119
120
generateZodValidationSchemaDefinition (
120
121
items ,
122
+ context ,
121
123
true ,
122
124
camel ( `${ name } -item` ) ,
123
125
strict ,
@@ -129,13 +131,21 @@ export const generateZodValidationSchemaDefinition = (
129
131
break ;
130
132
}
131
133
132
- if ( schema . format === 'date' ) {
134
+ if (
135
+ context . output . override . useDates &&
136
+ ( schema . format === 'date' || schema . format === 'date-time' )
137
+ ) {
133
138
functions . push ( [ 'date' , undefined ] ) ;
134
139
break ;
135
140
}
136
141
137
142
functions . push ( [ type as string , undefined ] ) ;
138
143
144
+ if ( schema . format === 'date' ) {
145
+ functions . push ( [ 'date' , undefined ] ) ;
146
+ break ;
147
+ }
148
+
139
149
if ( schema . format === 'date-time' ) {
140
150
functions . push ( [ 'datetime' , undefined ] ) ;
141
151
break ;
@@ -177,6 +187,7 @@ export const generateZodValidationSchemaDefinition = (
177
187
schemas . map ( ( schema ) =>
178
188
generateZodValidationSchemaDefinition (
179
189
schema as SchemaObject ,
190
+ context ,
180
191
true ,
181
192
camel ( name ) ,
182
193
strict ,
@@ -193,6 +204,7 @@ export const generateZodValidationSchemaDefinition = (
193
204
. map ( ( key ) => ( {
194
205
[ key ] : generateZodValidationSchemaDefinition (
195
206
schema . properties ?. [ key ] as any ,
207
+ context ,
196
208
schema . required ?. includes ( key ) ,
197
209
camel ( `${ name } -${ key } ` ) ,
198
210
strict ,
@@ -215,6 +227,7 @@ export const generateZodValidationSchemaDefinition = (
215
227
? schema . additionalProperties
216
228
: generateZodValidationSchemaDefinition (
217
229
schema . additionalProperties as SchemaObject ,
230
+ context ,
218
231
true ,
219
232
name ,
220
233
strict ,
@@ -285,6 +298,7 @@ export type ZodValidationSchemaDefinitionInput = {
285
298
286
299
export const parseZodValidationSchemaDefinition = (
287
300
input : ZodValidationSchemaDefinitionInput ,
301
+ contex : ContextSpecs ,
288
302
coerceTypes : boolean | ZodCoerceType [ ] = false ,
289
303
preprocessResponse ?: GeneratorMutator ,
290
304
) : { zod : string ; consts : string } => {
@@ -368,11 +382,15 @@ ${Object.entries(args)
368
382
return '.strict()' ;
369
383
}
370
384
371
- if (
385
+ const shouldCoerceType =
372
386
coerceTypes &&
373
387
( Array . isArray ( coerceTypes )
374
388
? coerceTypes . includes ( fn as ZodCoerceType )
375
- : COERCEABLE_TYPES . includes ( fn ) )
389
+ : COERCEABLE_TYPES . includes ( fn ) ) ;
390
+
391
+ if (
392
+ ( fn !== 'date' && shouldCoerceType ) ||
393
+ ( fn === 'date' && shouldCoerceType && contex . output . override . useDates )
376
394
) {
377
395
return `.coerce.${ fn } (${ args } )` ;
378
396
}
@@ -474,6 +492,7 @@ const parseBodyAndResponse = ({
474
492
return {
475
493
input : generateZodValidationSchemaDefinition (
476
494
resolvedJsonSchema . items as SchemaObject ,
495
+ context ,
477
496
true ,
478
497
name ,
479
498
strict ,
@@ -485,6 +504,7 @@ const parseBodyAndResponse = ({
485
504
return {
486
505
input : generateZodValidationSchemaDefinition (
487
506
resolvedJsonSchema ,
507
+ context ,
488
508
true ,
489
509
name ,
490
510
strict ,
@@ -549,6 +569,7 @@ const parseParameters = ({
549
569
550
570
const definition = generateZodValidationSchemaDefinition (
551
571
schema ,
572
+ context ,
552
573
parameter . required ,
553
574
camel ( `${ operationName } -${ parameter . in } -${ parameter . name } ` ) ,
554
575
mapStrict [ parameter . in as 'path' | 'query' | 'header' ] ?? false ,
@@ -641,6 +662,8 @@ const generateZodRoute = async (
641
662
| PathItemObject
642
663
| undefined ;
643
664
665
+ override . useDates ;
666
+
644
667
const parameters = spec ?. [ verb ] ?. parameters ;
645
668
const requestBody = spec ?. [ verb ] ?. requestBody ;
646
669
const response = spec ?. [ verb ] ?. responses ?. [ '200' ] as
@@ -654,7 +677,7 @@ const generateZodRoute = async (
654
677
strict : override . zod . strict . response ,
655
678
} ) ;
656
679
657
- const parsedPody = parseBodyAndResponse ( {
680
+ const parsedBody = parseBodyAndResponse ( {
658
681
data : requestBody ,
659
682
context,
660
683
name : camel ( `${ operationName } -body` ) ,
@@ -670,6 +693,7 @@ const generateZodRoute = async (
670
693
671
694
const inputParams = parseZodValidationSchemaDefinition (
672
695
parsedParameters . params ,
696
+ context ,
673
697
override . zod . coerce . param ,
674
698
) ;
675
699
@@ -681,15 +705,18 @@ const generateZodRoute = async (
681
705
682
706
const inputQueryParams = parseZodValidationSchemaDefinition (
683
707
parsedParameters . queryParams ,
708
+ context ,
684
709
override . zod . coerce . query ?? override . coerceTypes ,
685
710
) ;
686
711
const inputHeaders = parseZodValidationSchemaDefinition (
687
712
parsedParameters . headers ,
713
+ context ,
688
714
override . zod . coerce . header ,
689
715
) ;
690
716
691
717
const inputBody = parseZodValidationSchemaDefinition (
692
- parsedPody . input ,
718
+ parsedBody . input ,
719
+ context ,
693
720
override . zod . coerce . body ,
694
721
) ;
695
722
@@ -705,6 +732,7 @@ const generateZodRoute = async (
705
732
706
733
const inputResponse = parseZodValidationSchemaDefinition (
707
734
parsedResponse . input ,
735
+ context ,
708
736
override . zod . coerce . response ,
709
737
preprocessResponse ,
710
738
) ;
@@ -739,7 +767,7 @@ const generateZodRoute = async (
739
767
...( inputBody . consts ? [ inputBody . consts ] : [ ] ) ,
740
768
...( inputBody . zod
741
769
? [
742
- parsedPody . isArray
770
+ parsedBody . isArray
743
771
? `export const ${ operationName } BodyItem = ${ inputBody . zod }
744
772
export const ${ operationName } Body = zod.array(${ operationName } BodyItem)`
745
773
: `export const ${ operationName } Body = ${ inputBody . zod } ` ,
0 commit comments