Skip to content

Zod Schema contains invalid import to logical Prisma Client, when @@validate rule references an enum #2065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
diesal11 opened this issue Apr 3, 2025 · 1 comment

Comments

@diesal11
Copy link
Contributor

diesal11 commented Apr 3, 2025

Description and expected behavior
Schema:

enum FooType {
    Bar
    Baz
}

// Add JSON type field so a logical prisma client is generated.
// Polymorphic models also trigger this bug 
type Meta {
    test String?
}

model Foo {
    id   String  @id @db.Uuid @default(uuid())
    type FooType
    meta Meta @json

    @@validate(type == Bar, "FooType must be Bar")
}

Screenshots
The generated Zod Schema file for Foo then contains an invalid import. .zenstack/models is a .d.ts file and contains no value exports.

Image

Environment (please complete the following information):

  • ZenStack version: 2.13.0
  • Prisma version: 6.5.0
  • Database type: Postgresql

Additional context

This bug doesn't occur when a logical client isn't generated. I believe the issue lies somewhere in this code, It doesn't really make sense to me that prismaClientPath is being mapped to a definitions file?

const { dmmf, newPrismaClientDtsPath } = await new EnhancerGenerator(model, options, project, outDir).generate();
let prismaClientPath: string | undefined;
if (dmmf) {
// a logical client is generated
if (options.output || globalOptions?.output) {
// handle custom output path
// get the absolute path of the prisma client types
const prismaClientPathAbs = path.resolve(outDir, 'models');
// resolve it relative to the schema path
prismaClientPath = normalizedRelative(path.dirname(options.schemaPath), prismaClientPathAbs);
} else {
prismaClientPath = `${RUNTIME_PACKAGE}/models`;
}
}

@diesal11 diesal11 changed the title Zod Schemas contain invalid import to logical Prisma Client, when @@validate rule references an enum Zod Schema contains invalid import to logical Prisma Client, when @@validate rule references an enum Apr 3, 2025
@ymc9
Copy link
Member

ymc9 commented Apr 7, 2025

Hey @diesal11 , I had a look into it, you're right the problem is "./zenstack/models" currently only exports types. However, to import enums, values should be exported there as well.

I think a simple fix would be to generate a "models.js" file which reexports the original prisma client module. The "/models" will behave in a "strange" way that its types correspond to logical schema, however values come from the original schema. But that's how it should work ...

I'll make a PR for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants