File tree Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Expand file tree Collapse file tree 1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change @@ -15,27 +15,28 @@ export { createUniqueItemsValidator, isValidWithSchema } from "./core/utils";
1515 */
1616export function jsonSchemaObjectToZodRawShape ( schema : JSONSchema . Schema ) : z . ZodRawShape {
1717 const raw : Record < string , z . ZodType > = { } ;
18-
18+
1919 // Get the required fields set for efficient lookup
20- const requiredFields = new Set ( schema . required ?? [ ] ) ;
21-
20+ const requiredArray = Array . isArray ( schema . required ) ? schema . required : [ ] ;
21+ const requiredFields = new Set < string > ( requiredArray ) ;
22+
2223 for ( const [ key , value ] of Object . entries ( schema . properties ?? { } ) ) {
2324 if ( value === undefined ) continue ;
24-
25+
2526 let zodType = convertJsonSchemaToZod ( value ) ;
26-
27+
2728 // If there's a required array and the field is not in it, make it optional
2829 // If there's no required array, all fields are optional by default in JSON Schema
29- if ( schema . required !== undefined ) {
30+ if ( requiredArray . length > 0 ) {
3031 if ( ! requiredFields . has ( key ) ) {
3132 zodType = zodType . optional ( ) ;
3233 }
3334 } else {
3435 // No required array means all fields are optional
3536 zodType = zodType . optional ( ) ;
3637 }
37-
38+
3839 raw [ key ] = zodType ;
3940 }
4041 return raw ;
41- }
42+ }
You can’t perform that action at this time.
0 commit comments