Skip to content

Commit

Permalink
fix: generated zod schemas fail to compile when outputting to a custo…
Browse files Browse the repository at this point in the history
…m dir

Fixes #1610
  • Loading branch information
ymc9 committed Jul 23, 2024
1 parent 17fe8c3 commit 5b2a4c6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class EnhancerGenerator {

const enhanceTs = this.project.createSourceFile(
path.join(this.outDir, 'enhance.ts'),
`import { type EnhancementContext, type EnhancementOptions, type ZodSchemas, type AuthUser } from '@zenstackhq/runtime';
`/* eslint-disable */
import { type EnhancementContext, type EnhancementOptions, type ZodSchemas, type AuthUser } from '@zenstackhq/runtime';
import { createEnhancement } from '@zenstackhq/runtime/enhancements';
import modelMeta from './model-meta';
import policy from './policy';
Expand Down
6 changes: 4 additions & 2 deletions packages/schema/src/plugins/enhancer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const run: PluginFunction = async (model, options, _dmmf, globalOptions) => {
let prismaClientPath: string | undefined;
if (dmmf) {
// a logical client is generated
if (typeof options.output === 'string') {
if (options.output || globalOptions?.output) {
// handle custom output path

// get the absolute path of the prisma client types
const prismaClientPathAbs = path.resolve(options.output, 'models');
const prismaClientPathAbs = path.resolve(outDir, 'models');

// resolve it relative to the schema path
prismaClientPath = path.relative(path.dirname(options.schemaPath), prismaClientPathAbs);
Expand Down
56 changes: 56 additions & 0 deletions tests/regression/tests/issue-1610.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1610', () => {
it('regular prisma client', async () => {
await loadSchema(
`
model User {
id Int @id
posts Post[]
}
model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int
}
`,
{ fullZod: true, output: './lib/zen' }
);
});

it('logical prisma client', async () => {
await loadSchema(
`
model User {
id Int @id
posts Post[]
}
model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`,
{ fullZod: true, output: './lib/zen' }
);
});

it('no custom output', async () => {
await loadSchema(
`
model User {
id Int @id
posts Post[]
}
model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`,
{ fullZod: true, preserveTsFiles: true }
);
});
});

0 comments on commit 5b2a4c6

Please sign in to comment.