From f47c1d42378dc97bb19b98af355f55e870010e41 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Fri, 2 Aug 2024 09:20:21 -0700 Subject: [PATCH] fix(cli): fixes the issue that enhancer ignores zod schemas if it's explicitly configured in ZModel --- packages/schema/src/cli/plugin-runner.ts | 3 ++ packages/testtools/src/schema.ts | 3 -- tests/regression/tests/issue-1562.test.ts | 45 +++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/regression/tests/issue-1562.test.ts diff --git a/packages/schema/src/cli/plugin-runner.ts b/packages/schema/src/cli/plugin-runner.ts index f66b57295..ad1b304a3 100644 --- a/packages/schema/src/cli/plugin-runner.ts +++ b/packages/schema/src/cli/plugin-runner.ts @@ -183,12 +183,15 @@ export class PluginRunner { // 2. @core/enhancer const existingEnhancer = plugins.find((p) => p.provider === CorePlugins.Enhancer); if (existingEnhancer) { + // enhancer should load zod schemas if there're validation rules + existingEnhancer.options.withZodSchemas = hasValidation; corePlugins.push(existingEnhancer); plugins.splice(plugins.indexOf(existingEnhancer), 1); } else { if (options.defaultPlugins) { corePlugins.push( this.makeCorePlugin(CorePlugins.Enhancer, options.schemaPath, { + // enhancer should load zod schemas if there're validation rules withZodSchemas: hasValidation, }) ); diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index 6fa2b0e23..919a9d411 100644 --- a/packages/testtools/src/schema.ts +++ b/packages/testtools/src/schema.ts @@ -336,9 +336,6 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) { prisma, { user }, { - policy, - modelMeta, - zodSchemas, logPrismaQuery: opt.logPrismaQuery, transactionTimeout: 1000000, kinds: opt.enhancements, diff --git a/tests/regression/tests/issue-1562.test.ts b/tests/regression/tests/issue-1562.test.ts new file mode 100644 index 000000000..c07fee0d6 --- /dev/null +++ b/tests/regression/tests/issue-1562.test.ts @@ -0,0 +1,45 @@ +import { loadSchema } from '@zenstackhq/testtools'; +describe('issue 1562', () => { + it('regression', async () => { + const { enhance } = await loadSchema( + ` +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "sqlite" + url = "file:./dev.db" +} + +plugin zod { + provider = '@core/zod' +} + +plugin enhancer { + provider = '@core/enhancer' + generatePermissionChecker = true +} + +abstract model Base { + id String @id @default(uuid()) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt() + + // require login + @@allow('all', true) +} + +model User extends Base { + name String @unique @regex('^[a-zA-Z0-9_]{3,30}$') + + @@allow('read', true) +} + `, + { addPrelude: false } + ); + + const db = enhance(); + await expect(db.user.create({ data: { name: '1 2 3 4' } })).toBeRejectedByPolicy(); + }); +});