Skip to content

Is there a z.ZodSchema specifically for objects? #874

Discussion options

You must be logged in to vote

Is there any inbuilt solution to this issue?

I don't think there is.


I'm pretty sure you are looking for something like ShapeFromData (see below). I modified this from a few other implementations that I found online. So far it has been working well for me, but I haven't tested it for every possible schema. Let me know what you think?

type ShapeFromData<Data extends Record<string, any> | undefined> = Data extends undefined ? never : {
    [ key in keyof Data ]-?:
    unknown extends Data[ key ] ? z.ZodType<Data[ key ]> | z.ZodOptional<z.ZodType<Data[ key ]>> :
    undefined extends Data[ key ] ? z.ZodOptional<z.ZodType<Data[ key ]>> :
    z.ZodType<Data[ key ]>
}
type Obj = {
    a: 'a'
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@riordanpawley
Comment options

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