[Discussion] Parsing input that's a superset of the schema type
See original GitHub issueGiven a zod schema like
const ZRecord = z.object({
id: z.number(),
name: z.string()
});
how can I use ZRecord
to extract the schema from a conforming object that has additional fields? E.g.
const result = ZRecord.safeParse({ id: 123, name: "Joe", notes:"enjoys hiking" });
Is there any option that I can pass to the parse methods (e.g. ZRecord.safeParse
) that will ignore additional keys, but also mention them in the result.error.errors
list? I was hoping that even when result.success
was false
, result.data
would still have the parsed hierarchy. I don’t see a way to do this other than iterating through the ZAddEntryParams.shape
hierarchy and pulling the relevant attributes from the input into a new “clipped” object before parsing.
It would be valuable to extract the schema from a larger object for something like a web request, to give the API caller a message that they had extra keys in their request.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Oh I see, that was just a secondary thing and
.passthrough
covers my use case pretty well. I kinda merged the two questions into one and it wasn’t clear. The reply with the link was for posterity.Thanks for making this library. The more I use it the more I’m impressed with how complete and well conceived it is!
Thank you! That’s always great to hear 😃