Skip to content

Commit 51985b1

Browse files
authored
fix: make sure zod schemas have type annotations (#574)
1 parent 1e1ad8f commit 51985b1

File tree

26 files changed

+369
-252
lines changed

26 files changed

+369
-252
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist
66
.env.local
77
.npmcache
88
coverage
9+
.build

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"generate": "langium generate",
1010
"watch": "concurrently \"langium generate --watch\" \"tsc --watch\"",
1111
"lint": "eslint src --ext ts",
12-
"build": "pnpm lint && pnpm clean && pnpm generate && tsc && copyfiles -F ./README.md ./LICENSE ./package.json dist",
12+
"build": "pnpm lint && pnpm clean && pnpm generate && tsc && copyfiles -F ./README.md ./LICENSE ./package.json dist && pnpm pack dist --pack-destination '../../../.build'",
1313
"prepublishOnly": "pnpm build",
1414
"publish-dev": "pnpm build && pnpm publish --tag dev"
1515
},

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"scripts": {
1616
"clean": "rimraf dist",
17-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE dist && copyfiles -u 1 ./src/plugin.zmodel dist",
17+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE dist && copyfiles -u 1 ./src/plugin.zmodel dist && pnpm pack dist --pack-destination '../../../../.build'",
1818
"watch": "tsc --watch",
1919
"lint": "eslint src --ext ts",
2020
"test": "jest",

packages/plugins/swr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"clean": "rimraf dist",
13-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE 'res/**/*' dist",
13+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE 'res/**/*' dist && pnpm pack dist --pack-destination '../../../../.build'",
1414
"watch": "tsc --watch",
1515
"lint": "eslint src --ext ts",
1616
"test": "jest",

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"clean": "rimraf dist",
13-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE 'res/**/*' dist",
13+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE 'res/**/*' dist && pnpm pack dist --pack-destination '../../../../.build'",
1414
"watch": "tsc --watch",
1515
"lint": "eslint src --ext ts",
1616
"test": "jest",

packages/plugins/trpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"clean": "rimraf dist",
13-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE 'res/**/*' dist",
13+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ./LICENSE 'res/**/*' dist && pnpm pack dist --pack-destination '../../../../.build'",
1414
"watch": "tsc --watch",
1515
"lint": "eslint src --ext ts",
1616
"prepublishOnly": "pnpm build",

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"scripts": {
1111
"clean": "rimraf dist",
12-
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ../../LICENSE dist && copyfiles -u1 'res/**/*' dist",
12+
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./README.md ../../LICENSE dist && copyfiles -u1 'res/**/*' dist && pnpm pack dist --pack-destination '../../../.build'",
1313
"watch": "tsc --watch",
1414
"lint": "eslint src --ext ts",
1515
"prepublishOnly": "pnpm build",

packages/schema/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"vscode:prepublish": "pnpm bundle",
7070
"vscode:package": "vsce package --no-dependencies",
7171
"clean": "rimraf bundle dist",
72-
"build": "pnpm clean && pnpm lint && tsc && copyfiles -F \"bin/*\" dist && copyfiles ./README-global.md ./LICENSE ./package.json dist && renamer --replace \"README.md\" dist/README-global.md && copyfiles -u 1 \"src/res/*\" dist && node build/post-build.js",
72+
"build": "pnpm clean && pnpm lint && tsc && copyfiles -F \"bin/*\" dist && copyfiles ./README-global.md ./LICENSE ./package.json dist && renamer --replace \"README.md\" dist/README-global.md && copyfiles -u 1 \"src/res/*\" dist && node build/post-build.js && pnpm pack dist --pack-destination '../../../.build'",
7373
"bundle": "pnpm clean && pnpm lint && node build/bundle.js --minify",
7474
"watch": "tsc --watch",
7575
"lint": "eslint src tests --ext ts",
@@ -127,7 +127,6 @@
127127
"@typescript-eslint/parser": "^5.42.0",
128128
"@vscode/vsce": "^2.19.0",
129129
"@zenstackhq/runtime": "workspace:*",
130-
"@zenstackhq/testtools": "workspace:*",
131130
"concurrently": "^7.4.0",
132131
"copyfiles": "^2.4.1",
133132
"dotenv": "^16.0.3",

packages/schema/src/plugins/plugin-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function getDefaultOutputFolder() {
4040
// Find the real runtime module path, it might be a symlink in pnpm
4141
let runtimeModulePath = require.resolve('@zenstackhq/runtime');
4242

43-
if (process.env.NODE_ENV === 'test') {
43+
if (process.env.ZENSTACK_TEST === '1') {
4444
// handling the case when running as tests, resolve relative to CWD
4545
runtimeModulePath = path.resolve(path.join(process.cwd(), 'node_modules', '@zenstackhq', 'runtime'));
4646
}

packages/schema/src/plugins/zod/transformer.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,9 @@ export default class Transformer {
253253
if (isAggregateInputType(name)) {
254254
name = `${name}Type`;
255255
}
256-
const end = `export const ${this.name}ObjectSchema = Schema`;
257-
return `const Schema: z.ZodType<Omit<Prisma.${name}, ${AUXILIARY_FIELDS.map((f) => "'" + f + "'").join(
258-
'|'
259-
)}>> = ${schema};\n\n ${end}`;
256+
return `export const ${this.name}ObjectSchema: z.ZodType<Omit<Prisma.${name}, ${AUXILIARY_FIELDS.map(
257+
(f) => "'" + f + "'"
258+
).join('|')}>> = ${schema};`;
260259
}
261260

262261
addFinalWrappers({ zodStringFields }: { zodStringFields: string[] }) {
@@ -390,14 +389,21 @@ export default class Transformer {
390389
const { selectImport, includeImport, selectZodSchemaLineLazy, includeZodSchemaLineLazy } =
391390
this.resolveSelectIncludeImportAndZodSchemaLine(model);
392391

393-
let imports = [`import { z } from 'zod'`, selectImport, includeImport];
392+
let imports = [
393+
`import { z } from 'zod'`,
394+
this.generateImportPrismaStatement(),
395+
selectImport,
396+
includeImport,
397+
];
394398
let codeBody = '';
399+
const operations: [string, string][] = [];
395400

396401
if (findUnique) {
397402
imports.push(
398403
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`
399404
);
400405
codeBody += `findUnique: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema }),`;
406+
operations.push(['findUnique', modelName]);
401407
}
402408

403409
if (findFirst) {
@@ -412,6 +418,7 @@ export default class Transformer {
412418
codeBody += `findFirst: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${modelName}OrderByWithRelationInputObjectSchema, ${modelName}OrderByWithRelationInputObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${upperCaseFirst(
413419
modelName
414420
)}ScalarFieldEnumSchema).optional() }),`;
421+
operations.push(['findFirst', modelName]);
415422
}
416423

417424
if (findMany) {
@@ -426,6 +433,7 @@ export default class Transformer {
426433
codeBody += `findMany: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${modelName}OrderByWithRelationInputObjectSchema, ${modelName}OrderByWithRelationInputObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${upperCaseFirst(
427434
modelName
428435
)}ScalarFieldEnumSchema).optional() }),`;
436+
operations.push(['findMany', modelName]);
429437
}
430438

431439
if (createOne) {
@@ -434,27 +442,31 @@ export default class Transformer {
434442
`import { ${modelName}UncheckedCreateInputObjectSchema } from '../objects/${modelName}UncheckedCreateInput.schema'`
435443
);
436444
codeBody += `create: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} data: z.union([${modelName}CreateInputObjectSchema, ${modelName}UncheckedCreateInputObjectSchema]) }),`;
445+
operations.push(['create', modelName]);
437446
}
438447

439448
if (createMany) {
440449
imports.push(
441450
`import { ${modelName}CreateManyInputObjectSchema } from '../objects/${modelName}CreateManyInput.schema'`
442451
);
443452
codeBody += `createMany: z.object({ data: z.union([${modelName}CreateManyInputObjectSchema, z.array(${modelName}CreateManyInputObjectSchema)]) }),`;
453+
operations.push(['createMany', modelName]);
444454
}
445455

446456
if (deleteOne) {
447457
imports.push(
448458
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`
449459
);
450460
codeBody += `'delete': z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema }),`;
461+
operations.push(['delete', modelName]);
451462
}
452463

