Skip to content

Commit 3cbf004

Browse files
committed
fix "@@Validate"
1 parent 7b06d4b commit 3cbf004

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,12 @@ export class InputValidator<Schema extends SchemaDef> {
936936
}
937937
});
938938

939-
const uncheckedCreateSchema = addCustomValidation(z.strictObject(uncheckedVariantFields), modelDef.attributes);
940-
const checkedCreateSchema = addCustomValidation(z.strictObject(checkedVariantFields), modelDef.attributes);
939+
const uncheckedCreateSchema = this.extraValidationsEnabled
940+
? addCustomValidation(z.strictObject(uncheckedVariantFields), modelDef.attributes)
941+
: z.strictObject(uncheckedVariantFields);
942+
const checkedCreateSchema = this.extraValidationsEnabled
943+
? addCustomValidation(z.strictObject(checkedVariantFields), modelDef.attributes)
944+
: z.strictObject(checkedVariantFields);
941945

942946
if (!hasRelation) {
943947
return this.orArray(uncheckedCreateSchema, canBeArray);
@@ -1216,8 +1220,12 @@ export class InputValidator<Schema extends SchemaDef> {
12161220
}
12171221
});
12181222

1219-
const uncheckedUpdateSchema = addCustomValidation(z.strictObject(uncheckedVariantFields), modelDef.attributes);
1220-
const checkedUpdateSchema = addCustomValidation(z.strictObject(checkedVariantFields), modelDef.attributes);
1223+
const uncheckedUpdateSchema = this.extraValidationsEnabled
1224+
? addCustomValidation(z.strictObject(uncheckedVariantFields), modelDef.attributes)
1225+
: z.strictObject(uncheckedVariantFields);
1226+
const checkedUpdateSchema = this.extraValidationsEnabled
1227+
? addCustomValidation(z.strictObject(checkedVariantFields), modelDef.attributes)
1228+
: z.strictObject(checkedVariantFields);
12211229
if (!hasRelation) {
12221230
return uncheckedUpdateSchema;
12231231
} else {

tests/e2e/orm/validation/custom-validation.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe('Custom validation tests', () => {
115115
model User {
116116
id Int @id @default(autoincrement())
117117
email String @unique @email
118+
@@validate(length(email, 8))
118119
@@allow('all', true)
119120
}
120121
`,
@@ -127,6 +128,13 @@ describe('Custom validation tests', () => {
127128
},
128129
}),
129130
).toBeRejectedByValidation();
131+
await expect(
132+
db.user.create({
133+
data: {
134+
135+
},
136+
}),
137+
).toBeRejectedByValidation();
130138

131139
await expect(
132140
db.$setInputValidation(false).user.create({
@@ -141,7 +149,7 @@ describe('Custom validation tests', () => {
141149
db.$setInputValidation(false).user.update({
142150
where: { id: 1 },
143151
data: {
144-
email: 'abc',
152+
145153
},
146154
}),
147155
).toResolveTruthy();
@@ -154,5 +162,12 @@ describe('Custom validation tests', () => {
154162
},
155163
}),
156164
).toBeRejectedByValidation();
165+
await expect(
166+
db.user.create({
167+
data: {
168+
169+
},
170+
}),
171+
).toBeRejectedByValidation();
157172
});
158173
});

0 commit comments

Comments
 (0)