Browse Source

fix: safely resolve inner type for Zod arrays (#140)

Juzisuan965 4 months ago
parent
commit
7de08ccdf7
2 changed files with 16 additions and 2 deletions
  1. 8 1
      packages/core/src/schema.test.ts
  2. 8 1
      packages/core/src/schema.ts

+ 8 - 1
packages/core/src/schema.test.ts

@@ -169,7 +169,11 @@ describe("catalog.prompt", () => {
     const catalog = defineCatalog(testSchema, {
       components: {
         Card: {
-          props: z.object({ title: z.string() }),
+          props: z.object({
+            title: z.string(),
+            names: z.array(z.string()),
+            users: z.array(z.object({ name: z.string(), age: z.number() })),
+          }),
           description: "A card container",
           slots: ["default"],
         },
@@ -180,6 +184,9 @@ describe("catalog.prompt", () => {
     expect(prompt).toContain("AVAILABLE COMPONENTS");
     expect(prompt).toContain("Card");
     expect(prompt).toContain("A card container");
+    expect(prompt).toContain("title: string");
+    expect(prompt).toContain("names: Array<string>");
+    expect(prompt).toContain("users: Array<{ name: string, age: number }>");
   });
 
   it("includes AVAILABLE ACTIONS when present", () => {

+ 8 - 1
packages/core/src/schema.ts

@@ -1188,7 +1188,14 @@ function formatZodType(schema: z.ZodType): string {
     }
     case "ZodArray":
     case "array": {
-      const inner = (def.type as z.ZodType) ?? (def.element as z.ZodType);
+      // safely resolve inner type for Zod arrays
+      const inner = (
+        typeof def.element === "object"
+          ? def.element
+          : typeof def.type === "object"
+            ? def.type
+            : undefined
+      ) as z.ZodType | undefined;
       return inner ? `Array<${formatZodType(inner)}>` : "Array<unknown>";
     }
     case "ZodObject":