453464
if (deleteMany) {
454465
imports.push(
455466
`import { ${modelName}WhereInputObjectSchema } from '../objects/${modelName}WhereInput.schema'`
456467
);
457468
codeBody += `deleteMany: z.object({ where: ${modelName}WhereInputObjectSchema.optional() }),`;
469+
operations.push(['deleteMany', modelName]);
458470
}
459471

460472
if (updateOne) {
@@ -464,6 +476,7 @@ export default class Transformer {
464476
`import { ${modelName}WhereUniqueInputObjectSchema } from '../objects/${modelName}WhereUniqueInput.schema'`
465477
);
466478
codeBody += `update: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} data: z.union([${modelName}UpdateInputObjectSchema, ${modelName}UncheckedUpdateInputObjectSchema]), where: ${modelName}WhereUniqueInputObjectSchema }),`;
479+
operations.push(['update', modelName]);
467480
}
468481

469482
if (updateMany) {
@@ -473,6 +486,7 @@ export default class Transformer {
473486
`import { ${modelName}WhereInputObjectSchema } from '../objects/${modelName}WhereInput.schema'`
474487
);
475488
codeBody += `updateMany: z.object({ data: z.union([${modelName}UpdateManyMutationInputObjectSchema, ${modelName}UncheckedUpdateManyInputObjectSchema]), where: ${modelName}WhereInputObjectSchema.optional() }),`;
489+
operations.push(['updateMany', modelName]);
476490
}
477491

