zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 88 forks source link

Zod models are undefined on runtime for client components #861

Closed eelior closed 10 months ago

eelior commented 10 months ago

Description and expected behavior After following the instructions in the Form Validation blog post, I attempted to use the generated zod models. They seemed to be working correctly at first, but then I encountered a runtime error.

Screenshots If applicable, add screenshots to help explain your problem.

image

(alias) const ClientCreateSchema: z.ZodObject<{
    id: z.ZodOptional<z.ZodString>;
    createdAt: z.ZodOptional<z.ZodDate>;
    updatedAt: z.ZodOptional<z.ZodDate>;
    email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
    phone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
    firstName: z.ZodString;
    lastName: z.ZodString;
}, "strip", z.ZodTypeAny, {
    firstName: string;
    lastName: string;
    id?: string | undefined;
    createdAt?: Date | undefined;
    updatedAt?: Date | undefined;
    email?: string | null | undefined;
    phone?: string | null | undefined;
}, {
    firstName: string;
    lastName: string;
    ... 4 more ...;
    phone?: string | null | undefined;
}>
image
Unhandled Runtime Error
TypeError: Cannot read properties of undefined (reading 'ClientCreateSchema')

Environment:

Additional context I have searched everywhere but couldn't find a similar issue. would greatly appreciate any help or direction in debugging it. 🙏🏽

ymc9 commented 10 months ago

Hi @eelior , thanks for reporting this! Do you have a repro project that we can look into?

eelior commented 10 months ago

Here you go: https://github.com/eelior/zenstack-zod-model-undefined

While creating this repro I noticed this is happening only in client components, see 2 lasts commits. Updating issue title accordingly.

ymc9 commented 10 months ago

Thank you @eelior ! Let me look into it. Feel free to find me on Discord 😄

ymc9 commented 10 months ago

Hi @eelior , sorry for the latest response.

It seems that Next13's app dir has stricter rules about the way modules are exported and required. I'm making a fix, and it'll be included in the v1.5.0 release this week. Thanks for reporting the issue!