@zenstackhq/language 3.7.0 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +52 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +52 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
- package/res/stdlib.zmodel +7 -0
package/dist/index.cjs
CHANGED
|
@@ -4577,6 +4577,7 @@ var DataModelValidator = class {
|
|
|
4577
4577
|
this.validateFields(dm, accept);
|
|
4578
4578
|
if (dm.mixins.length > 0) this.validateMixins(dm, accept);
|
|
4579
4579
|
this.validateInherits(dm, accept);
|
|
4580
|
+
this.validateDelegateMap(dm, accept);
|
|
4580
4581
|
}
|
|
4581
4582
|
validateFields(dm, accept) {
|
|
4582
4583
|
const allFields = require_utils.getAllFields(dm);
|
|
@@ -4807,6 +4808,57 @@ var DataModelValidator = class {
|
|
|
4807
4808
|
todo.push(...current.mixins.map((mixin) => mixin.ref));
|
|
4808
4809
|
}
|
|
4809
4810
|
}
|
|
4811
|
+
validateDelegateMap(dm, accept) {
|
|
4812
|
+
const delegateMapAttrs = dm.attributes.filter((attr) => attr.decl.$refText === "@@delegateMap");
|
|
4813
|
+
if (delegateMapAttrs.length > 1) accept("error", "Model can include at most one @@delegateMap attribute", { node: delegateMapAttrs[1] });
|
|
4814
|
+
const delegateMapAttr = delegateMapAttrs[0];
|
|
4815
|
+
if (delegateMapAttr) {
|
|
4816
|
+
if (!dm.baseModel) accept("error", "`@@delegateMap` can only be used on models that extend a delegate base model", { node: delegateMapAttr });
|
|
4817
|
+
else if (dm.baseModel.ref) this.validateDelegateMapValue(dm.baseModel.ref, delegateMapAttr, accept);
|
|
4818
|
+
}
|
|
4819
|
+
if (!require_utils.hasAttribute(dm, "@@delegate")) return;
|
|
4820
|
+
const subModels = dm.$container.declarations.filter(require_ast.isDataModel).filter((model) => model.baseModel?.ref === dm);
|
|
4821
|
+
if (subModels.length === 0) return;
|
|
4822
|
+
const seen = /* @__PURE__ */ new Map();
|
|
4823
|
+
subModels.forEach((model) => {
|
|
4824
|
+
const value = this.getDelegateMapRawValue(model) ?? model.name;
|
|
4825
|
+
const existing = seen.get(value);
|
|
4826
|
+
if (existing) accept("error", `Duplicate @@delegateMap value "${value}" on models "${existing.name}" and "${model.name}"`, { node: model });
|
|
4827
|
+
else seen.set(value, model);
|
|
4828
|
+
});
|
|
4829
|
+
}
|
|
4830
|
+
getDelegateMapRawValue(dm) {
|
|
4831
|
+
const valueExpr = dm.attributes.find((attr) => attr.decl.$refText === "@@delegateMap")?.args[0]?.value;
|
|
4832
|
+
if (!valueExpr) return;
|
|
4833
|
+
if (require_ast.isStringLiteral(valueExpr)) return valueExpr.value;
|
|
4834
|
+
if (require_utils.isEnumFieldReference(valueExpr)) return valueExpr.target.ref?.name;
|
|
4835
|
+
}
|
|
4836
|
+
validateDelegateMapValue(baseModel, attr, accept) {
|
|
4837
|
+
const delegateMapValueExpr = attr.args[0]?.value;
|
|
4838
|
+
if (!delegateMapValueExpr) {
|
|
4839
|
+
accept("error", "`@@delegateMap` expects a value", { node: attr });
|
|
4840
|
+
return;
|
|
4841
|
+
}
|
|
4842
|
+
const delegateAttr = require_utils.getAttribute(baseModel, "@@delegate");
|
|
4843
|
+
const discriminatorArg = delegateAttr && require_utils.getAttributeArg(delegateAttr, "discriminator");
|
|
4844
|
+
const discriminatorRef = discriminatorArg && require_ast.isReferenceExpr(discriminatorArg) ? discriminatorArg.target.ref : void 0;
|
|
4845
|
+
if (!discriminatorRef || !require_ast.isDataField(discriminatorRef)) return;
|
|
4846
|
+
const discriminatorType = discriminatorRef.type;
|
|
4847
|
+
const discriminatorEnum = discriminatorType.reference?.ref;
|
|
4848
|
+
if (require_utils.isEnumFieldReference(delegateMapValueExpr)) {
|
|
4849
|
+
if (!require_ast.isEnum(discriminatorEnum)) {
|
|
4850
|
+
accept("error", "`@@delegateMap` enum value cannot be used when the discriminator field is String", { node: delegateMapValueExpr });
|
|
4851
|
+
return;
|
|
4852
|
+
}
|
|
4853
|
+
if (delegateMapValueExpr.target.ref?.$container !== discriminatorEnum) accept("error", "`@@delegateMap` enum value must come from the discriminator enum type", { node: delegateMapValueExpr });
|
|
4854
|
+
return;
|
|
4855
|
+
}
|
|
4856
|
+
if (require_ast.isStringLiteral(delegateMapValueExpr)) {
|
|
4857
|
+
if (discriminatorType.type !== "String") accept("error", "`@@delegateMap` string value must match a String discriminator field", { node: delegateMapValueExpr });
|
|
4858
|
+
return;
|
|
4859
|
+
}
|
|
4860
|
+
accept("error", "`@@delegateMap` expects a string literal or enum value", { node: delegateMapValueExpr });
|
|
4861
|
+
}
|
|
4810
4862
|
};
|
|
4811
4863
|
//#endregion
|
|
4812
4864
|
//#region src/validators/datasource-validator.ts
|