Skip to content

Make nested Zod object partial at only a specific level and add properties at specific level #2935

Discussion options

You must be logged in to vote

I don't think there is a good way to do what you are asking.

Here's the only way I can think of:

const mySchema = z.object( {
    value1: z.object( {
        value2: z.object( {
            value3: z.string(),
        } )
    } )
} )

const mySchema2 = mySchema.extend( {
    value1: mySchema.shape.value1.extend( {
        value2: mySchema.shape.value1.shape.value2.extend( {
            value4: z.number(),
        } )
    } )
} )

console.log( Object.keys( mySchema2.shape.value1.shape.value2.shape ) ) // [ 'value3', 'value4' ]

If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏
https://github.com/sponsors/JacobWei…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@JacobWeisenburger
Comment options

@RenaudAubert
Comment options

Comment options

You must be logged in to vote
3 replies
@RenaudAubert
Comment options

@JacobWeisenburger
Comment options

Answer selected by JacobWeisenburger
@RenaudAubert
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants