Skip to content
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

fix(cli): fixes the issue that enhancer ignores zod schemas if it's explicitly configured in ZModel #1632

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/schema/src/cli/plugin-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);
Expand Down
3 changes: 0 additions & 3 deletions packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 45 additions & 0 deletions tests/regression/tests/issue-1562.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
Loading