Skip to content

Zod 4 - Default Date values in nested objects #650

@arnolicious

Description

@arnolicious

Description
When defining a nested schema with, which includes dates, and the default values for those dates are set on the parent object, the default values are strings instead dates.
This is a regression compared to zod v3

export const schema = z.object({
	name: z.string().min(2),
	email: z.email(),
	dateRange: z.object({
		start: z.date(),
		end: z.date()
	}).default({
		start: new Date(),
		end: new Date()
	}),
	dateRangeIndividual: z.object({
		start: z.date().default(new Date()),
		end: z.date().default(new Date())
	}),
	date: z.date().default(new Date())
});

Resulting $form object values:

{
  name: "",
  email: "",
  dateRange: {
    start: "2025-10-29T10:47:03.068Z",   // <- string
    end: "2025-10-29T10:47:03.068Z"     // <- string
  },
  dateRangeIndividual: {
    start: 2025-10-29T10:52:25.537Z,      // <- date
    end: 2025-10-29T10:52:25.537Z        // <- date
  },
  date: 2025-10-29T10:47:03.068Z         // <- date
}

If applicable, a MRE
https://www.sveltelab.dev/kata9i0q30dg6vk?files=.%2Fsrc%2Froutes%2Fschema.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdoneIssue is fixed, will most likely be included in the next release.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions