Skip to content

Commit 7e64c60

Browse files
committed
Fix
1 parent 7259bde commit 7e64c60

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@ export { createUniqueItemsValidator, isValidWithSchema } from "./core/utils";
1515
*/
1616
export 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+
}

0 commit comments

Comments
 (0)