@@ -1022,10 +1022,14 @@ describe("jsonSchemaObjectToZodRawShape", () => {
10221022 expect ( rawShape ) . toHaveProperty ( "age" ) ;
10231023 expect ( rawShape ) . toHaveProperty ( "isActive" ) ;
10241024
1025- // Verify types are correct
1025+ // Verify types are correct - required fields are direct types
10261026 expect ( rawShape . name instanceof z . ZodString ) . toBe ( true ) ;
10271027 expect ( rawShape . age instanceof z . ZodNumber ) . toBe ( true ) ;
1028- expect ( rawShape . isActive instanceof z . ZodBoolean ) . toBe ( true ) ;
1028+
1029+ // isActive is not in required array, so it should be optional
1030+ expect ( rawShape . isActive instanceof z . ZodOptional ) . toBe ( true ) ;
1031+ // Check the inner type of the optional
1032+ expect ( ( rawShape . isActive as z . ZodOptional < any > ) . _def . innerType instanceof z . ZodBoolean ) . toBe ( true ) ;
10291033 } ) ;
10301034
10311035 it ( "should handle empty properties" , ( ) => {
@@ -1072,7 +1076,10 @@ describe("jsonSchemaObjectToZodRawShape", () => {
10721076 const rawShape = jsonSchemaObjectToZodRawShape ( jsonSchema ) ;
10731077
10741078 expect ( rawShape ) . toHaveProperty ( "user" ) ;
1075- expect ( rawShape . user instanceof z . ZodObject ) . toBe ( true ) ;
1079+ // Since there's no required array at the top level, user field is optional
1080+ expect ( rawShape . user instanceof z . ZodOptional ) . toBe ( true ) ;
1081+ // The inner type should be a ZodObject
1082+ expect ( ( rawShape . user as z . ZodOptional < any > ) . _def . innerType instanceof z . ZodObject ) . toBe ( true ) ;
10761083
10771084 // Create a schema with the raw shape to test validation
10781085 const schema = z . object ( rawShape ) ;
@@ -1090,6 +1097,11 @@ describe("jsonSchemaObjectToZodRawShape", () => {
10901097 user :
{ email :
"[email protected] " } , 10911098 } ) ,
10921099 ) . toThrow ( ) ;
1100+
1101+ // Since user is optional at the top level, empty object should pass
1102+ expect ( ( ) =>
1103+ schema . parse ( { } ) ,
1104+ ) . not . toThrow ( ) ;
10931105 } ) ;
10941106
10951107 it ( "should be usable to build custom schemas" , ( ) => {
@@ -1136,4 +1148,73 @@ describe("jsonSchemaObjectToZodRawShape", () => {
11361148 // Refinement with correct age, using same format as above test to confirm error was throw for correct reason
11371149 expect ( ( ) => customSchema . parse ( { age : 19 } ) ) . not . toThrow ( ) ;
11381150 } ) ;
1151+
1152+ it ( "should respect the required field when converting object properties" , ( ) => {
1153+ const jsonSchema = {
1154+ type : "object" ,
1155+ properties : {
1156+ requiredField : { type : "string" } ,
1157+ optionalField : { type : "number" } ,
1158+ anotherRequired : { type : "boolean" } ,
1159+ } ,
1160+ required : [ "requiredField" , "anotherRequired" ] ,
1161+ } ;
1162+
1163+ const rawShape = jsonSchemaObjectToZodRawShape ( jsonSchema ) ;
1164+
1165+ // Create a schema to test the shape
1166+ const schema = z . object ( rawShape ) ;
1167+
1168+ // Required fields should be required
1169+ expect ( ( ) =>
1170+ schema . parse ( {
1171+ optionalField : 42 ,
1172+ } ) ,
1173+ ) . toThrow ( ) ; // Missing required fields
1174+
1175+ // Optional field should be optional
1176+ expect ( ( ) =>
1177+ schema . parse ( {
1178+ requiredField : "test" ,
1179+ anotherRequired : true ,
1180+ } ) ,
1181+ ) . not . toThrow ( ) ; // Optional field missing is OK
1182+
1183+ // All fields present should work
1184+ expect ( ( ) =>
1185+ schema . parse ( {
1186+ requiredField : "test" ,
1187+ optionalField : 42 ,
1188+ anotherRequired : true ,
1189+ } ) ,
1190+ ) . not . toThrow ( ) ;
1191+ } ) ;
1192+
1193+ it ( "should make all fields optional when required array is not present" , ( ) => {
1194+ const jsonSchema = {
1195+ type : "object" ,
1196+ properties : {
1197+ field1 : { type : "string" } ,
1198+ field2 : { type : "number" } ,
1199+ field3 : { type : "boolean" } ,
1200+ } ,
1201+ // No required field - all properties should be optional
1202+ } ;
1203+
1204+ const rawShape = jsonSchemaObjectToZodRawShape ( jsonSchema ) ;
1205+ const schema = z . object ( rawShape ) ;
1206+
1207+ // All fields should be optional
1208+ expect ( ( ) => schema . parse ( { } ) ) . not . toThrow ( ) ;
1209+ expect ( ( ) => schema . parse ( { field1 : "test" } ) ) . not . toThrow ( ) ;
1210+ expect ( ( ) => schema . parse ( { field2 : 42 } ) ) . not . toThrow ( ) ;
1211+ expect ( ( ) => schema . parse ( { field3 : true } ) ) . not . toThrow ( ) ;
1212+ expect ( ( ) =>
1213+ schema . parse ( {
1214+ field1 : "test" ,
1215+ field2 : 42 ,
1216+ field3 : true ,
1217+ } ) ,
1218+ ) . not . toThrow ( ) ;
1219+ } ) ;
11391220} ) ;
0 commit comments