Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 1a038be

Browse files
committed
update
1 parent 291833b commit 1a038be

1 file changed

Lines changed: 23 additions & 34 deletions

File tree

  • packages/orm/src/client/crud/validator

packages/orm/src/client/crud/validator/index.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ import {
6565

6666
type GetSchemaFunc<Schema extends SchemaDef> = (model: GetModels<Schema>) => ZodType;
6767

68+
/**
69+
* Helper decorator that caches schema builders with class's state included
70+
* as part of the key (here the `extraValidationsEnabled` property).
71+
*/
72+
const cacheWithState = () => cache({ includeProperties: ['extraValidationsEnabled'] });
73+
6874
export class InputValidator<Schema extends SchemaDef> {
6975
private readonly schemaCache = new Map<string, ZodType>();
7076

@@ -312,25 +318,8 @@ export class InputValidator<Schema extends SchemaDef> {
312318
// #region Validation helpers
313319

314320
private validate<T>(model: GetModels<Schema>, operation: string, getSchema: GetSchemaFunc<Schema>, args: unknown) {
315-
// const schema = this.cached(
316-
// {
317-
// type: 'model',
318-
// model,
319-
// operation,
320-
// extraValidationsEnabled: this.extraValidationsEnabled,
321-
// },
322-
// () => getSchema(model),
323-
// );
324-
325321
const schema = getSchema(model);
326-
327322
const { error, data } = schema.safeParse(args);
328-
329-
// const start1 = new Date();
330-
// schema.safeParse(args);
331-
// const took1 = new Date().getTime() - start1.getTime();
332-
// console.log('Validation took:', took1);
333-
334323
if (error) {
335324
throw createInvalidInputError(
336325
`Invalid ${operation} args for model "${model}": ${formatError(error)}`,
@@ -431,7 +420,7 @@ export class InputValidator<Schema extends SchemaDef> {
431420

432421
// #region Find
433422

434-
@cache()
423+
@cacheWithState()
435424
private makeFindSchema(model: string, operation: CoreCrudOperations) {
436425
const fields: Record<string, z.ZodSchema> = {};
437426
const unique = operation === 'findUnique';
@@ -470,7 +459,7 @@ export class InputValidator<Schema extends SchemaDef> {
470459
return result;
471460
}
472461

473-
@cache()
462+
@cacheWithState()
474463
private makeExistsSchema(model: string) {
475464
const baseSchema = z.strictObject({
476465
where: this.makeWhereSchema(model, false).optional(),
@@ -524,7 +513,7 @@ export class InputValidator<Schema extends SchemaDef> {
524513
return z.enum(Object.keys(enumDef.values) as [string, ...string[]]);
525514
}
526515

527-
@cache({ includeProperties: ['extraValidationsEnabled'] })
516+
@cacheWithState()
528517
private makeTypeDefSchema(type: string): z.ZodType {
529518
const typeDef = getTypeDef(this.schema, type);
530519
invariant(typeDef, `Type definition "${type}" not found in schema`);
@@ -1210,7 +1199,7 @@ export class InputValidator<Schema extends SchemaDef> {
12101199

12111200
// #region Create
12121201

1213-
@cache()
1202+
@cacheWithState()
12141203
private makeCreateSchema(model: string) {
12151204
const dataSchema = this.makeCreateDataSchema(model, false);
12161205
const baseSchema = z.strictObject({
@@ -1225,12 +1214,12 @@ export class InputValidator<Schema extends SchemaDef> {
12251214
return schema;
12261215
}
12271216

1228-
@cache()
1217+
@cacheWithState()
12291218
private makeCreateManySchema(model: string) {
12301219
return this.mergePluginArgsSchema(this.makeCreateManyDataSchema(model, []), 'createMany').optional();
12311220
}
12321221

1233-
@cache()
1222+
@cacheWithState()
12341223
private makeCreateManyAndReturnSchema(model: string) {
12351224
const base = this.makeCreateManyDataSchema(model, []);
12361225
let result: ZodObject = base.extend({
@@ -1241,7 +1230,7 @@ export class InputValidator<Schema extends SchemaDef> {
12411230
return this.refineForSelectOmitMutuallyExclusive(result).optional();
12421231
}
12431232

1244-
@cache({ includeProperties: ['extraValidationsEnabled'] })
1233+
@cacheWithState()
12451234
private makeCreateDataSchema(
12461235
model: string,
12471236
canBeArray: boolean,
@@ -1518,7 +1507,7 @@ export class InputValidator<Schema extends SchemaDef> {
15181507

15191508
// #region Update
15201509

1521-
@cache()
1510+
@cacheWithState()
15221511
private makeUpdateSchema(model: string) {
15231512
const baseSchema = z.strictObject({
15241513
where: this.makeWhereSchema(model, true),
@@ -1533,7 +1522,7 @@ export class InputValidator<Schema extends SchemaDef> {
15331522
return schema;
15341523
}
15351524

1536-
@cache()
1525+
@cacheWithState()
15371526
private makeUpdateManySchema(model: string) {
15381527
return this.mergePluginArgsSchema(
15391528
z.strictObject({
@@ -1545,7 +1534,7 @@ export class InputValidator<Schema extends SchemaDef> {
15451534
);
15461535
}
15471536

1548-
@cache()
1537+
@cacheWithState()
15491538
private makeUpdateManyAndReturnSchema(model: string) {
15501539
// plugin extended args schema is merged in `makeUpdateManySchema`
15511540
const baseSchema: ZodObject = this.makeUpdateManySchema(model);
@@ -1557,7 +1546,7 @@ export class InputValidator<Schema extends SchemaDef> {
15571546
return schema;
15581547
}
15591548

1560-
@cache()
1549+
@cacheWithState()
15611550
private makeUpsertSchema(model: string) {
15621551
const baseSchema = z.strictObject({
15631552
where: this.makeWhereSchema(model, true),
@@ -1573,7 +1562,7 @@ export class InputValidator<Schema extends SchemaDef> {
15731562
return schema;
15741563
}
15751564

1576-
@cache({ includeProperties: ['extraValidationsEnabled'] })
1565+
@cacheWithState()
15771566
private makeUpdateDataSchema(model: string, withoutFields: string[] = [], withoutRelationFields = false) {
15781567
// Normalize array argument for consistent cache keys
15791568
withoutFields = [...withoutFields].sort();
@@ -1687,7 +1676,7 @@ export class InputValidator<Schema extends SchemaDef> {
16871676

16881677
// #region Delete
16891678

1690-
@cache()
1679+
@cacheWithState()
16911680
private makeDeleteSchema(model: GetModels<Schema>) {
16921681
const baseSchema = z.strictObject({
16931682
where: this.makeWhereSchema(model, true),
@@ -1701,7 +1690,7 @@ export class InputValidator<Schema extends SchemaDef> {
17011690
return schema;
17021691
}
17031692

1704-
@cache()
1693+
@cacheWithState()
17051694
private makeDeleteManySchema(model: GetModels<Schema>) {
17061695
return this.mergePluginArgsSchema(
17071696
z.strictObject({
@@ -1716,7 +1705,7 @@ export class InputValidator<Schema extends SchemaDef> {
17161705

17171706
// #region Count
17181707

1719-
@cache()
1708+
@cacheWithState()
17201709
makeCountSchema(model: GetModels<Schema>) {
17211710
return this.mergePluginArgsSchema(
17221711
z.strictObject({
@@ -1752,7 +1741,7 @@ export class InputValidator<Schema extends SchemaDef> {
17521741

17531742
// #region Aggregate
17541743

1755-
@cache()
1744+
@cacheWithState()
17561745
makeAggregateSchema(model: GetModels<Schema>) {
17571746
return this.mergePluginArgsSchema(
17581747
z.strictObject({
@@ -1804,7 +1793,7 @@ export class InputValidator<Schema extends SchemaDef> {
18041793
);
18051794
}
18061795

1807-
@cache()
1796+
@cacheWithState()
18081797
private makeGroupBySchema(model: GetModels<Schema>) {
18091798
const modelDef = requireModel(this.schema, model);
18101799
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);

0 commit comments

Comments
 (0)