From e381e5c14bd3476dd1e88457e0d9172a5b2d2b1a Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sat, 9 Mar 2024 16:43:16 -0800 Subject: [PATCH] fix: disallow extending from multiple delegate models --- .../validator/datamodel-validator.ts | 10 ++++++++ .../validation/datamodel-validation.test.ts | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index 4f9cd0039..0baf5ace3 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -22,6 +22,7 @@ import { validateDuplicatedDeclarations } from './utils'; export default class DataModelValidator implements AstValidator { validate(dm: DataModel, accept: ValidationAcceptor): void { this.validateBaseAbstractModel(dm, accept); + this.validateBaseDelegateModel(dm, accept); validateDuplicatedDeclarations(dm, getModelFieldsWithBases(dm), accept); this.validateAttributes(dm, accept); this.validateFields(dm, accept); @@ -396,6 +397,15 @@ export default class DataModelValidator implements AstValidator { ); }); } + + private validateBaseDelegateModel(model: DataModel, accept: ValidationAcceptor) { + if (model.superTypes.filter((base) => base.ref && isDelegateModel(base.ref)).length > 1) { + accept('error', 'Extending from multiple delegate models is not supported', { + node: model, + property: 'superTypes', + }); + } + } } export interface MissingOppositeRelationData { diff --git a/packages/schema/tests/schema/validation/datamodel-validation.test.ts b/packages/schema/tests/schema/validation/datamodel-validation.test.ts index 955f315c3..4f44b109e 100644 --- a/packages/schema/tests/schema/validation/datamodel-validation.test.ts +++ b/packages/schema/tests/schema/validation/datamodel-validation.test.ts @@ -714,4 +714,28 @@ describe('Data Model Validation Tests', () => { errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`) ); }); + + it('delegate base type', async () => { + const errors = await safelyLoadModel(` + ${prelude} + + model Base1 { + id String @id + type String + @@delegate(type) + } + + model Base2 { + id String @id + type String + @@delegate(type) + } + + model A extends Base1,Base2 { + a String + } + `); + + expect(errors).toMatchObject(errorLike(`Extending from multiple delegate models is not supported`)); + }); });