Z
Zodโ€ข3mo ago
shadi

shadi - hey everyone, i've been struggling with...

hey everyone, i've been struggling with building a recursive zod schema. I tried following the docs: https://github.com/colinhacks/zod?tab=readme-ov-file#recursive-types but still can't figure it out for my use case. here is what I'm trying to achieve:
const BranchSchema = z.object({
prompt: BranchPromptSchema, // regular object schema
branches: z.array(ConfigSchema),
});

const ConfigSchema = z.union([
z.object({
name: z.string(),
branch: BranchSchema,
actions: z.undefined(),
}),
z.object({
name: z.string(),
branch: z.undefined(),
actions: z.array(ActionSchema),
}),
]);
const BranchSchema = z.object({
prompt: BranchPromptSchema, // regular object schema
branches: z.array(ConfigSchema),
});

const ConfigSchema = z.union([
z.object({
name: z.string(),
branch: BranchSchema,
actions: z.undefined(),
}),
z.object({
name: z.string(),
branch: z.undefined(),
actions: z.array(ActionSchema),
}),
]);
thanks in advance ๐Ÿ™
Solution:
Message Not Public
Sign In & Join Server To View
Jump to solution
2 Replies
Solution
Unknown User
Unknown Userโ€ข3mo ago
Message Not Public
Sign In & Join Server To View
shadi
shadiโ€ข3mo ago
thanks alot, that worked perfectly!