convention_builder 1.2.3 → 1.4.0

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.
@@ -8404,6 +8404,45 @@ var require_schema_utilities = __commonJS({
8404
8404
  }
8405
8405
  }
8406
8406
  __name(dig, "dig");
8407
+ function findCommonPath(targetObject, path) {
8408
+ const chunks = path.split(/\.|\[|\]/).filter((d) => d.length !== 0);
8409
+ let partialPath = "";
8410
+ chunks.forEach((attr) => {
8411
+ let newPartialPath = partialPath.concat(".", attr).replace(/^\./, "");
8412
+ let partialPathExists = dig(targetObject, newPartialPath, false) ? true : false;
8413
+ if (partialPathExists) {
8414
+ partialPath = newPartialPath;
8415
+ }
8416
+ ;
8417
+ });
8418
+ return partialPath;
8419
+ }
8420
+ __name(findCommonPath, "findCommonPath");
8421
+ function bury(targetObject, path, value, merge = false) {
8422
+ let acum = value;
8423
+ let partialPath = findCommonPath(targetObject, path);
8424
+ let sharedPathPattern = new RegExp("^".concat(partialPath));
8425
+ let targetPath = path.replace(sharedPathPattern, "").replace(/\.$/, "");
8426
+ let chunks = targetPath.split(/\.|\[|\]/).filter((d) => d.length !== 0);
8427
+ chunks.reverse().forEach((attr) => {
8428
+ let newLayer = {};
8429
+ newLayer[attr] = acum;
8430
+ acum = newLayer;
8431
+ });
8432
+ if (merge) {
8433
+ let sharedObjectSection = dig(targetObject, partialPath);
8434
+ Object.assign(sharedObjectSection, acum);
8435
+ } else {
8436
+ chunks = partialPath.split(/\.|\[|\]/).filter((d) => d.length !== 0);
8437
+ let lastAttr = chunks[chunks.length - 1];
8438
+ let remainingPath = path.replace(new RegExp(lastAttr.concat("$")), "").replace(/\.$/, "");
8439
+ let sharedObjectSection = dig(targetObject, remainingPath);
8440
+ sharedObjectSection[lastAttr] = value;
8441
+ }
8442
+ ;
8443
+ return targetObject;
8444
+ }
8445
+ __name(bury, "bury");
8407
8446
  function fixRelationshipDataField(schema) {
8408
8447
  if (schema.properties.relationships) {
8409
8448
  Object.keys(schema.properties.relationships.properties).forEach((relKey) => {
@@ -8871,6 +8910,57 @@ var require_schema_utilities = __commonJS({
8871
8910
  return table;
8872
8911
  }
8873
8912
  __name(flattenJSONSchema, "flattenJSONSchema");
8913
+ function listAttributesAndSubAttributes(schema) {
8914
+ let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8915
+ let output = topLevelAttributes;
8916
+ let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8917
+ objectTypeAttributes.flatMap((objAttr) => {
8918
+ let subAttrs = Object.keys(schema.properties.attributes.properties[objAttr].properties);
8919
+ subAttrs.forEach((subAttr) => {
8920
+ output.add(`${objAttr}.properties.${subAttr}`);
8921
+ output.delete(`${objAttr}`);
8922
+ });
8923
+ });
8924
+ return output;
8925
+ }
8926
+ __name(listAttributesAndSubAttributes, "listAttributesAndSubAttributes");
8927
+ function findAllObjectAttributesInTree(schemataTree) {
8928
+ let output = [];
8929
+ Object.keys(schemataTree).forEach((typeKey) => {
8930
+ Object.keys(schemataTree[typeKey]).forEach((bundleKey) => {
8931
+ let schema = schemataTree[typeKey][bundleKey];
8932
+ let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8933
+ let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8934
+ objectTypeAttributes.forEach((attr) => {
8935
+ let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8936
+ if (entry) {
8937
+ entry.instances.push(
8938
+ {
8939
+ type: typeKey,
8940
+ bundle: bundleKey,
8941
+ schema: `${typeKey}--${bundleKey}`
8942
+ }
8943
+ );
8944
+ } else {
8945
+ output.push({
8946
+ attribute: attr,
8947
+ instances: [
8948
+ {
8949
+ type: typeKey,
8950
+ bundle: bundleKey,
8951
+ schema: `${typeKey}--${bundleKey}`
8952
+ }
8953
+ ]
8954
+ });
8955
+ }
8956
+ });
8957
+ });
8958
+ });
8959
+ return output;
8960
+ }
8961
+ __name(findAllObjectAttributesInTree, "findAllObjectAttributesInTree");
8962
+ exports.dig = dig;
8963
+ exports.bury = bury;
8874
8964
  exports.fixRelationshipDataField = fixRelationshipDataField;
8875
8965
  exports.buildValidator = buildValidator;
8876
8966
  exports.buildStaticValidator = buildStaticValidator;
@@ -8887,6 +8977,8 @@ var require_schema_utilities = __commonJS({
8887
8977
  exports.reduceSchemaToRequiredFields = reduceSchemaToRequiredFields;
8888
8978
  exports.flattenObjectBranch = flattenObjectBranch;
8889
8979
  exports.flattenJSONSchema = flattenJSONSchema;
8980
+ exports.listAttributesAndSubAttributes = listAttributesAndSubAttributes;
8981
+ exports.findAllObjectAttributesInTree = findAllObjectAttributesInTree;
8890
8982
  }
8891
8983
  });
8892
8984
 
@@ -8894,7 +8986,7 @@ var require_schema_utilities = __commonJS({
8894
8986
  var require_convention_builder = __commonJS({
8895
8987
  "src/convention_builder.js"(exports) {
8896
8988
  var fs = __require("fs");
8897
- var { buildValidator, urlToStringPair } = require_schema_utilities();
8989
+ var { buildValidator, urlToStringPair, dig, bury, listAttributesAndSubAttributes } = require_schema_utilities();
8898
8990
  function getAttributes(root, convention) {
8899
8991
  let branches = convention.relationships.filter((rel) => rel.containerEntity == root).map((d) => d.mentionedEntity);
8900
8992
  let output = {};
@@ -8949,7 +9041,7 @@ var require_convention_builder = __commonJS({
8949
9041
  }
8950
9042
  ;
8951
9043
  this.baseSchema = baseSchema;
8952
- this.unmodifiedAttributes = new Set(Object.keys(this.baseSchema.properties.attributes.properties));
9044
+ this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
8953
9045
  this.modifiedAttributes = /* @__PURE__ */ new Set([]);
8954
9046
  if (!overlay) {
8955
9047
  this.overlay = {
@@ -8978,10 +9070,11 @@ var require_convention_builder = __commonJS({
8978
9070
  this.schema = {};
8979
9071
  this.schema.$id = `${this.repoURL}/schemata/${this.typeAndBundle.replace("--", "/")}/schema.json`;
8980
9072
  Object.assign(this.schema, this.baseSchema);
8981
- let allModifiedAttributes = new Set(Object.keys(this.overlay.properties.attributes.properties));
9073
+ let allModifiedAttributes = listAttributesAndSubAttributes(this.overlay);
8982
9074
  this.modifiedAttributes = allModifiedAttributes;
8983
9075
  Array.from(this.modifiedAttributes).forEach((attr) => {
8984
- this.schema.properties.attributes.properties[attr] = this.overlay.properties.attributes.properties[attr];
9076
+ let currentValue = dig(this.overlay.properties.attributes.properties, attr);
9077
+ bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, true);
8985
9078
  });
8986
9079
  let originalRequiredFields = this.schema.properties.attributes.required ? this.schema.properties.attributes.required : [];
8987
9080
  let allRequiredSet = Array.from(/* @__PURE__ */ new Set([...originalRequiredFields, ...Array.from(this.requiredFields)]));
@@ -9023,6 +9116,21 @@ var require_convention_builder = __commonJS({
9023
9116
  this.requiredFields.add(attribute);
9024
9117
  this.updateSchema();
9025
9118
  }
9119
+ setGenericSection({ attribute, section, description }) {
9120
+ this.checkAttributeStatus(attribute);
9121
+ bury(this.overlay.properties.attributes.properties, attribute, section, true);
9122
+ if (attribute.match(/\./)) {
9123
+ let firstLevel = attribute.split(/\./)[0];
9124
+ this.overlay.properties.attributes.properties[firstLevel].type = "object";
9125
+ }
9126
+ ;
9127
+ if (description) {
9128
+ bury(this.overlay.properties.attributes.properties, attribute.concat(".description"), description, true);
9129
+ }
9130
+ ;
9131
+ this.updateAttributeStatus(attribute);
9132
+ this.updateSchema();
9133
+ }
9026
9134
  /**
9027
9135
  * Sets an attribute as a constant as indicated by the given value.
9028
9136
  * @param {string} attribute -- Attribute name for the attribute which will be set to a constant.
@@ -9030,14 +9138,8 @@ var require_convention_builder = __commonJS({
9030
9138
  * @param {string} description -- Describes the rationale behind the chosing of the value.
9031
9139
  */
9032
9140
  setConstant({ attribute, value, description }) {
9033
- this.checkAttributeStatus(attribute);
9034
- this.overlay.properties.attributes.properties[attribute] = { const: value };
9035
- if (description) {
9036
- this.overlay.properties.attributes.properties[attribute].description = description;
9037
- }
9038
- ;
9039
- this.updateAttributeStatus(attribute);
9040
- this.updateSchema();
9141
+ let constantSection = { const: value };
9142
+ this.setGenericSection({ attribute, section: constantSection, description });
9041
9143
  }
9042
9144
  /**
9043
9145
  * Sets a regex pattern as a restriction for the value of an attribute. Look into the JSON Schema documentation, as it recomends sticking to a [subset of RegEx](https://website-2v2.pages.dev/understanding-json-schema/reference/regular_expressions).
@@ -9047,17 +9149,11 @@ var require_convention_builder = __commonJS({
9047
9149
  * @param {string} description -- Describes the rationale behind the chosing of the value.
9048
9150
  */
9049
9151
  setPattern({ attribute, pattern, string, description }) {
9050
- this.checkAttributeStatus(attribute);
9051
- this.overlay.properties.attributes.properties[attribute] = {
9152
+ let patternSchemaSection = {
9052
9153
  type: "string",
9053
9154
  pattern: pattern ? pattern : string
9054
9155
  };
9055
- if (description) {
9056
- this.overlay.properties.attributes.properties[attribute].description = description;
9057
- }
9058
- ;
9059
- this.updateAttributeStatus(attribute);
9060
- this.updateSchema();
9156
+ this.setGenericSection({ attribute, section: patternSchemaSection, description });
9061
9157
  }
9062
9158
  /**
9063
9159
  * Sets an attribute as a constrained to a list of possible values.
@@ -9073,13 +9169,8 @@ var require_convention_builder = __commonJS({
9073
9169
  } else {
9074
9170
  subschema = { anyOf: [{ type: "string" }, { enum: valuesArray }] };
9075
9171
  }
9076
- this.overlay.properties.attributes.properties[attribute] = subschema;
9077
- if (description) {
9078
- this.overlay.properties.attributes.properties[attribute].description = description;
9079
- }
9080
9172
  ;
9081
- this.updateAttributeStatus(attribute);
9082
- this.updateSchema();
9173
+ this.setGenericSection({ attribute, section: subschema, description });
9083
9174
  }
9084
9175
  /**
9085
9176
  * Build an AJV validator and ensure all valid examples are accepted and all error examples are rejected. Returns an array attribute for each set of examples, plus a general success attribute indicating wether all examples resulted as expected and a failedExamples array only listing entities for which there was no success (meanin unrejected error examples and rejected valid examples).
@@ -8397,6 +8397,45 @@ var require_schema_utilities = __commonJS({
8397
8397
  }
8398
8398
  }
8399
8399
  __name(dig, "dig");
8400
+ function findCommonPath(targetObject, path) {
8401
+ const chunks = path.split(/\.|\[|\]/).filter((d) => d.length !== 0);
8402
+ let partialPath = "";
8403
+ chunks.forEach((attr) => {
8404
+ let newPartialPath = partialPath.concat(".", attr).replace(/^\./, "");
8405
+ let partialPathExists = dig(targetObject, newPartialPath, false) ? true : false;
8406
+ if (partialPathExists) {
8407
+ partialPath = newPartialPath;
8408
+ }
8409
+ ;
8410
+ });
8411
+ return partialPath;
8412
+ }
8413
+ __name(findCommonPath, "findCommonPath");
8414
+ function bury(targetObject, path, value, merge = false) {
8415
+ let acum = value;
8416
+ let partialPath = findCommonPath(targetObject, path);
8417
+ let sharedPathPattern = new RegExp("^".concat(partialPath));
8418
+ let targetPath = path.replace(sharedPathPattern, "").replace(/\.$/, "");
8419
+ let chunks = targetPath.split(/\.|\[|\]/).filter((d) => d.length !== 0);
8420
+ chunks.reverse().forEach((attr) => {
8421
+ let newLayer = {};
8422
+ newLayer[attr] = acum;
8423
+ acum = newLayer;
8424
+ });
8425
+ if (merge) {
8426
+ let sharedObjectSection = dig(targetObject, partialPath);
8427
+ Object.assign(sharedObjectSection, acum);
8428
+ } else {
8429
+ chunks = partialPath.split(/\.|\[|\]/).filter((d) => d.length !== 0);
8430
+ let lastAttr = chunks[chunks.length - 1];
8431
+ let remainingPath = path.replace(new RegExp(lastAttr.concat("$")), "").replace(/\.$/, "");
8432
+ let sharedObjectSection = dig(targetObject, remainingPath);
8433
+ sharedObjectSection[lastAttr] = value;
8434
+ }
8435
+ ;
8436
+ return targetObject;
8437
+ }
8438
+ __name(bury, "bury");
8400
8439
  function fixRelationshipDataField(schema) {
8401
8440
  if (schema.properties.relationships) {
8402
8441
  Object.keys(schema.properties.relationships.properties).forEach((relKey) => {
@@ -8864,6 +8903,57 @@ var require_schema_utilities = __commonJS({
8864
8903
  return table;
8865
8904
  }
8866
8905
  __name(flattenJSONSchema, "flattenJSONSchema");
8906
+ function listAttributesAndSubAttributes(schema) {
8907
+ let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8908
+ let output = topLevelAttributes;
8909
+ let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8910
+ objectTypeAttributes.flatMap((objAttr) => {
8911
+ let subAttrs = Object.keys(schema.properties.attributes.properties[objAttr].properties);
8912
+ subAttrs.forEach((subAttr) => {
8913
+ output.add(`${objAttr}.properties.${subAttr}`);
8914
+ output.delete(`${objAttr}`);
8915
+ });
8916
+ });
8917
+ return output;
8918
+ }
8919
+ __name(listAttributesAndSubAttributes, "listAttributesAndSubAttributes");
8920
+ function findAllObjectAttributesInTree(schemataTree) {
8921
+ let output = [];
8922
+ Object.keys(schemataTree).forEach((typeKey) => {
8923
+ Object.keys(schemataTree[typeKey]).forEach((bundleKey) => {
8924
+ let schema = schemataTree[typeKey][bundleKey];
8925
+ let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8926
+ let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8927
+ objectTypeAttributes.forEach((attr) => {
8928
+ let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8929
+ if (entry) {
8930
+ entry.instances.push(
8931
+ {
8932
+ type: typeKey,
8933
+ bundle: bundleKey,
8934
+ schema: `${typeKey}--${bundleKey}`
8935
+ }
8936
+ );
8937
+ } else {
8938
+ output.push({
8939
+ attribute: attr,
8940
+ instances: [
8941
+ {
8942
+ type: typeKey,
8943
+ bundle: bundleKey,
8944
+ schema: `${typeKey}--${bundleKey}`
8945
+ }
8946
+ ]
8947
+ });
8948
+ }
8949
+ });
8950
+ });
8951
+ });
8952
+ return output;
8953
+ }
8954
+ __name(findAllObjectAttributesInTree, "findAllObjectAttributesInTree");
8955
+ exports2.dig = dig;
8956
+ exports2.bury = bury;
8867
8957
  exports2.fixRelationshipDataField = fixRelationshipDataField;
8868
8958
  exports2.buildValidator = buildValidator;
8869
8959
  exports2.buildStaticValidator = buildStaticValidator;
@@ -8880,6 +8970,8 @@ var require_schema_utilities = __commonJS({
8880
8970
  exports2.reduceSchemaToRequiredFields = reduceSchemaToRequiredFields;
8881
8971
  exports2.flattenObjectBranch = flattenObjectBranch;
8882
8972
  exports2.flattenJSONSchema = flattenJSONSchema;
8973
+ exports2.listAttributesAndSubAttributes = listAttributesAndSubAttributes;
8974
+ exports2.findAllObjectAttributesInTree = findAllObjectAttributesInTree;
8883
8975
  }
8884
8976
  });
8885
8977
 
@@ -8887,7 +8979,7 @@ var require_schema_utilities = __commonJS({
8887
8979
  var require_convention_builder = __commonJS({
8888
8980
  "src/convention_builder.js"(exports2) {
8889
8981
  var fs = require("fs");
8890
- var { buildValidator, urlToStringPair } = require_schema_utilities();
8982
+ var { buildValidator, urlToStringPair, dig, bury, listAttributesAndSubAttributes } = require_schema_utilities();
8891
8983
  function getAttributes(root, convention) {
8892
8984
  let branches = convention.relationships.filter((rel) => rel.containerEntity == root).map((d) => d.mentionedEntity);
8893
8985
  let output = {};
@@ -8942,7 +9034,7 @@ var require_convention_builder = __commonJS({
8942
9034
  }
8943
9035
  ;
8944
9036
  this.baseSchema = baseSchema;
8945
- this.unmodifiedAttributes = new Set(Object.keys(this.baseSchema.properties.attributes.properties));
9037
+ this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
8946
9038
  this.modifiedAttributes = /* @__PURE__ */ new Set([]);
8947
9039
  if (!overlay) {
8948
9040
  this.overlay = {
@@ -8971,10 +9063,11 @@ var require_convention_builder = __commonJS({
8971
9063
  this.schema = {};
8972
9064
  this.schema.$id = `${this.repoURL}/schemata/${this.typeAndBundle.replace("--", "/")}/schema.json`;
8973
9065
  Object.assign(this.schema, this.baseSchema);
8974
- let allModifiedAttributes = new Set(Object.keys(this.overlay.properties.attributes.properties));
9066
+ let allModifiedAttributes = listAttributesAndSubAttributes(this.overlay);
8975
9067
  this.modifiedAttributes = allModifiedAttributes;
8976
9068
  Array.from(this.modifiedAttributes).forEach((attr) => {
8977
- this.schema.properties.attributes.properties[attr] = this.overlay.properties.attributes.properties[attr];
9069
+ let currentValue = dig(this.overlay.properties.attributes.properties, attr);
9070
+ bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, true);
8978
9071
  });
8979
9072
  let originalRequiredFields = this.schema.properties.attributes.required ? this.schema.properties.attributes.required : [];
8980
9073
  let allRequiredSet = Array.from(/* @__PURE__ */ new Set([...originalRequiredFields, ...Array.from(this.requiredFields)]));
@@ -9016,6 +9109,21 @@ var require_convention_builder = __commonJS({
9016
9109
  this.requiredFields.add(attribute);
9017
9110
  this.updateSchema();
9018
9111
  }
9112
+ setGenericSection({ attribute, section, description }) {
9113
+ this.checkAttributeStatus(attribute);
9114
+ bury(this.overlay.properties.attributes.properties, attribute, section, true);
9115
+ if (attribute.match(/\./)) {
9116
+ let firstLevel = attribute.split(/\./)[0];
9117
+ this.overlay.properties.attributes.properties[firstLevel].type = "object";
9118
+ }
9119
+ ;
9120
+ if (description) {
9121
+ bury(this.overlay.properties.attributes.properties, attribute.concat(".description"), description, true);
9122
+ }
9123
+ ;
9124
+ this.updateAttributeStatus(attribute);
9125
+ this.updateSchema();
9126
+ }
9019
9127
  /**
9020
9128
  * Sets an attribute as a constant as indicated by the given value.
9021
9129
  * @param {string} attribute -- Attribute name for the attribute which will be set to a constant.
@@ -9023,14 +9131,8 @@ var require_convention_builder = __commonJS({
9023
9131
  * @param {string} description -- Describes the rationale behind the chosing of the value.
9024
9132
  */
9025
9133
  setConstant({ attribute, value, description }) {
9026
- this.checkAttributeStatus(attribute);
9027
- this.overlay.properties.attributes.properties[attribute] = { const: value };
9028
- if (description) {
9029
- this.overlay.properties.attributes.properties[attribute].description = description;
9030
- }
9031
- ;
9032
- this.updateAttributeStatus(attribute);
9033
- this.updateSchema();
9134
+ let constantSection = { const: value };
9135
+ this.setGenericSection({ attribute, section: constantSection, description });
9034
9136
  }
9035
9137
  /**
9036
9138
  * Sets a regex pattern as a restriction for the value of an attribute. Look into the JSON Schema documentation, as it recomends sticking to a [subset of RegEx](https://website-2v2.pages.dev/understanding-json-schema/reference/regular_expressions).
@@ -9040,17 +9142,11 @@ var require_convention_builder = __commonJS({
9040
9142
  * @param {string} description -- Describes the rationale behind the chosing of the value.
9041
9143
  */
9042
9144
  setPattern({ attribute, pattern, string, description }) {
9043
- this.checkAttributeStatus(attribute);
9044
- this.overlay.properties.attributes.properties[attribute] = {
9145
+ let patternSchemaSection = {
9045
9146
  type: "string",
9046
9147
  pattern: pattern ? pattern : string
9047
9148
  };
9048
- if (description) {
9049
- this.overlay.properties.attributes.properties[attribute].description = description;
9050
- }
9051
- ;
9052
- this.updateAttributeStatus(attribute);
9053
- this.updateSchema();
9149
+ this.setGenericSection({ attribute, section: patternSchemaSection, description });
9054
9150
  }
9055
9151
  /**
9056
9152
  * Sets an attribute as a constrained to a list of possible values.
@@ -9066,13 +9162,8 @@ var require_convention_builder = __commonJS({
9066
9162
  } else {
9067
9163
  subschema = { anyOf: [{ type: "string" }, { enum: valuesArray }] };
9068
9164
  }
9069
- this.overlay.properties.attributes.properties[attribute] = subschema;
9070
- if (description) {
9071
- this.overlay.properties.attributes.properties[attribute].description = description;
9072
- }
9073
9165
  ;
9074
- this.updateAttributeStatus(attribute);
9075
- this.updateSchema();
9166
+ this.setGenericSection({ attribute, section: subschema, description });
9076
9167
  }
9077
9168
  /**
9078
9169
  * Build an AJV validator and ensure all valid examples are accepted and all error examples are rejected. Returns an array attribute for each set of examples, plus a general success attribute indicating wether all examples resulted as expected and a failedExamples array only listing entities for which there was no success (meanin unrejected error examples and rejected valid examples).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convention_builder",
3
- "version": "1.2.3",
3
+ "version": "1.4.0",
4
4
  "description": "Helper tools that offer a high level interface that transforms a convention description into it's json schema.",
5
5
  "main": "./dist/node/index.js",
6
6
  "browser": "./dist/browser/index.js",