@@ -187,40 +187,30 @@ function openApiSchemaToZod(schema: Record<string, unknown>): z.ZodTypeAny {
187187 shape [ propName ] = openApiSchemaToZod ( propSchema as Record < string , unknown > ) ;
188188 }
189189
190- // Make optional properties optional
191- if ( schema . required && Array . isArray ( schema . required ) ) {
192- const optionalShape : Record < string , z . ZodTypeAny > = { } ;
193- for ( const [ propName , zodSchema ] of Object . entries ( shape ) ) {
194- if ( schema . required . includes ( propName ) ) {
195- optionalShape [ propName ] = zodSchema ;
196- } else {
197- optionalShape [ propName ] = zodSchema . optional ( ) ;
198- }
199- }
200- let objectSchema = z . object ( optionalShape ) ;
201-
202- if ( schema . additionalProperties === true ) {
203- return schema . nullable
204- ? objectSchema . passthrough ( ) . nullable ( )
205- : objectSchema . passthrough ( ) ;
206- } else if ( schema . additionalProperties === false ) {
207- return schema . nullable ? objectSchema . strict ( ) . nullable ( ) : objectSchema . strict ( ) ;
190+ // Make optional properties optional based on required array
191+ const optionalShape : Record < string , z . ZodTypeAny > = { } ;
192+ const requiredFields =
193+ schema . required && Array . isArray ( schema . required ) ? schema . required : [ ] ;
194+
195+ for ( const [ propName , zodSchema ] of Object . entries ( shape ) ) {
196+ if ( requiredFields . includes ( propName ) ) {
197+ optionalShape [ propName ] = zodSchema ;
198+ } else {
199+ optionalShape [ propName ] = zodSchema . optional ( ) ;
208200 }
201+ }
209202
210- return schema . nullable ? objectSchema . nullable ( ) : objectSchema ;
211- } else {
212- let objectSchema = z . object ( shape ) ;
203+ let objectSchema = z . object ( optionalShape ) ;
213204
214- if ( schema . additionalProperties === true ) {
215- return schema . nullable
216- ? objectSchema . passthrough ( ) . nullable ( )
217- : objectSchema . passthrough ( ) ;
218- } else if ( schema . additionalProperties === false ) {
219- return schema . nullable ? objectSchema . strict ( ) . nullable ( ) : objectSchema . strict ( ) ;
220- }
221-
222- return schema . nullable ? objectSchema . nullable ( ) : objectSchema ;
205+ if ( schema . additionalProperties === true ) {
206+ return schema . nullable
207+ ? objectSchema . passthrough ( ) . nullable ( )
208+ : objectSchema . passthrough ( ) ;
209+ } else if ( schema . additionalProperties === false ) {
210+ return schema . nullable ? objectSchema . strict ( ) . nullable ( ) : objectSchema . strict ( ) ;
223211 }
212+
213+ return schema . nullable ? objectSchema . nullable ( ) : objectSchema ;
224214 }
225215 return schema . nullable ? z . record ( z . any ( ) ) . nullable ( ) : z . record ( z . any ( ) ) ;
226216
0 commit comments