Skip to content

Commit a820bdd

Browse files
authored
Merge pull request #565 from zenstackhq/fix/issue-558
fix(orm): preserve zod validation errors when validating custom json types
2 parents de795f5 + b625c50 commit a820bdd

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,13 @@ export class InputValidator<Schema extends SchemaDef> {
382382
// zod doesn't preserve object field order after parsing, here we use a
383383
// validation-only custom schema and use the original data if parsing
384384
// is successful
385-
const finalSchema = z.custom((v) => {
386-
return schema.safeParse(v).success;
385+
const finalSchema = z.any().superRefine((value, ctx) => {
386+
const parseResult = schema.safeParse(value);
387+
if (!parseResult.success) {
388+
parseResult.error.issues.forEach((issue) => ctx.addIssue(issue as any));
389+
}
387390
});
391+
388392
this.setSchemaCache(key!, finalSchema);
389393
return finalSchema;
390394
}
@@ -495,7 +499,7 @@ export class InputValidator<Schema extends SchemaDef> {
495499
}
496500

497501
// expression builder
498-
fields['$expr'] = z.custom((v) => typeof v === 'function').optional();
502+
fields['$expr'] = z.custom((v) => typeof v === 'function', { error: '"$expr" must be a function' }).optional();
499503

500504
// logical operators
501505
fields['AND'] = this.orArray(

tests/e2e/orm/client-api/typed-json-fields.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ model User {
121121
},
122122
},
123123
}),
124-
).rejects.toThrow(/invalid/i);
124+
).rejects.toThrow('data.identity.providers[0].id');
125125
});
126126

127127
it('works with find', async () => {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Regression for issue #558', () => {
5+
it('verifies issue 558', async () => {
6+
const db = await createTestClient(`
7+
type Foo {
8+
x Int
9+
}
10+
11+
model Model {
12+
id String @id @default(cuid())
13+
foo Foo @json
14+
}
15+
`);
16+
17+
await expect(db.model.create({ data: { foo: { x: 'hello' } } })).rejects.toThrow('data.foo.x');
18+
});
19+
});

0 commit comments

Comments
 (0)