I have the following Zod schema:
const schema = z.object({
nested1: z.object({
nested2: z.object({
nested3: z.string(),
dummy1: z.string(),
dummy2: z.string(),
}),
}),
});
And I try to build a new schema which would look like the original but with some .catch()
statements:
const secondSchema = z.object({
nested1: z.object({
nested2: z.object({
nested3: z.string().catch(null),
dummy1: z.string(),
dummy2: z.string(),
}),
}),
});
I would like to extend the original schema
with the least duplicate code.
Right now the fields dummy1
and dummy2
are duplicated.
I try to get something like:
const secondSchema = schema.extend({
nested1: z.object({
nested2: z.object({
nested3: z.string().catch(null),
}),
}),
});
But this code would remove dummy1
and dummy2
off the schema because I overwrite nested2
How can I do it with least duplicate code?