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..dea0857 100644 --- a/zod_test.go +++ b/zod_test.go @@ -2262,3 +2262,71 @@ export type ItemF = z.infer `, c.Export()) } + +func TestRecursiveEmbeddedWithPointersAndDates(t *testing.T) { + 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 + } + + 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{})) + }) + + 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 + } + + 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{})) + }) +}