Skip to content

Commit f47c1d4

Browse files
committed
fix(cli): fixes the issue that enhancer ignores zod schemas if it's explicitly configured in ZModel
1 parent 3d7678a commit f47c1d4

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

packages/schema/src/cli/plugin-runner.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ export class PluginRunner {
183183
// 2. @core/enhancer
184184
const existingEnhancer = plugins.find((p) => p.provider === CorePlugins.Enhancer);
185185
if (existingEnhancer) {
186+
// enhancer should load zod schemas if there're validation rules
187+
existingEnhancer.options.withZodSchemas = hasValidation;
186188
corePlugins.push(existingEnhancer);
187189
plugins.splice(plugins.indexOf(existingEnhancer), 1);
188190
} else {
189191
if (options.defaultPlugins) {
190192
corePlugins.push(
191193
this.makeCorePlugin(CorePlugins.Enhancer, options.schemaPath, {
194+
// enhancer should load zod schemas if there're validation rules
192195
withZodSchemas: hasValidation,
193196
})
194197
);

packages/testtools/src/schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,6 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
336336
prisma,
337337
{ user },
338338
{
339-
policy,
340-
modelMeta,
341-
zodSchemas,
342339
logPrismaQuery: opt.logPrismaQuery,
343340
transactionTimeout: 1000000,
344341
kinds: opt.enhancements,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
describe('issue 1562', () => {
3+
it('regression', async () => {
4+
const { enhance } = await loadSchema(
5+
`
6+
generator client {
7+
provider = "prisma-client-js"
8+
}
9+
10+
datasource db {
11+
provider = "sqlite"
12+
url = "file:./dev.db"
13+
}
14+
15+
plugin zod {
16+
provider = '@core/zod'
17+
}
18+
19+
plugin enhancer {
20+
provider = '@core/enhancer'
21+
generatePermissionChecker = true
22+
}
23+
24+
abstract model Base {
25+
id String @id @default(uuid())
26+
createdAt DateTime @default(now())
27+
updatedAt DateTime @updatedAt()
28+
29+
// require login
30+
@@allow('all', true)
31+
}
32+
33+
model User extends Base {
34+
name String @unique @regex('^[a-zA-Z0-9_]{3,30}$')
35+
36+
@@allow('read', true)
37+
}
38+
`,
39+
{ addPrelude: false }
40+
);
41+
42+
const db = enhance();
43+
await expect(db.user.create({ data: { name: '1 2 3 4' } })).toBeRejectedByPolicy();
44+
});
45+
});

0 commit comments

Comments
 (0)