Feb 17, 2026
0
Level 1
Zod object to plain object (with default values)
Is there a way for zodobject to be converted to a plain object?
Like, I have the following schema:
const schema = z.object({
name: z.string().default(someResource?.name ?? null),
email: z.email().optional().default(someResource?.email ?? null),
address: z.string().optional().default(someResource?.address ?? null),
})
and I have tried: schema.parse({}) and it seems like it gives me a plain object, but without the default values of someObject (for this example's sake, let's just say someObject is not undefined or null).
I'm asking this question, because it's very inconvenient if I create the same object for the initial form data values like:
const formData = {
name: someResource?.name ?? null,
email: someResource?.email ?? null,
address: someResource?.address ?? null)
}
// and then define again the same fields:
const schema = z.object({
name: z.string().default(someResource?.name ?? null),
email: z.email().optional().default(someResource?.email ?? null),
address: z.string().optional().default(someResource?.address ?? null),
})
because what if I have over 10 fields, it would be very tiresome to re-define the same fields for formData and for the schema
Please or to participate in this conversation.