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.5 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?I wasn't planning too haha
that's a good shout! Thank you! 🙂
Yes, it is a serious project. I'm using zod at work for our internal site. In the end I changed zod for Effect.Schema as zod was quite restrictive and couldn't do what I wanted it to do. :\
Be very aware that recursion and polymorphism will probably memory leak quite easily