Skip to content

Commit 319e207

Browse files
fix(orm): enforce field validation attributes and @@validate inside custom types
The ORM built the input schema of a typed JSON field from the custom type's field types only. Validation attributes on the type's fields (`@length`, `@gte`, `@email`, ...) and the type's own `@@validate` rules were dropped, so create, update and procedure calls accepted values that violate them, while `@zenstackhq/zod`'s `makeTypeSchema` enforces the same rules. `makeTypeDefSchema` now passes each field's attributes to `makeScalarSchema`, applies `@length` to list fields with `addListValidation`, and applies the type's `@@validate` rules with `addCustomValidation`, the same way model fields and primitive custom types are handled. Fixes #2844 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ee71982 commit 319e207

2 files changed

Lines changed: 106 additions & 7 deletions

File tree

‎packages/orm/src/client/zod/factory.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,16 +483,18 @@ export class ZodSchemaFactory<
483483
return this.extraValidationsEnabled ? ZodUtils.addCustomValidation(schema, typeDef.attributes) : schema;
484484
}
485485
const func = typeDef.strict ? z.strictObject : z.looseObject;
486-
const schema = func(
486+
const objectSchema = func(
487487
Object.fromEntries(
488488
Object.entries(typeDef.fields).map(([field, def]) => {
489-
// Wrap nested typedef references in z.lazy() so cyclic or self-referencing
490-
// typedefs don't recurse infinitely while building schemas.
491-
let fieldSchema: ZodType = isTypeDef(this.schema, def.type)
492-
? z.lazy(() => this.makeTypeDefSchema(def.type))
493-
: this.makeScalarSchema(def.type);
489+
// Wrap nested object typedef references in z.lazy() so cyclic or self-referencing
490+
// typedefs don't recurse infinitely while building schemas. All other fields get
491+
// their validation attributes applied, the same as model fields.
492+
let fieldSchema: ZodType =
493+
isTypeDef(this.schema, def.type) && !isPrimitiveTypeDef(this.schema, def.type)
494+
? z.lazy(() => this.makeTypeDefSchema(def.type))
495+
: this.makeScalarSchema(def.type, this.getItemAttributes(def));
494496
if (def.array) {
495-
fieldSchema = fieldSchema.array();
497+
fieldSchema = ZodUtils.addListValidation(fieldSchema.array(), def.attributes);
496498
}
497499
if (def.optional) {
498500
fieldSchema = fieldSchema.nullish();
@@ -501,6 +503,9 @@ export class ZodSchemaFactory<
501503
}),
502504
),
503505
);
506+
const schema = this.extraValidationsEnabled
507+
? ZodUtils.addCustomValidation(objectSchema, typeDef.attributes)
508+
: objectSchema;
504509

505510
// zod doesn't preserve object field order after parsing, here we use a
506511
// validation-only custom schema and use the original data if parsing
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
// https://github.com/zenstackhq/zenstack/issues/2844
5+
describe('Regression for issue 2844', () => {
6+
const schema = `
7+
type Bounds {
8+
caption String @length(1, 3)
9+
low Int @gte(0)
10+
high Int
11+
12+
@@validate(low <= high, 'low must not exceed high')
13+
}
14+
15+
model Example {
16+
id Int @id
17+
bounds Bounds @json
18+
}
19+
`;
20+
21+
it('enforces custom type validation rules on create', async () => {
22+
const db = await createTestClient(schema);
23+
24+
await expect(
25+
db.example.create({ data: { id: 1, bounds: { caption: 'too long', low: 0, high: 1 } } }),
26+
).toBeRejectedByValidation(['data.bounds.caption']);
27+
await expect(
28+
db.example.create({ data: { id: 1, bounds: { caption: 'ok', low: -1, high: 1 } } }),
29+
).toBeRejectedByValidation(['data.bounds.low']);
30+
await expect(
31+
db.example.create({ data: { id: 1, bounds: { caption: 'ok', low: 5, high: 1 } } }),
32+
).toBeRejectedByValidation(['low must not exceed high']);
33+
34+
await expect(
35+
db.example.create({ data: { id: 1, bounds: { caption: 'abc', low: 0, high: 0 } } }),
36+
).resolves.toMatchObject({ bounds: { caption: 'abc', low: 0, high: 0 } });
37+
});
38+
39+
it('enforces custom type validation rules on update', async () => {
40+
const db = await createTestClient(schema);
41+
await db.example.create({ data: { id: 1, bounds: { caption: 'ok', low: 0, high: 1 } } });
42+
43+
await expect(
44+
db.example.update({ where: { id: 1 }, data: { bounds: { caption: 'too long', low: 0, high: 1 } } }),
45+
).toBeRejectedByValidation(['data.bounds.caption']);
46+
await expect(
47+
db.example.update({ where: { id: 1 }, data: { bounds: { caption: 'ok', low: -1, high: 1 } } }),
48+
).toBeRejectedByValidation(['data.bounds.low']);
49+
await expect(
50+
db.example.update({ where: { id: 1 }, data: { bounds: { caption: 'ok', low: 5, high: 1 } } }),
51+
).toBeRejectedByValidation(['low must not exceed high']);
52+
53+
await expect(db.example.findUnique({ where: { id: 1 } })).resolves.toMatchObject({
54+
bounds: { caption: 'ok', low: 0, high: 1 },
55+
});
56+
});
57+
58+
it('enforces validation rules of nested custom types and list fields', async () => {
59+
const db = await createTestClient(`
60+
type Item {
61+
name String @length(1, 5)
62+
}
63+
64+
type Cart {
65+
owner String @email
66+
items Item[] @length(1, 2)
67+
}
68+
69+
model Order {
70+
id Int @id
71+
cart Cart @json
72+
}
73+
`);
74+
75+
await expect(
76+
db.order.create({ data: { id: 1, cart: { owner: 'not-an-email', items: [{ name: 'a' }] } } }),
77+
).toBeRejectedByValidation(['data.cart.owner']);
78+
await expect(
79+
db.order.create({ data: { id: 1, cart: { owner: '[email protected]', items: [{ name: 'too long' }] } } }),
80+
).toBeRejectedByValidation(['data.cart.items[0].name']);
81+
await expect(
82+
db.order.create({ data: { id: 1, cart: { owner: '[email protected]', items: [] } } }),
83+
).toBeRejectedByValidation(['data.cart.items']);
84+
await expect(
85+
db.order.create({
86+
data: { id: 1, cart: { owner: '[email protected]', items: [{ name: 'a' }, { name: 'b' }, { name: 'c' }] } },
87+
}),
88+
).toBeRejectedByValidation(['data.cart.items']);
89+
90+
await expect(
91+
db.order.create({ data: { id: 1, cart: { owner: '[email protected]', items: [{ name: 'apple' }] } } }),
92+
).resolves.toMatchObject({ cart: { owner: '[email protected]', items: [{ name: 'apple' }] } });
93+
});
94+
});

0 commit comments

Comments
 (0)