From 5dc6c8c3ca2fa63fc50f4e8dca95319da3c3bee3 Mon Sep 17 00:00:00 2001 From: Satvik Choudhary Date: Tue, 23 Sep 2025 18:18:37 +0530 Subject: [PATCH 1/2] Fix TypeScript Date type generation for time.Time fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Changed generated TypeScript type from 'date' to 'Date' (capital D) - Added test cases for recursive embedded structs with pointers and dates - TypeScript uses 'Date' as the correct type name, not lowercase 'date' 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- zod.go | 2 +- zod_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/zod.go b/zod.go index d3c4f2f..84c627b 100644 --- a/zod.go +++ b/zod.go @@ -525,7 +525,7 @@ func (c *Converter) getType(t reflect.Type, indent int) string { // Handle fields with non-defined types - these are inline. return c.getTypeStruct(t, indent) } else if t.Name() == "Time" { - return "date" + return "Date" } else { return c.prefix + name } diff --git a/zod_test.go b/zod_test.go index 2aeed41..2b98a20 100644 --- a/zod_test.go +++ b/zod_test.go @@ -2262,3 +2262,69 @@ export type ItemF = z.infer `, c.Export()) } + +func TestRecursiveEmbeddedWithPointersAndDates(t *testing.T) { + // Test 1: Recursive struct with pointer field and date + type TreeNode struct { + Value string + CreatedAt time.Time + Children *[]TreeNode + } + + type Tree struct { + TreeNode + UpdatedAt time.Time + } + + assert.Equal(t, `export type TreeNode = { + Value: string, + CreatedAt: Date, + Children: TreeNode[] | null, +} +const TreeNodeSchemaShape = { + Value: z.string(), + CreatedAt: z.coerce.date(), + Children: z.lazy(() => TreeNodeSchema).array().nullable(), +} +export const TreeNodeSchema: z.ZodType = z.object(TreeNodeSchemaShape) + +export const TreeSchema = z.object({ + ...TreeNodeSchemaShape, + UpdatedAt: z.coerce.date(), +}) +export type Tree = z.infer + +`, StructToZodSchema(Tree{})) + + // Test 2: Embedded struct with pointer to self and date + type Comment struct { + Text string + Timestamp time.Time + Reply *Comment + } + + type Article struct { + Comment + Title string + } + + assert.Equal(t, `export type Comment = { + Text: string, + Timestamp: Date, + Reply: Comment | null, +} +const CommentSchemaShape = { + Text: z.string(), + Timestamp: z.coerce.date(), + Reply: z.lazy(() => CommentSchema).nullable(), +} +export const CommentSchema: z.ZodType = z.object(CommentSchemaShape) + +export const ArticleSchema = z.object({ + ...CommentSchemaShape, + Title: z.string(), +}) +export type Article = z.infer + +`, StructToZodSchema(Article{})) +} From bceb749b0574a34d95919d224084812b0419a2d9 Mon Sep 17 00:00:00 2001 From: Satvik Choudhary Date: Tue, 23 Sep 2025 18:27:38 +0530 Subject: [PATCH 2/2] fix split into subtests --- zod_test.go | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/zod_test.go b/zod_test.go index 2b98a20..dea0857 100644 --- a/zod_test.go +++ b/zod_test.go @@ -2264,19 +2264,19 @@ export type ItemF = z.infer } func TestRecursiveEmbeddedWithPointersAndDates(t *testing.T) { - // Test 1: Recursive struct with pointer field and date - type TreeNode struct { - Value string - CreatedAt time.Time - Children *[]TreeNode - } + t.Run("recursive struct with pointer field and date", func(t *testing.T) { + type TreeNode struct { + Value string + CreatedAt time.Time + Children *[]TreeNode + } - type Tree struct { - TreeNode - UpdatedAt time.Time - } + type Tree struct { + TreeNode + UpdatedAt time.Time + } - assert.Equal(t, `export type TreeNode = { + assert.Equal(t, `export type TreeNode = { Value: string, CreatedAt: Date, Children: TreeNode[] | null, @@ -2295,20 +2295,21 @@ export const TreeSchema = z.object({ export type Tree = z.infer `, StructToZodSchema(Tree{})) + }) - // Test 2: Embedded struct with pointer to self and date - type Comment struct { - Text string - Timestamp time.Time - Reply *Comment - } + t.Run("embedded struct with pointer to self and date", func(t *testing.T) { + type Comment struct { + Text string + Timestamp time.Time + Reply *Comment + } - type Article struct { - Comment - Title string - } + type Article struct { + Comment + Title string + } - assert.Equal(t, `export type Comment = { + assert.Equal(t, `export type Comment = { Text: string, Timestamp: Date, Reply: Comment | null, @@ -2327,4 +2328,5 @@ export const ArticleSchema = z.object({ export type Article = z.infer `, StructToZodSchema(Article{})) + }) }