Skip to content

Commit 76ce4f4

Browse files
committed
additional fixes
- use zod schema that contains only non-relation fields to validate mutation payload - make sure id fields are always included in the base zod schema (even if they're also FK fields)
1 parent 6a101f3 commit 76ce4f4

File tree

2 files changed

+7
-27
lines changed

2 files changed

+7
-27
lines changed

packages/schema/src/plugins/zod/generator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isEnumFieldReference,
1111
isForeignKeyField,
1212
isFromStdlib,
13+
isIdField,
1314
parseOptionAsStrings,
1415
resolvePath,
1516
} from '@zenstackhq/sdk';
@@ -291,8 +292,10 @@ export class ZodSchemaGenerator {
291292
sf.replaceWithText((writer) => {
292293
const scalarFields = model.fields.filter(
293294
(field) =>
295+
// id fields are always included
296+
isIdField(field) ||
294297
// regular fields only
295-
!isDataModel(field.type.reference?.ref) && !isForeignKeyField(field)
298+
(!isDataModel(field.type.reference?.ref) && !isForeignKeyField(field))
296299
);
297300

298301
const relations = model.fields.filter((field) => isDataModel(field.type.reference?.ref));

packages/server/src/api/rest/index.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -719,21 +719,10 @@ class RequestHandler extends APIHandlerBase {
719719
const parsed = this.createUpdatePayloadSchema.parse(body);
720720
const attributes: any = parsed.data.attributes;
721721

722-
// Map in the compound id relationships as attributes, as they are expected by the zod schema
723-
if (parsed.data.relationships) {
724-
for (const [key, data] of Object.entries<any>(parsed.data.relationships)) {
725-
const typeInfo = this.typeMap[key];
726-
if (typeInfo && typeInfo.idFields.length > 1) {
727-
typeInfo.idFields.forEach((field, index) => {
728-
attributes[field.name] = this.coerce(field.type, data.data.id.split(this.idDivider)[index]);
729-
});
730-
}
731-
}
732-
}
733-
734722
if (attributes) {
735-
const schemaName = `${upperCaseFirst(type)}${upperCaseFirst(mode)}Schema`;
736-
// zod-parse attributes if a schema is provided
723+
// use the zod schema (that only contains non-relation fields) to validate the payload,
724+
// if available
725+
const schemaName = `${upperCaseFirst(type)}${upperCaseFirst(mode)}ScalarSchema`;
737726
const payloadSchema = zodSchemas?.models?.[schemaName];
738727
if (payloadSchema) {
739728
const parsed = payloadSchema.safeParse(attributes);
@@ -769,18 +758,6 @@ class RequestHandler extends APIHandlerBase {
769758

770759
const { error, attributes, relationships } = this.processRequestBody(type, requestBody, zodSchemas, 'create');
771760

772-
if (relationships) {
773-
// Remove attributes that are present in compound id relationships, as they are not expected by Prisma
774-
for (const [key] of Object.entries<any>(relationships)) {
775-
const typeInfo = this.typeMap[key];
776-
if (typeInfo && typeInfo.idFields.length > 1) {
777-
typeInfo.idFields.forEach((field) => {
778-
delete attributes[field.name];
779-
});
780-
}
781-
}
782-
}
783-
784761
if (error) {
785762
return error;
786763
}

0 commit comments

Comments
 (0)