Bence (Arzen) - Hi guys 🙂 Is there a way to cr...
Hi guys 🙂 Is there a way to create a
recursive
record
schema? I know how to make recursive schema using an object
, but I am curious if it is currently possible to do with records
. The keys can be anything in my data, that's why I'm trying to use a record.3 Replies
Certainly, you can see an example here: https://zod.dev/?id=recursive-types or my implementation here: https://gitlab.com/toegang-voor-iedereen/specification/types/typescript/-/blob/develop/src/resources.ts?ref_type=heads
https://gitlab.com/toegang-voor-iedereen/specification/types/typescript/-/blob/develop/src/resources.ts?ref_type=heads#L140-165 Pay close attention to the definition of the Input type (
DefinitionDetailsInput
), The Output Type (DefinitionDetails
), extending a base (DefinitionDetailsBase
) and passing Input and Output types to the function signature to Zod, as well to the use of z.lazy()
Thank you Steve! I'll take a look at your examples 👍
P.S. don't use
z.transform()
with recursive types
It will memory leak and there probably is a exponential correlation about the depth of recursion, and how bad it will memory leak
@Bence (Arzen) are you using zod for a serious project?