Skip to content

Commit a7b9ad3

Browse files
authored
fix: format zod errors, issue with "this" member access (#250)
* fix: format zod errors, issue with "this" member access * update tests
1 parent 966018f commit a7b9ad3

File tree

9 files changed

+424
-10
lines changed

9 files changed

+424
-10
lines changed

packages/runtime/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"toposort": "^2.0.2",
7474
"ts-pattern": "catalog:",
7575
"ulid": "^3.0.0",
76-
"uuid": "^11.0.5"
76+
"uuid": "^11.0.5",
77+
"zod-validation-error": "catalog:"
7778
},
7879
"peerDependencies": {
7980
"better-sqlite3": "^12.2.0",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { z, ZodType } from 'zod';
66
import { type BuiltinType, type EnumDef, type FieldDef, type GetModels, type SchemaDef } from '../../schema';
77
import { enumerate } from '../../utils/enumerate';
88
import { extractFields } from '../../utils/object-utils';
9+
import { formatError } from '../../utils/zod-utils';
910
import { AGGREGATE_OPERATORS, LOGICAL_COMBINATORS, NUMERIC_FIELD_TYPES } from '../constants';
1011
import {
1112
type AggregateArgs,
@@ -185,7 +186,7 @@ export class InputValidator<Schema extends SchemaDef> {
185186
}
186187
const { error } = schema.safeParse(args);
187188
if (error) {
188-
throw new InputValidationError(`Invalid ${operation} args: ${error.message}`, error);
189+
throw new InputValidationError(`Invalid ${operation} args: ${formatError(error)}`, error);
189190
}
190191
return args as T;
191192
}

packages/runtime/src/plugins/policy/expression-transformer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,12 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
281281
.map((f) => f.name);
282282
invariant(idFields.length > 0, 'auth type model must have at least one id field');
283283

284+
// convert `auth() == other` into `auth().id == other.id`
284285
const conditions = idFields.map((fieldName) =>
285286
ExpressionUtils.binary(
286287
ExpressionUtils.member(authExpr, [fieldName]),
287288
'==',
288-
ExpressionUtils.member(other, [fieldName]),
289+
this.makeOrAppendMember(other, fieldName),
289290
),
290291
);
291292
let result = this.buildAnd(conditions);
@@ -296,6 +297,14 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
296297
}
297298
}
298299

300+
private makeOrAppendMember(other: Expression, fieldName: string): Expression {
301+
if (ExpressionUtils.isMember(other)) {
302+
return ExpressionUtils.member(other.receiver, [...other.members, fieldName]);
303+
} else {
304+
return ExpressionUtils.member(other, [fieldName]);
305+
}
306+
}
307+
299308
private transformValue(value: unknown, type: BuiltinType) {
300309
return ValueNode.create(this.dialect.transformPrimitive(value, type, false) ?? null);
301310
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ZodError } from 'zod';
2+
import { fromError as fromError3 } from 'zod-validation-error/v3';
3+
import { fromError as fromError4 } from 'zod-validation-error/v4';
4+
5+
/**
6+
* Format ZodError into a readable string
7+
*/
8+
export function formatError(error: ZodError): string {
9+
if ('_zod' in error) {
10+
return fromError4(error).toString();
11+
} else {
12+
return fromError3(error).toString();
13+
}
14+
}

packages/runtime/test/client-api/group-by.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client groupBy tests', ({ createCl
269269
age: 10,
270270
},
271271
}),
272-
).rejects.toThrow(/must be in \\"by\\"/);
272+
).rejects.toThrow(/must be in "by"/);
273273
});
274274

275275
it('complains about fields in orderBy that are not in by', async () => {
@@ -280,6 +280,6 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client groupBy tests', ({ createCl
280280
age: 'asc',
281281
},
282282
}),
283-
).rejects.toThrow(/must be in \\"by\\"/);
283+
).rejects.toThrow(/must be in "by"/);
284284
});
285285
});

0 commit comments

Comments
 (0)