Skip to content

Commit 5aac7a4

Browse files
committed
fix(policy): when using zod4, during handling of @password, schemas from different zod versions may be mixed up
1 parent 961603a commit 5aac7a4

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
"test-integration": "pnpm run --filter=integration test --silent --forceExit",
1212
"test-regression": "pnpm run --filter=regression test --silent --forceExit",
1313
"test-scaffold": "tsx script/test-scaffold.ts",
14-
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public",
14+
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public --tag v2",
1515
"publish-preview": "pnpm --filter \"./packages/**\" -r publish --force --registry https://preview.registry.zenstack.dev/",
1616
"unpublish-preview": "pnpm --recursive --shell-mode exec -- npm unpublish -f --registry https://preview.registry.zenstack.dev/ \"\\$PNPM_PACKAGE_NAME\"",
17-
"publish-next": "pnpm --filter \"./packages/**\" -r publish --access public --tag next",
1817
"publish-preview-next": "pnpm --filter \"./packages/**\" -r publish --force --registry https://preview.registry.zenstack.dev/ --tag next",
1918
"unpublish-preview-next": "pnpm --recursive --shell-mode exec -- npm unpublish -f --registry https://preview.registry.zenstack.dev/ --tag next \"\\$PNPM_PACKAGE_NAME\"",
2019
"publish-test": "pnpm --filter \"./packages/**\" -r publish --access public --tag test"

packages/runtime/src/enhancements/node/policy/policy-utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

33
import deepmerge from 'deepmerge';
4-
import { z, type ZodError, type ZodObject, type ZodSchema } from 'zod';
4+
import type { z, ZodError, ZodObject, ZodSchema } from 'zod';
5+
import { z as zod3 } from 'zod/v3';
6+
import { z as zod4 } from 'zod/v4';
57
import { CrudFailureReason, PrismaErrorCode } from '../../../constants';
68
import {
79
clone,
@@ -1376,17 +1378,19 @@ export class PolicyUtil extends QueryUtils {
13761378
let schema: ZodObject<any> | undefined;
13771379

13781380
const overridePasswordFields = (schema: z.ZodObject<any>) => {
1381+
const useZod: any = schema._def ? zod3 : zod4;
1382+
13791383
let result = schema;
13801384
const modelFields = this.modelMeta.models[lowerCaseFirst(model)]?.fields;
13811385
if (modelFields) {
13821386
for (const [key, field] of Object.entries(modelFields)) {
13831387
if (field.attributes?.some((attr) => attr.name === '@password')) {
13841388
// override `@password` field schema with a string schema
1385-
let pwFieldSchema: ZodSchema = z.string();
1389+
let pwFieldSchema: ZodSchema = useZod.string();
13861390
if (field.isOptional) {
13871391
pwFieldSchema = pwFieldSchema.nullish();
13881392
}
1389-
result = result.merge(z.object({ [key]: pwFieldSchema }));
1393+
result = result.merge(useZod.object({ [key]: pwFieldSchema }));
13901394
}
13911395
}
13921396
}
@@ -1540,7 +1544,12 @@ export class PolicyUtil extends QueryUtils {
15401544
continue;
15411545
}
15421546

1543-
if (fieldInfo.isDataModel && queryArgs?.include && typeof queryArgs.include === 'object' && !queryArgs.include[field]) {
1547+
if (
1548+
fieldInfo.isDataModel &&
1549+
queryArgs?.include &&
1550+
typeof queryArgs.include === 'object' &&
1551+
!queryArgs.include[field]
1552+
) {
15441553
// respect include
15451554
delete entityData[field];
15461555
continue;

packages/runtime/src/validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { z } from 'zod';
1+
import type { z } from 'zod';
22
import { getZodErrorMessage } from './local-helpers';
33

44
/**

packages/runtime/src/zod-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { z as Z } from 'zod';
2+
import type { z as Z } from 'zod';
33

44
/**
55
* A smarter version of `z.union` that decide which candidate to use based on how few unrecognized keys it has.

0 commit comments

Comments
 (0)