Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"toposort": "^2.0.2",
"ts-pattern": "catalog:",
"ulid": "^3.0.0",
"uuid": "^11.0.5"
"uuid": "^11.0.5",
"zod-validation-error": "catalog:"
},
"peerDependencies": {
"better-sqlite3": "^12.2.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/src/client/crud/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { z, ZodType } from 'zod';
import { type BuiltinType, type EnumDef, type FieldDef, type GetModels, type SchemaDef } from '../../schema';
import { enumerate } from '../../utils/enumerate';
import { extractFields } from '../../utils/object-utils';
import { formatError } from '../../utils/zod-utils';
import { AGGREGATE_OPERATORS, LOGICAL_COMBINATORS, NUMERIC_FIELD_TYPES } from '../constants';
import {
type AggregateArgs,
Expand Down Expand Up @@ -185,7 +186,7 @@ export class InputValidator<Schema extends SchemaDef> {
}
const { error } = schema.safeParse(args);
if (error) {
throw new InputValidationError(`Invalid ${operation} args: ${error.message}`, error);
throw new InputValidationError(`Invalid ${operation} args: ${formatError(error)}`, error);
}
return args as T;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/runtime/src/plugins/policy/expression-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,12 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
.map((f) => f.name);
invariant(idFields.length > 0, 'auth type model must have at least one id field');

// convert `auth() == other` into `auth().id == other.id`
const conditions = idFields.map((fieldName) =>
ExpressionUtils.binary(
ExpressionUtils.member(authExpr, [fieldName]),
'==',
ExpressionUtils.member(other, [fieldName]),
this.makeOrAppendMember(other, fieldName),
),
);
let result = this.buildAnd(conditions);
Expand All @@ -296,6 +297,14 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
}
}

private makeOrAppendMember(other: Expression, fieldName: string): Expression {
if (ExpressionUtils.isMember(other)) {
return ExpressionUtils.member(other.receiver, [...other.members, fieldName]);
} else {
return ExpressionUtils.member(other, [fieldName]);
}
}

private transformValue(value: unknown, type: BuiltinType) {
return ValueNode.create(this.dialect.transformPrimitive(value, type, false) ?? null);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/runtime/src/utils/zod-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ZodError } from 'zod';
import { fromError as fromError3 } from 'zod-validation-error/v3';
import { fromError as fromError4 } from 'zod-validation-error/v4';

/**
* Format ZodError into a readable string
*/
export function formatError(error: ZodError): string {
if ('_zod' in error) {
return fromError4(error).toString();
} else {
return fromError3(error).toString();
}
}
4 changes: 2 additions & 2 deletions packages/runtime/test/client-api/group-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe.each(createClientSpecs(PG_DB_NAME))('Client groupBy tests', ({ createCl
age: 10,
},
}),
).rejects.toThrow(/must be in \\"by\\"/);
).rejects.toThrow(/must be in "by"/);
});

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