Make nested Zod object partial at only a specific level and add properties at specific level #2935
-
I'm currently working on an Express app and using Zod to validate an incoming request. I know Zod provide a What I'm currently doing is: const contractBodySchema = z.object({
name: z.string(),
nestedObject1: z.object({
value1: z.number // this value should never be optional
})
});
const contractCreationSchema = z.object({
body: contractBodySchema
});
const contractUpdateSchema = z.object({
body: contractBodySchema.partial()
}); It works fine but I was wondering if there was a simpler way to do this and maybe avoid creating the Add propertyAnother problem I encountered is adding nested properties to an existing schema. Let's say I have this schema const mySchema = z.object({
value1: z.object({
value2: z.object({
value3: z.string()
})
})
}) How do I add Cheers. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Has a solution been found? This kind of processing is critical for any real commercial applications, I'd expect there to be a well-supported way to handle this with Zod by now. |
Beta Was this translation helpful? Give feedback.
-
Is this what you are looking for? const mySchema = z.object( {
value1: z.object( {
value2: z.object( {
value3: z.string(),
value4: z.number(),
} )
} )
} ) If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
I don't think there is a good way to do what you are asking.
Here's the only way I can think of:
If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏
https://github.com/sponsors/JacobWei…