@zenstackhq/language 3.0.0-beta.20 → 3.0.0-beta.21

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 CHANGED
@@ -5302,7 +5302,7 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
5302
5302
  ] : []
5303
5303
  ];
5304
5304
  bases.forEach((base) => {
5305
- const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
5305
+ const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
5306
5306
  if (baseDecl) {
5307
5307
  if (!includeDelegate && isDelegateModel(baseDecl)) {
5308
5308
  return;
@@ -5425,6 +5425,10 @@ function getArray(expr) {
5425
5425
  return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
5426
5426
  }
5427
5427
  __name(getArray, "getArray");
5428
+ function getAttributeArg(attr, name) {
5429
+ return attr.args.find((arg) => arg.$resolvedParam?.name === name)?.value;
5430
+ }
5431
+ __name(getAttributeArg, "getAttributeArg");
5428
5432
  function getFunctionExpressionContext(funcDecl) {
5429
5433
  const funcAllowedContext = [];
5430
5434
  const funcAttr = funcDecl.attributes.find((attr) => attr.decl.$refText === "@@@expressionContext");
@@ -5693,6 +5697,7 @@ function findRootNode(node) {
5693
5697
  __name(findRootNode, "findRootNode");
5694
5698
 
5695
5699
  // src/validators/attribute-application-validator.ts
5700
+ var import_common_helpers = require("@zenstackhq/common-helpers");
5696
5701
  var import_langium3 = require("langium");
5697
5702
  var import_pluralize = __toESM(require("pluralize"), 1);
5698
5703
  function _ts_decorate(decorators, target, key, desc) {
@@ -5913,7 +5918,7 @@ var AttributeApplicationValidator = class {
5913
5918
  }
5914
5919
  }
5915
5920
  _checkConstraint(attr, accept) {
5916
- const fields = attr.args[0]?.value;
5921
+ const fields = getAttributeArg(attr, "fields");
5917
5922
  const attrName = attr.decl.ref?.name;
5918
5923
  if (!fields) {
5919
5924
  accept("error", `expects an array of field references`, {
@@ -5953,6 +5958,24 @@ var AttributeApplicationValidator = class {
5953
5958
  });
5954
5959
  }
5955
5960
  }
5961
+ _checkSchema(attr, accept) {
5962
+ const schemaName = getStringLiteral(attr.args[0]?.value);
5963
+ (0, import_common_helpers.invariant)(schemaName, `@@schema expects a string literal`);
5964
+ const zmodel = import_langium3.AstUtils.getContainerOfType(attr, isModel);
5965
+ const datasource = zmodel.declarations.find(isDataSource);
5966
+ if (datasource) {
5967
+ let found = false;
5968
+ const schemas = datasource.fields.find((f) => f.name === "schemas");
5969
+ if (schemas && isConfigArrayExpr(schemas.value)) {
5970
+ found = schemas.value.items.some((item) => isLiteralExpr(item) && item.value === schemaName);
5971
+ }
5972
+ if (!found) {
5973
+ accept("error", `Schema "${schemaName}" is not defined in the datasource`, {
5974
+ node: attr
5975
+ });
5976
+ }
5977
+ }
5978
+ }
5956
5979
  validatePolicyKinds(kind, candidates, attr, accept) {
5957
5980
  const items = kind.split(",").map((x) => x.trim());
5958
5981
  items.forEach((item) => {
@@ -6005,6 +6028,15 @@ _ts_decorate([
6005
6028
  ]),
6006
6029
  _ts_metadata("design:returntype", void 0)
6007
6030
  ], AttributeApplicationValidator.prototype, "_checkConstraint", null);
6031
+ _ts_decorate([
6032
+ check("@@schema"),
6033
+ _ts_metadata("design:type", Function),
6034
+ _ts_metadata("design:paramtypes", [
6035
+ typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
6036
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
6037
+ ]),
6038
+ _ts_metadata("design:returntype", void 0)
6039
+ ], AttributeApplicationValidator.prototype, "_checkSchema", null);
6008
6040
  function assignableToAttributeParam(arg, param, attr) {
6009
6041
  const argResolvedType = arg.$resolvedType;
6010
6042
  if (!argResolvedType) {
@@ -6138,7 +6170,7 @@ var AttributeValidator = class {
6138
6170
  };
6139
6171
 
6140
6172
  // src/validators/datamodel-validator.ts
6141
- var import_common_helpers = require("@zenstackhq/common-helpers");
6173
+ var import_common_helpers2 = require("@zenstackhq/common-helpers");
6142
6174
  var import_langium4 = require("langium");
6143
6175
 
6144
6176
  // src/validators/common.ts
@@ -6509,7 +6541,7 @@ var DataModelValidator = class {
6509
6541
  if (!model.baseModel) {
6510
6542
  return;
6511
6543
  }
6512
- (0, import_common_helpers.invariant)(model.baseModel.ref, "baseModel must be resolved");
6544
+ (0, import_common_helpers2.invariant)(model.baseModel.ref, "baseModel must be resolved");
6513
6545
  if (!isDelegateModel(model.baseModel.ref)) {
6514
6546
  accept("error", `Model ${model.baseModel.$refText} cannot be extended because it's not a delegate model`, {
6515
6547
  node: model,
@@ -6531,7 +6563,7 @@ var DataModelValidator = class {
6531
6563
  }
6532
6564
  seen.push(current);
6533
6565
  if (current.baseModel) {
6534
- (0, import_common_helpers.invariant)(current.baseModel.ref, "baseModel must be resolved");
6566
+ (0, import_common_helpers2.invariant)(current.baseModel.ref, "baseModel must be resolved");
6535
6567
  todo.push(current.baseModel.ref);
6536
6568
  }
6537
6569
  }
@@ -6562,7 +6594,6 @@ var DataSourceValidator = class {
6562
6594
  validateDuplicatedDeclarations(ds, ds.fields, accept);
6563
6595
  this.validateProvider(ds, accept);
6564
6596
  this.validateUrl(ds, accept);
6565
- this.validateRelationMode(ds, accept);
6566
6597
  }
6567
6598
  validateProvider(ds, accept) {
6568
6599
  const provider = ds.fields.find((f) => f.name === "provider");
@@ -6572,16 +6603,52 @@ var DataSourceValidator = class {
6572
6603
  });
6573
6604
  return;
6574
6605
  }
6575
- const value = getStringLiteral(provider.value);
6576
- if (!value) {
6606
+ const providerValue = getStringLiteral(provider.value);
6607
+ if (!providerValue) {
6577
6608
  accept("error", '"provider" must be set to a string literal', {
6578
6609
  node: provider.value
6579
6610
  });
6580
- } else if (!SUPPORTED_PROVIDERS.includes(value)) {
6581
- accept("error", `Provider "${value}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map((p) => '"' + p + '"').join(" | ")}.`, {
6611
+ } else if (!SUPPORTED_PROVIDERS.includes(providerValue)) {
6612
+ accept("error", `Provider "${providerValue}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map((p) => '"' + p + '"').join(" | ")}.`, {
6582
6613
  node: provider.value
6583
6614
  });
6584
6615
  }
6616
+ const defaultSchemaField = ds.fields.find((f) => f.name === "defaultSchema");
6617
+ let defaultSchemaValue;
6618
+ if (defaultSchemaField) {
6619
+ if (providerValue !== "postgresql") {
6620
+ accept("error", '"defaultSchema" is only supported for "postgresql" provider', {
6621
+ node: defaultSchemaField
6622
+ });
6623
+ }
6624
+ defaultSchemaValue = getStringLiteral(defaultSchemaField.value);
6625
+ if (!defaultSchemaValue) {
6626
+ accept("error", '"defaultSchema" must be a string literal', {
6627
+ node: defaultSchemaField.value
6628
+ });
6629
+ }
6630
+ }
6631
+ const schemasField = ds.fields.find((f) => f.name === "schemas");
6632
+ if (schemasField) {
6633
+ if (providerValue !== "postgresql") {
6634
+ accept("error", '"schemas" is only supported for "postgresql" provider', {
6635
+ node: schemasField
6636
+ });
6637
+ }
6638
+ const schemasValue = schemasField.value;
6639
+ if (!isConfigArrayExpr(schemasValue) || !schemasValue.items.every((e) => isLiteralExpr(e) && typeof getStringLiteral(e) === "string")) {
6640
+ accept("error", '"schemas" must be an array of string literals', {
6641
+ node: schemasField
6642
+ });
6643
+ } else if (
6644
+ // validate `defaultSchema` is included in `schemas`
6645
+ defaultSchemaValue && !schemasValue.items.some((e) => getStringLiteral(e) === defaultSchemaValue)
6646
+ ) {
6647
+ accept("error", `"${defaultSchemaValue}" must be included in the "schemas" array`, {
6648
+ node: schemasField
6649
+ });
6650
+ }
6651
+ }
6585
6652
  }
6586
6653
  validateUrl(ds, accept) {
6587
6654
  const urlField = ds.fields.find((f) => f.name === "url");
@@ -6595,20 +6662,6 @@ var DataSourceValidator = class {
6595
6662
  });
6596
6663
  }
6597
6664
  }
6598
- validateRelationMode(ds, accept) {
6599
- const field = ds.fields.find((f) => f.name === "relationMode");
6600
- if (field) {
6601
- const val = getStringLiteral(field.value);
6602
- if (!val || ![
6603
- "foreignKeys",
6604
- "prisma"
6605
- ].includes(val)) {
6606
- accept("error", '"relationMode" must be set to "foreignKeys" or "prisma"', {
6607
- node: field.value
6608
- });
6609
- }
6610
- }
6611
- }
6612
6665
  };
6613
6666
 
6614
6667
  // src/validators/enum-validator.ts