Skip to content

Commit 31e3361

Browse files
authored
fix: use z.strictObject consistently (#179)
* fix: use z.strictObject consistently * use nominal operator for onQuery plugin callback
1 parent 1b5b5a1 commit 31e3361

File tree

6 files changed

+71
-55
lines changed

6 files changed

+71
-55
lines changed

packages/runtime/src/client/client-impl.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { AuthType } from '../schema/auth';
1414
import type { UnwrapTuplePromises } from '../utils/type-utils';
1515
import type { ClientConstructor, ClientContract, ModelOperations, TransactionIsolationLevel } from './contract';
1616
import { AggregateOperationHandler } from './crud/operations/aggregate';
17-
import type { CrudOperation } from './crud/operations/base';
17+
import type { AllCrudOperation, CoreCrudOperation } from './crud/operations/base';
1818
import { BaseOperationHandler } from './crud/operations/base';
1919
import { CountOperationHandler } from './crud/operations/count';
2020
import { CreateOperationHandler } from './crud/operations/create';
@@ -351,7 +351,8 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
351351
resultProcessor: ResultProcessor<Schema>,
352352
): ModelOperations<Schema, Model> {
353353
const createPromise = (
354-
operation: CrudOperation,
354+
operation: CoreCrudOperation,
355+
nominalOperation: AllCrudOperation,
355356
args: unknown,
356357
handler: BaseOperationHandler<Schema>,
357358
postProcess = false,
@@ -383,7 +384,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
383384
onQuery({
384385
client,
385386
model,
386-
operation,
387+
operation: nominalOperation,
387388
// reflect the latest override if provided
388389
args: _args,
389390
// ensure inner overrides are propagated to the previous proceed
@@ -400,6 +401,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
400401
return {
401402
findUnique: (args: unknown) => {
402403
return createPromise(
404+
'findUnique',
403405
'findUnique',
404406
args,
405407
new FindOperationHandler<Schema>(client, model, inputValidator),
@@ -410,6 +412,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
410412
findUniqueOrThrow: (args: unknown) => {
411413
return createPromise(
412414
'findUnique',
415+
'findUniqueOrThrow',
413416
args,
414417
new FindOperationHandler<Schema>(client, model, inputValidator),
415418
true,
@@ -419,6 +422,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
419422

420423
findFirst: (args: unknown) => {
421424
return createPromise(
425+
'findFirst',
422426
'findFirst',
423427
args,
424428
new FindOperationHandler<Schema>(client, model, inputValidator),
@@ -429,6 +433,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
429433
findFirstOrThrow: (args: unknown) => {
430434
return createPromise(
431435
'findFirst',
436+
'findFirstOrThrow',
432437
args,
433438
new FindOperationHandler<Schema>(client, model, inputValidator),
434439
true,
@@ -438,6 +443,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
438443

439444
findMany: (args: unknown) => {
440445
return createPromise(
446+
'findMany',
441447
'findMany',
442448
args,
443449
new FindOperationHandler<Schema>(client, model, inputValidator),
@@ -447,6 +453,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
447453

448454
create: (args: unknown) => {
449455
return createPromise(
456+
'create',
450457
'create',
451458
args,
452459
new CreateOperationHandler<Schema>(client, model, inputValidator),
@@ -456,6 +463,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
456463

457464
createMany: (args: unknown) => {
458465
return createPromise(
466+
'createMany',
459467
'createMany',
460468
args,
461469
new CreateOperationHandler<Schema>(client, model, inputValidator),
@@ -465,6 +473,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
465473

466474
createManyAndReturn: (args: unknown) => {
467475
return createPromise(
476+
'createManyAndReturn',
468477
'createManyAndReturn',
469478
args,
470479
new CreateOperationHandler<Schema>(client, model, inputValidator),
@@ -474,6 +483,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
474483

475484
update: (args: unknown) => {
476485
return createPromise(
486+
'update',
477487
'update',
478488
args,
479489
new UpdateOperationHandler<Schema>(client, model, inputValidator),
@@ -483,6 +493,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
483493

484494
updateMany: (args: unknown) => {
485495
return createPromise(
496+
'updateMany',
486497
'updateMany',
487498
args,
488499
new UpdateOperationHandler<Schema>(client, model, inputValidator),
@@ -492,6 +503,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
492503

493504
updateManyAndReturn: (args: unknown) => {
494505
return createPromise(
506+
'updateManyAndReturn',
495507
'updateManyAndReturn',
496508
args,
497509
new UpdateOperationHandler<Schema>(client, model, inputValidator),
@@ -501,6 +513,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
501513

502514
upsert: (args: unknown) => {
503515
return createPromise(
516+
'upsert',
504517
'upsert',
505518
args,
506519
new UpdateOperationHandler<Schema>(client, model, inputValidator),
@@ -510,6 +523,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
510523

511524
delete: (args: unknown) => {
512525
return createPromise(
526+
'delete',
513527
'delete',
514528
args,
515529
new DeleteOperationHandler<Schema>(client, model, inputValidator),
@@ -519,6 +533,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
519533

520534
deleteMany: (args: unknown) => {
521535
return createPromise(
536+
'deleteMany',
522537
'deleteMany',
523538
args,
524539
new DeleteOperationHandler<Schema>(client, model, inputValidator),
@@ -528,6 +543,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
528543

529544
count: (args: unknown) => {
530545
return createPromise(
546+
'count',
531547
'count',
532548
args,
533549
new CountOperationHandler<Schema>(client, model, inputValidator),
@@ -537,6 +553,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
537553

538554
aggregate: (args: unknown) => {
539555
return createPromise(
556+
'aggregate',
540557
'aggregate',
541558
args,
542559
new AggregateOperationHandler<Schema>(client, model, inputValidator),
@@ -546,6 +563,7 @@ function createModelCrudHandler<Schema extends SchemaDef, Model extends GetModel
546563

547564
groupBy: (args: unknown) => {
548565
return createPromise(
566+
'groupBy',
549567
'groupBy',
550568
args,
551569
new GroupByOperationHandler<Schema>(client, model, inputValidator),

packages/runtime/src/client/crud/operations/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { getCrudDialect } from '../dialects';
5151
import type { BaseCrudDialect } from '../dialects/base';
5252
import { InputValidator } from '../validator';
5353

54-
export type CrudOperation =
54+
export type CoreCrudOperation =
5555
| 'findMany'
5656
| 'findUnique'
5757
| 'findFirst'
@@ -68,7 +68,7 @@ export type CrudOperation =
6868
| 'aggregate'
6969
| 'groupBy';
7070

71-
export type AllCrudOperation = CrudOperation | 'findUniqueOrThrow' | 'findFirstOrThrow';
71+
export type AllCrudOperation = CoreCrudOperation | 'findUniqueOrThrow' | 'findFirstOrThrow';
7272

7373
export type FromRelationContext<Schema extends SchemaDef> = {
7474
model: GetModels<Schema>;
@@ -99,7 +99,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
9999
return this.client.$qb;
100100
}
101101

102-
abstract handle(operation: CrudOperation, args: any): Promise<unknown>;
102+
abstract handle(operation: CoreCrudOperation, args: any): Promise<unknown>;
103103

104104
withClient(client: ClientContract<Schema>) {
105105
return new (this.constructor as new (...args: any[]) => this)(client, this.model, this.inputValidator);

packages/runtime/src/client/crud/operations/find.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { GetModels, SchemaDef } from '../../../schema';
22
import type { FindArgs } from '../../crud-types';
3-
import { BaseOperationHandler, type CrudOperation } from './base';
3+
import { BaseOperationHandler, type CoreCrudOperation } from './base';
44

55
export class FindOperationHandler<Schema extends SchemaDef> extends BaseOperationHandler<Schema> {
6-
async handle(operation: CrudOperation, args: unknown, validateArgs = true): Promise<unknown> {
6+
async handle(operation: CoreCrudOperation, args: unknown, validateArgs = true): Promise<unknown> {
77
// normalize args to strip `undefined` fields
88
const normalizedArgs = this.normalizeArgs(args);
99

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

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -627,30 +627,32 @@ export class InputValidator<Schema extends SchemaDef> {
627627
}
628628

629629
private makeRelationSelectIncludeSchema(fieldDef: FieldDef) {
630-
return z.union([
631-
z.boolean(),
632-
z.strictObject({
633-
...(fieldDef.array || fieldDef.optional
634-
? {
635-
// to-many relations and optional to-one relations are filterable
636-
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
637-
}
638-
: {}),
639-
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
640-
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
641-
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
642-
...(fieldDef.array
643-
? {
644-
// to-many relations can be ordered, skipped, taken, and cursor-located
645-
orderBy: z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
646-
skip: this.makeSkipSchema().optional(),
647-
take: this.makeTakeSchema().optional(),
648-
cursor: this.makeCursorSchema(fieldDef.type).optional(),
649-
distinct: this.makeDistinctSchema(fieldDef.type).optional(),
650-
}
651-
: {}),
652-
}),
653-
]);
630+
let objSchema: z.ZodType = z.strictObject({
631+
...(fieldDef.array || fieldDef.optional
632+
? {
633+
// to-many relations and optional to-one relations are filterable
634+
where: z.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional(),
635+
}
636+
: {}),
637+
select: z.lazy(() => this.makeSelectSchema(fieldDef.type)).optional(),
638+
include: z.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional(),
639+
omit: z.lazy(() => this.makeOmitSchema(fieldDef.type)).optional(),
640+
...(fieldDef.array
641+
? {
642+
// to-many relations can be ordered, skipped, taken, and cursor-located
643+
orderBy: z.lazy(() => this.makeOrderBySchema(fieldDef.type, true, false)).optional(),
644+
skip: this.makeSkipSchema().optional(),
645+
take: this.makeTakeSchema().optional(),
646+
cursor: this.makeCursorSchema(fieldDef.type).optional(),
647+
distinct: this.makeDistinctSchema(fieldDef.type).optional(),
648+
}
649+
: {}),
650+
});
651+
652+
objSchema = this.refineForSelectIncludeMutuallyExclusive(objSchema);
653+
objSchema = this.refineForSelectOmitMutuallyExclusive(objSchema);
654+
655+
return z.union([z.boolean(), objSchema]);
654656
}
655657

656658
private makeOmitSchema(model: string) {
@@ -742,7 +744,7 @@ export class InputValidator<Schema extends SchemaDef> {
742744

743745
private makeCreateSchema(model: string) {
744746
const dataSchema = this.makeCreateDataSchema(model, false);
745-
const schema = z.object({
747+
const schema = z.strictObject({
746748
data: dataSchema,
747749
select: this.makeSelectSchema(model).optional(),
748750
include: this.makeIncludeSchema(model).optional(),
@@ -757,12 +759,10 @@ export class InputValidator<Schema extends SchemaDef> {
757759

758760
private makeCreateManyAndReturnSchema(model: string) {
759761
const base = this.makeCreateManyDataSchema(model, []);
760-
const result = base.merge(
761-
z.strictObject({
762-
select: this.makeSelectSchema(model).optional(),
763-
omit: this.makeOmitSchema(model).optional(),
764-
}),
765-
);
762+
const result = base.extend({
763+
select: this.makeSelectSchema(model).optional(),
764+
omit: this.makeOmitSchema(model).optional(),
765+
});
766766
return this.refineForSelectOmitMutuallyExclusive(result).optional();
767767
}
768768

@@ -986,7 +986,7 @@ export class InputValidator<Schema extends SchemaDef> {
986986
const whereSchema = this.makeWhereSchema(model, true);
987987
const createSchema = this.makeCreateDataSchema(model, false, withoutFields);
988988
return this.orArray(
989-
z.object({
989+
z.strictObject({
990990
where: whereSchema,
991991
create: createSchema,
992992
}),
@@ -995,7 +995,7 @@ export class InputValidator<Schema extends SchemaDef> {
995995
}
996996

997997
private makeCreateManyDataSchema(model: string, withoutFields: string[]) {
998-
return z.object({
998+
return z.strictObject({
999999
data: this.makeCreateDataSchema(model, true, withoutFields, true),
10001000
skipDuplicates: z.boolean().optional(),
10011001
});
@@ -1006,7 +1006,7 @@ export class InputValidator<Schema extends SchemaDef> {
10061006
// #region Update
10071007

10081008
private makeUpdateSchema(model: string) {
1009-
const schema = z.object({
1009+
const schema = z.strictObject({
10101010
where: this.makeWhereSchema(model, true),
10111011
data: this.makeUpdateDataSchema(model),
10121012
select: this.makeSelectSchema(model).optional(),
@@ -1017,7 +1017,7 @@ export class InputValidator<Schema extends SchemaDef> {
10171017
}
10181018

10191019
private makeUpdateManySchema(model: string) {
1020-
return z.object({
1020+
return z.strictObject({
10211021
where: this.makeWhereSchema(model, false).optional(),
10221022
data: this.makeUpdateDataSchema(model, [], true),
10231023
limit: z.int().nonnegative().optional(),
@@ -1026,17 +1026,15 @@ export class InputValidator<Schema extends SchemaDef> {
10261026

10271027
private makeUpdateManyAndReturnSchema(model: string) {
10281028
const base = this.makeUpdateManySchema(model);
1029-
const result = base.merge(
1030-
z.strictObject({
1031-
select: this.makeSelectSchema(model).optional(),
1032-
omit: this.makeOmitSchema(model).optional(),
1033-
}),
1034-
);
1029+
const result = base.extend({
1030+
select: this.makeSelectSchema(model).optional(),
1031+
omit: this.makeOmitSchema(model).optional(),
1032+
});
10351033
return this.refineForSelectOmitMutuallyExclusive(result);
10361034
}
10371035

10381036
private makeUpsertSchema(model: string) {
1039-
const schema = z.object({
1037+
const schema = z.strictObject({
10401038
where: this.makeWhereSchema(model, true),
10411039
create: this.makeCreateDataSchema(model, false),
10421040
update: this.makeUpdateDataSchema(model),
@@ -1148,7 +1146,7 @@ export class InputValidator<Schema extends SchemaDef> {
11481146
// #region Delete
11491147

11501148
private makeDeleteSchema(model: GetModels<Schema>) {
1151-
const schema = z.object({
1149+
const schema = z.strictObject({
11521150
where: this.makeWhereSchema(model, true),
11531151
select: this.makeSelectSchema(model).optional(),
11541152
include: this.makeIncludeSchema(model).optional(),
@@ -1187,7 +1185,7 @@ export class InputValidator<Schema extends SchemaDef> {
11871185
const modelDef = requireModel(this.schema, model);
11881186
return z.union([
11891187
z.literal(true),
1190-
z.object({
1188+
z.strictObject({
11911189
_all: z.literal(true).optional(),
11921190
...Object.keys(modelDef.fields).reduce(
11931191
(acc, field) => {
@@ -1257,7 +1255,7 @@ export class InputValidator<Schema extends SchemaDef> {
12571255
const modelDef = requireModel(this.schema, model);
12581256
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
12591257

1260-
let schema = z.object({
1258+
let schema = z.strictObject({
12611259
where: this.makeWhereSchema(model, false).optional(),
12621260
orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
12631261
by: this.orArray(z.enum(nonRelationFields), true),

packages/runtime/src/client/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function definePlugin<Schema extends SchemaDef>(plugin: RuntimePlugin<Sch
4646
return plugin;
4747
}
4848

49-
export { type CrudOperation } from './crud/operations/base';
49+
export { type CoreCrudOperation as CrudOperation } from './crud/operations/base';
5050

5151
// #region OnQuery hooks
5252

packages/zod/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function makeSelectSchema<Schema extends SchemaDef, Model extends GetMode
77
schema: Schema,
88
model: Model,
99
) {
10-
return z.object(mapFields(schema, model)) as SelectSchema<Schema, typeof model>;
10+
return z.strictObject(mapFields(schema, model)) as SelectSchema<Schema, typeof model>;
1111
}
1212

1313
function mapFields<Schema extends SchemaDef>(schema: Schema, model: GetModels<Schema>): any {

0 commit comments

Comments
 (0)