Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit b71c1c5

Browse files
authored
fix: Show the correct incomplete error for multiple level inheritance (zenstackhq#916)
1 parent 3afe42f commit b71c1c5

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

packages/schema/src/language-server/validator/datamodel-validator.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,13 @@ export default class DataModelValidator implements AstValidator<DataModel> {
235235
const node = field.$isInherited ? field.$container : field;
236236
const info: DiagnosticInfo<AstNode, string> = { node, code: IssueCodes.MissingOppositeRelation };
237237

238-
let relationFieldDocUri: string;
239-
let relationDataModelName: string;
240-
241-
if (field.$isInherited) {
242-
info.property = 'name';
243-
const container = field.$container as DataModel;
244-
const abstractContainer = container.superTypes.find((x) =>
245-
x.ref?.fields.find((f) => f.name === field.name)
246-
)?.ref as DataModel;
247-
248-
relationFieldDocUri = getDocument(abstractContainer).textDocument.uri;
249-
relationDataModelName = abstractContainer.name;
250-
} else {
251-
relationFieldDocUri = getDocument(field).textDocument.uri;
252-
relationDataModelName = field.$container.name;
253-
}
238+
info.property = 'name';
239+
// use cstNode because the field might be inherited from parent model
240+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
241+
const container = field.$cstNode!.element.$container as DataModel;
242+
243+
const relationFieldDocUri = getDocument(container).textDocument.uri;
244+
const relationDataModelName = container.name;
254245

255246
const data: MissingOppositeRelationData = {
256247
relationFieldName: field.name,

packages/schema/tests/schema/validation/datamodel-validation.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,5 +633,29 @@ describe('Data Model Validation Tests', () => {
633633
expect(errors.length).toBe(1);
634634

635635
expect(errors[0]).toEqual(`Model A cannot be extended because it's not abstract`);
636+
637+
// relation incomplete from multiple level inheritance
638+
expect(
639+
await loadModelWithError(`
640+
${prelude}
641+
model User {
642+
id Int @id @default(autoincrement())
643+
}
644+
645+
abstract model Base {
646+
id Int @id @default(autoincrement())
647+
user User @relation(fields: [userId], references: [id])
648+
userId Int
649+
}
650+
651+
abstract model Base1 extends Base {
652+
isPublic Boolean @default(false)
653+
}
654+
655+
model A extends Base1 {
656+
a String
657+
}
658+
`)
659+
).toContain(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`);
636660
});
637661
});

0 commit comments

Comments
 (0)