478492
if (upsertOne) {
@@ -484,6 +498,7 @@ export default class Transformer {
484498
`import { ${modelName}UncheckedUpdateInputObjectSchema } from '../objects/${modelName}UncheckedUpdateInput.schema'`
485499
);
486500
codeBody += `upsert: z.object({ ${selectZodSchemaLineLazy} ${includeZodSchemaLineLazy} where: ${modelName}WhereUniqueInputObjectSchema, create: z.union([${modelName}CreateInputObjectSchema, ${modelName}UncheckedCreateInputObjectSchema]), update: z.union([${modelName}UpdateInputObjectSchema, ${modelName}UncheckedUpdateInputObjectSchema]) }),`;
501+
operations.push(['upsert', modelName]);
487502
}
488503

489504
const aggregateOperations = [];
@@ -532,6 +547,7 @@ export default class Transformer {
532547
codeBody += `aggregate: z.object({ where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${modelName}OrderByWithRelationInputObjectSchema, ${modelName}OrderByWithRelationInputObjectSchema.array()]).optional(), cursor: ${modelName}WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), ${aggregateOperations.join(
533548
', '
534549
)} }),`;
550+
operations.push(['aggregate', modelNameVar]);
535551
}
536552

537553
if (groupBy) {
@@ -546,6 +562,7 @@ export default class Transformer {
546562
codeBody += `groupBy: z.object({ where: ${modelName}WhereInputObjectSchema.optional(), orderBy: z.union([${modelName}OrderByWithAggregationInputObjectSchema, ${modelName}OrderByWithAggregationInputObjectSchema.array()]).optional(), having: ${modelName}ScalarWhereWithAggregatesInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), by: z.array(${upperCaseFirst(
547563
modelName
548564
)}ScalarFieldEnumSchema), ${aggregateOperations.join(', ')} }),`;
565+
operations.push(['groupBy', modelNameVar]);
549566
}
550567

551568
imports = [...new Set(imports)];
@@ -555,7 +572,13 @@ export default class Transformer {
555572
/* eslint-disable */
556573
${imports.join(';\n')}
557574
558-
export const ${modelName}InputSchema = {
575+
type ${modelName}InputSchemaType = {
576+
${operations
577+
.map((op) => indentString(`${op[0]}: z.ZodType<Prisma.${op[1]}${upperCaseFirst(op[0])}Args>`, 4))
578+
.join(',\n')}
579+
}
580+
581+
export const ${modelName}InputSchema: ${modelName}InputSchemaType = {
559582
${indentString(codeBody, 4)}
560583
};
561584
`;

packages/schema/src/utils/pkg-utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ export function installPackage(
4747
switch (manager) {
4848
case 'yarn':
4949
execSync(
50-
`yarn --cwd "${projectPath}" add ${exactVersion ? '--exact' : ''} ${pkg}@${tag} ${
51-
dev ? ' --dev' : ''
52-
} --ignore-engines`
50+
`yarn --cwd "${projectPath}" add ${exactVersion ? '--exact' : ''} ${pkg}@${tag} ${dev ? ' --dev' : ''}`
5351
);
5452
break;
5553

0 commit comments

Comments
 (0)