Skip to content

Commit b9bcbf8

Browse files
authored
chore: use zod v3 for dev dependency (#201)
* chore: use zod v3 for dev dependency * update
1 parent 378a6b5 commit b9bcbf8

File tree

9 files changed

+46
-45
lines changed

9 files changed

+46
-45
lines changed

packages/runtime/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"@zenstackhq/testtools": "workspace:*",
100100
"@zenstackhq/typescript-config": "workspace:*",
101101
"@zenstackhq/vitest-config": "workspace:*",
102-
"tsx": "^4.19.2"
102+
"tsx": "^4.19.2",
103+
"zod": "~3.25.0"
103104
}
104105
}

packages/runtime/src/client/crud-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable e
247247
};
248248

249249
type ArrayFilter<Schema extends SchemaDef, T extends string> = {
250-
equals?: MapScalarType<Schema, T>[];
251-
has?: MapScalarType<Schema, T>;
250+
equals?: MapScalarType<Schema, T>[] | null;
251+
has?: MapScalarType<Schema, T> | null;
252252
hasEvery?: MapScalarType<Schema, T>[];
253253
hasSome?: MapScalarType<Schema, T>[];
254254
isEmpty?: boolean;

packages/runtime/src/client/crud/validator.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ export class InputValidator<Schema extends SchemaDef> {
233233
} else {
234234
return match(type)
235235
.with('String', () => z.string())
236-
.with('Int', () => z.int())
236+
.with('Int', () => z.number().int())
237237
.with('Float', () => z.number())
238238
.with('Boolean', () => z.boolean())
239-
.with('BigInt', () => z.union([z.int(), z.bigint()]))
239+
.with('BigInt', () => z.union([z.number().int(), z.bigint()]))
240240
.with('Decimal', () => z.union([z.number(), z.instanceof(Decimal), z.string()]))
241241
.with('DateTime', () => z.union([z.date(), z.string().datetime()]))
242242
.with('Bytes', () => z.instanceof(Uint8Array))
@@ -252,20 +252,22 @@ export class InputValidator<Schema extends SchemaDef> {
252252
}
253253
const typeDef = this.schema.typeDefs?.[type];
254254
invariant(typeDef, `Type definition "${type}" not found in schema`);
255-
schema = z.looseObject(
256-
Object.fromEntries(
257-
Object.entries(typeDef.fields).map(([field, def]) => {
258-
let fieldSchema = this.makePrimitiveSchema(def.type);
259-
if (def.array) {
260-
fieldSchema = fieldSchema.array();
261-
}
262-
if (def.optional) {
263-
fieldSchema = fieldSchema.optional();
264-
}
265-
return [field, fieldSchema];
266-
}),
267-
),
268-
);
255+
schema = z
256+
.object(
257+
Object.fromEntries(
258+
Object.entries(typeDef.fields).map(([field, def]) => {
259+
let fieldSchema = this.makePrimitiveSchema(def.type);
260+
if (def.array) {
261+
fieldSchema = fieldSchema.array();
262+
}
263+
if (def.optional) {
264+
fieldSchema = fieldSchema.optional();
265+
}
266+
return [field, fieldSchema];
267+
}),
268+
),
269+
)
270+
.passthrough();
269271
this.schemaCache.set(key, schema);
270272
return schema;
271273
}
@@ -469,7 +471,7 @@ export class InputValidator<Schema extends SchemaDef> {
469471

470472
private makeDateTimeFilterSchema(optional: boolean, withAggregations: boolean): ZodType {
471473
return this.makeCommonPrimitiveFilterSchema(
472-
z.union([z.iso.datetime(), z.date()]),
474+
z.union([z.string().datetime(), z.date()]),
473475
optional,
474476
() => z.lazy(() => this.makeDateTimeFilterSchema(optional, withAggregations)),
475477
withAggregations ? ['_count', '_min', '_max'] : undefined,
@@ -519,7 +521,7 @@ export class InputValidator<Schema extends SchemaDef> {
519521
gte: baseSchema.optional(),
520522
not: makeThis().optional(),
521523
...(withAggregations?.includes('_count')
522-
? { _count: this.makeNumberFilterSchema(z.int(), false, false).optional() }
524+
? { _count: this.makeNumberFilterSchema(z.number().int(), false, false).optional() }
523525
: {}),
524526
...(withAggregations?.includes('_avg') ? { _avg: commonAggSchema() } : {}),
525527
...(withAggregations?.includes('_sum') ? { _sum: commonAggSchema() } : {}),
@@ -1020,7 +1022,7 @@ export class InputValidator<Schema extends SchemaDef> {
10201022
return z.strictObject({
10211023
where: this.makeWhereSchema(model, false).optional(),
10221024
data: this.makeUpdateDataSchema(model, [], true),
1023-
limit: z.int().nonnegative().optional(),
1025+
limit: z.number().int().nonnegative().optional(),
10241026
});
10251027
}
10261028

@@ -1158,7 +1160,7 @@ export class InputValidator<Schema extends SchemaDef> {
11581160
return z
11591161
.object({
11601162
where: this.makeWhereSchema(model, false).optional(),
1161-
limit: z.int().nonnegative().optional(),
1163+
limit: z.number().int().nonnegative().optional(),
11621164
})
11631165

11641166
.optional();
@@ -1255,10 +1257,10 @@ export class InputValidator<Schema extends SchemaDef> {
12551257
const modelDef = requireModel(this.schema, model);
12561258
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
12571259

1258-
let schema = z.strictObject({
1260+
let schema: z.ZodSchema = z.strictObject({
12591261
where: this.makeWhereSchema(model, false).optional(),
12601262
orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
1261-
by: this.orArray(z.enum(nonRelationFields), true),
1263+
by: this.orArray(z.enum(nonRelationFields as [string, ...string[]]), true),
12621264
having: this.makeHavingSchema(model).optional(),
12631265
skip: this.makeSkipSchema().optional(),
12641266
take: this.makeTakeSchema().optional(),
@@ -1340,11 +1342,11 @@ export class InputValidator<Schema extends SchemaDef> {
13401342
// #region Helpers
13411343

13421344
private makeSkipSchema() {
1343-
return z.int().nonnegative();
1345+
return z.number().int().nonnegative();
13441346
}
13451347

13461348
private makeTakeSchema() {
1347-
return z.int();
1349+
return z.number().int();
13481350
}
13491351

13501352
private refineForSelectIncludeMutuallyExclusive(schema: ZodType) {

packages/runtime/test/client-api/mixin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ model Bar with CommonFields {
5151
description: 'Bar',
5252
},
5353
}),
54-
).rejects.toThrow('Invalid input');
54+
).rejects.toThrow(/invalid/i);
5555

5656
await expect(
5757
client.bar.create({

packages/runtime/test/client-api/typed-json-fields.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ model User {
127127
},
128128
},
129129
}),
130-
).rejects.toThrow('Invalid input');
130+
).rejects.toThrow(/invalid/i);
131131
});
132132

133133
it('works with find', async () => {
@@ -215,7 +215,7 @@ model User {
215215
},
216216
},
217217
}),
218-
).rejects.toThrow('Invalid input');
218+
).rejects.toThrow(/invalid/i);
219219
});
220220
},
221221
);

packages/zod/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
},
3131
"devDependencies": {
3232
"@zenstackhq/eslint-config": "workspace:*",
33-
"@zenstackhq/typescript-config": "workspace:*"
33+
"@zenstackhq/typescript-config": "workspace:*",
34+
"zod": "~3.25.0"
3435
},
3536
"peerDependencies": {
3637
"zod": "catalog:"

packages/zod/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ function makeScalarSchema(fieldDef: FieldDef): ZodType {
2828
.with('String', () => z.string())
2929
.with(P.union('Int', 'BigInt', 'Float', 'Decimal'), () => z.number())
3030
.with('Boolean', () => z.boolean())
31-
.with('DateTime', () => z.iso.datetime())
31+
.with('DateTime', () => z.string().datetime())
3232
.otherwise(() => z.unknown());
3333
}

pnpm-lock.yaml

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ packages:
44
- tests/**
55
catalog:
66
kysely: ^0.27.6
7-
zod: ^4.0.0
7+
zod: ^3.25.0 || ^4.0.0
88
prisma: ^6.10.0
99
langium: 3.5.0
1010
langium-cli: 3.5.0

0 commit comments

Comments
 (0)