Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
68 changes: 68 additions & 0 deletions zod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2262,3 +2262,71 @@ export type ItemF = z.infer<typeof ItemFSchema>

`, 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<TreeNode> = z.object(TreeNodeSchemaShape)

export const TreeSchema = z.object({
...TreeNodeSchemaShape,
UpdatedAt: z.coerce.date(),
})
export type Tree = z.infer<typeof TreeSchema>

`, 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<Comment> = z.object(CommentSchemaShape)

export const ArticleSchema = z.object({
...CommentSchemaShape,
Title: z.string(),
})
export type Article = z.infer<typeof ArticleSchema>

`, StructToZodSchema(Article{}))
})
}