convention_builder 1.4.1 → 1.4.2

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.
@@ -8434,10 +8434,15 @@ var require_schema_utilities = __commonJS({
8434
8434
  Object.assign(sharedObjectSection, acum);
8435
8435
  } else {
8436
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;
8437
+ let lastAttr = chunks.length == 0 ? chunks[0] : chunks[chunks.length - 1];
8438
+ if (lastAttr) {
8439
+ let remainingPath = path.replace(new RegExp(lastAttr.concat("$")), "").replace(/\.$/, "");
8440
+ let sharedObjectSection = dig(targetObject, remainingPath);
8441
+ sharedObjectSection[lastAttr] = value;
8442
+ } else {
8443
+ targetObject[path] = value;
8444
+ }
8445
+ ;
8441
8446
  }
8442
8447
  ;
8443
8448
  return targetObject;
@@ -8483,7 +8488,6 @@ var require_schema_utilities = __commonJS({
8483
8488
  fs.mkdirSync(`${targetFolder}/${typeKey}/${bundleKey}/`, { recursive: true }, console.error);
8484
8489
  let path = `${targetFolder}/${typeKey}/${bundleKey}/schema.json`;
8485
8490
  fs.writeFileSync(path, JSON.stringify(schema, null, " "));
8486
- console.log(`writing ${typeKey}--${bundleKey}`);
8487
8491
  });
8488
8492
  });
8489
8493
  }
@@ -8539,7 +8543,6 @@ var require_schema_utilities = __commonJS({
8539
8543
  return files;
8540
8544
  }).filter((filename) => !/\./.test(filename));
8541
8545
  conventions.forEach((conventionName) => {
8542
- console.log(`Working on ${conventionName}`);
8543
8546
  try {
8544
8547
  let schema = JSON.parse(fs.readFileSync(`${conventionsFolder}/${conventionName}/schema.json`));
8545
8548
  schemasArgument.schemas.push(schema);
@@ -8774,6 +8777,9 @@ var require_schema_utilities = __commonJS({
8774
8777
  function organizeEntitiesArrayByRelationships({ entitiesArray, conventionObject }) {
8775
8778
  let attributesTree = conventionObject.getAttributesTree();
8776
8779
  let mainAttribute = Object.keys(attributesTree);
8780
+ return {
8781
+ tree: attributesTree
8782
+ };
8777
8783
  }
8778
8784
  __name(organizeEntitiesArrayByRelationships, "organizeEntitiesArrayByRelationships");
8779
8785
  function trimNonRequiredFields(schema, attribute = false) {
@@ -8934,7 +8940,14 @@ var require_schema_utilities = __commonJS({
8934
8940
  let schema = schemataTree[typeKey][bundleKey];
8935
8941
  let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8936
8942
  let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8937
- objectTypeAttributes.forEach((attr) => {
8943
+ objectTypeAttributes.flatMap((objAttr) => {
8944
+ let output2 = [];
8945
+ let subAttrs = Object.keys(schema.properties.attributes.properties[objAttr].properties);
8946
+ subAttrs.forEach((subAttr) => {
8947
+ output2.push(`${objAttr}.properties.${subAttr}`);
8948
+ });
8949
+ return output2;
8950
+ }).forEach((attr) => {
8938
8951
  let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8939
8952
  if (entry) {
8940
8953
  entry.instances.push(
@@ -8962,6 +8975,42 @@ var require_schema_utilities = __commonJS({
8962
8975
  return output;
8963
8976
  }
8964
8977
  __name(findAllObjectAttributesInTree, "findAllObjectAttributesInTree");
8978
+ function findAllNonObjectAttributesInTree(schemataTree) {
8979
+ let output = [];
8980
+ Object.keys(schemataTree).forEach((typeKey) => {
8981
+ Object.keys(schemataTree[typeKey]).forEach((bundleKey) => {
8982
+ let schema = schemataTree[typeKey][bundleKey];
8983
+ let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8984
+ let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8985
+ let otherAttributes = Array.from(topLevelAttributes).filter((attr) => !objectTypeAttributes.includes(attr));
8986
+ otherAttributes.forEach((attr) => {
8987
+ let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8988
+ if (entry) {
8989
+ entry.instances.push(
8990
+ {
8991
+ type: typeKey,
8992
+ bundle: bundleKey,
8993
+ schema: `${typeKey}--${bundleKey}`
8994
+ }
8995
+ );
8996
+ } else {
8997
+ output.push({
8998
+ attribute: attr,
8999
+ instances: [
9000
+ {
9001
+ type: typeKey,
9002
+ bundle: bundleKey,
9003
+ schema: `${typeKey}--${bundleKey}`
9004
+ }
9005
+ ]
9006
+ });
9007
+ }
9008
+ });
9009
+ });
9010
+ });
9011
+ return output;
9012
+ }
9013
+ __name(findAllNonObjectAttributesInTree, "findAllNonObjectAttributesInTree");
8965
9014
  exports.dig = dig;
8966
9015
  exports.bury = bury;
8967
9016
  exports.fixRelationshipDataField = fixRelationshipDataField;
@@ -8982,6 +9031,7 @@ var require_schema_utilities = __commonJS({
8982
9031
  exports.flattenJSONSchema = flattenJSONSchema;
8983
9032
  exports.listAttributesAndSubAttributes = listAttributesAndSubAttributes;
8984
9033
  exports.findAllObjectAttributesInTree = findAllObjectAttributesInTree;
9034
+ exports.findAllNonObjectAttributesInTree = findAllNonObjectAttributesInTree;
8985
9035
  }
8986
9036
  });
8987
9037
 
@@ -8989,7 +9039,15 @@ var require_schema_utilities = __commonJS({
8989
9039
  var require_convention_builder = __commonJS({
8990
9040
  "src/convention_builder.js"(exports) {
8991
9041
  var fs = __require("fs");
8992
- var { buildValidator, urlToStringPair, dig, bury, listAttributesAndSubAttributes } = require_schema_utilities();
9042
+ var {
9043
+ buildValidator,
9044
+ urlToStringPair,
9045
+ dig,
9046
+ bury,
9047
+ listAttributesAndSubAttributes,
9048
+ findAllObjectAttributesInTree,
9049
+ findAllNonObjectAttributesInTree
9050
+ } = require_schema_utilities();
8993
9051
  function getAttributes(root, convention) {
8994
9052
  let branches = convention.relationships.filter((rel) => rel.containerEntity == root).map((d) => d.mentionedEntity);
8995
9053
  let output = {};
@@ -9010,7 +9068,8 @@ var require_convention_builder = __commonJS({
9010
9068
  originalSrcScript,
9011
9069
  trivial = false,
9012
9070
  baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9013
- requiredFields = []
9071
+ requiredFields = [],
9072
+ modifiedAttributes
9014
9073
  }) {
9015
9074
  this.name = name;
9016
9075
  this.typeAndBundle = typeAndBundle;
@@ -9045,7 +9104,7 @@ var require_convention_builder = __commonJS({
9045
9104
  ;
9046
9105
  this.baseSchema = baseSchema;
9047
9106
  this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9048
- this.modifiedAttributes = /* @__PURE__ */ new Set([]);
9107
+ this.modifiedAttributes = modifiedAttributes ? modifiedAttributes : /* @__PURE__ */ new Set([]);
9049
9108
  if (!overlay) {
9050
9109
  this.overlay = {
9051
9110
  properties: {
@@ -9061,11 +9120,17 @@ var require_convention_builder = __commonJS({
9061
9120
  this.schema = baseSchema;
9062
9121
  let validatorObj = buildValidator();
9063
9122
  this.validate = validatorObj.compile(this.schema);
9064
- this.updateSchema();
9065
9123
  }
9066
9124
  storagePath() {
9067
9125
  return `${this.storageFolder}`;
9068
9126
  }
9127
+ /**
9128
+ * Given an attribute path (such as "attributes.properties.geometry.properties.value"), it will set is as required in the adequate containing attribute (for this example, "attributes.properties.geometry.required").
9129
+ * @param {string} path -- Attribute path.
9130
+ * @returns {}
9131
+ */
9132
+ requireFieldByPath(path) {
9133
+ }
9069
9134
  /**
9070
9135
  * User input only works over the overlay, the schema should always be the direct result of applying the overlay over the baseSchema.
9071
9136
  */
@@ -9075,9 +9140,11 @@ var require_convention_builder = __commonJS({
9075
9140
  Object.assign(this.schema, this.baseSchema);
9076
9141
  let allModifiedAttributes = listAttributesAndSubAttributes(this.overlay);
9077
9142
  this.modifiedAttributes = allModifiedAttributes;
9143
+ this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9144
+ this.modifiedAttributes.forEach((attr) => this.updateAttributeStatus(attr));
9078
9145
  Array.from(this.modifiedAttributes).forEach((attr) => {
9079
9146
  let currentValue = dig(this.overlay.properties.attributes.properties, attr);
9080
- bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, true);
9147
+ bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, false);
9081
9148
  });
9082
9149
  let originalRequiredFields = this.schema.properties.attributes.required ? this.schema.properties.attributes.required : [];
9083
9150
  let allRequiredSet = Array.from(/* @__PURE__ */ new Set([...originalRequiredFields, ...Array.from(this.requiredFields)]));
@@ -9119,12 +9186,18 @@ var require_convention_builder = __commonJS({
9119
9186
  this.requiredFields.add(attribute);
9120
9187
  this.updateSchema();
9121
9188
  }
9122
- setGenericSection({ attribute, section, description }) {
9189
+ /**
9190
+ * The section body "section" will replace the previous schema content in full.
9191
+ * @param {} attribute
9192
+ * @param {} section
9193
+ * @param {} description
9194
+ */
9195
+ setGenericSection({ attribute, section, description, merge = true }) {
9123
9196
  this.checkAttributeStatus(attribute);
9124
- bury(this.overlay.properties.attributes.properties, attribute, section, true);
9197
+ bury(this.overlay.properties.attributes.properties, attribute, section, merge);
9125
9198
  if (attribute.match(/\./)) {
9126
9199
  let firstLevel = attribute.split(/\./)[0];
9127
- this.overlay.properties.attributes.properties[firstLevel].type = "object";
9200
+ bury(this.overlay.properties.attributes.properties, `${firstLevel}.type`, "object", merge);
9128
9201
  }
9129
9202
  ;
9130
9203
  if (description) {
@@ -9261,6 +9334,42 @@ var require_convention_builder = __commonJS({
9261
9334
  };
9262
9335
  __name(_SchemaOverlay, "SchemaOverlay");
9263
9336
  var SchemaOverlay = _SchemaOverlay;
9337
+ var _AttributePatchingSchemaOverlay = class _AttributePatchingSchemaOverlay extends SchemaOverlay {
9338
+ constructor({
9339
+ typeAndBundle,
9340
+ attributeOverlayName,
9341
+ repoURL,
9342
+ strictEnums = true,
9343
+ baseSchemataFolder = `${__dirname}/../../../../input/collection`
9344
+ }) {
9345
+ super({
9346
+ typeAndBundle,
9347
+ repoURL,
9348
+ storageFolder: baseSchemataFolder,
9349
+ baseSchemataFolder,
9350
+ strictEnums
9351
+ });
9352
+ this.attributeOverlayName = attributeOverlayName;
9353
+ }
9354
+ store() {
9355
+ this.updateSchema();
9356
+ if (this.schema.appliedOverlays) {
9357
+ this.schema.appliedOverlays.push(this.attributeOverlayName);
9358
+ } else {
9359
+ this.schema.appliedOverlays = [this.attributeOverlayName];
9360
+ }
9361
+ ;
9362
+ let targetPath = `${this.storagePath()}/${this.typeAndBundle.split("--")[0]}/${this.typeAndBundle.split("--")[1]}/schema.json`;
9363
+ fs.writeFileSync(targetPath, JSON.stringify(this.schema, null, " "), console.error);
9364
+ let output = {
9365
+ path: targetPath,
9366
+ test: false
9367
+ };
9368
+ return output;
9369
+ }
9370
+ };
9371
+ __name(_AttributePatchingSchemaOverlay, "AttributePatchingSchemaOverlay");
9372
+ var AttributePatchingSchemaOverlay = _AttributePatchingSchemaOverlay;
9264
9373
  var _ConventionSchema = class _ConventionSchema {
9265
9374
  constructor({
9266
9375
  title,
@@ -9372,6 +9481,7 @@ var require_convention_builder = __commonJS({
9372
9481
  required: this.required
9373
9482
  };
9374
9483
  Object.keys(this.overlays).forEach((attributeName) => {
9484
+ this.overlays[attributeName].updateSchema();
9375
9485
  this.schema.properties[attributeName] = this.overlays[attributeName].schema;
9376
9486
  delete this.schema.properties[attributeName].$id;
9377
9487
  if (!this.overlays[attributeName].originalSrcScript) {
@@ -9705,17 +9815,121 @@ import TabItem from '@theme/TabItem';
9705
9815
  };
9706
9816
  __name(_ConventionSchema, "ConventionSchema");
9707
9817
  var ConventionSchema = _ConventionSchema;
9818
+ var _AttributeOverlay = class _AttributeOverlay {
9819
+ constructor({
9820
+ type,
9821
+ bundle,
9822
+ attribute,
9823
+ baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9824
+ sourceSchemataFile = `${__dirname}/../../../../input/farmos.json`,
9825
+ overlayName,
9826
+ overlayDescription
9827
+ }) {
9828
+ let integralSchemataFile = JSON.parse(fs.readFileSync(sourceSchemataFile));
9829
+ this.type = type;
9830
+ this.bundle = bundle;
9831
+ this.overlayName = overlayName;
9832
+ this.overlayDescription = overlayDescription;
9833
+ this.attribute = attribute;
9834
+ this.sourceSchemata = integralSchemataFile;
9835
+ this.lexicon = [...findAllObjectAttributesInTree(this.sourceSchemata), ...findAllNonObjectAttributesInTree(this.sourceSchemata)];
9836
+ this.baseSchemataFolder = baseSchemataFolder;
9837
+ let involvedSchemata = this.lexicon.filter((entry) => this.attribute == entry.attribute).flatMap((entry) => entry.instances).filter((instance) => this.type ? instance.type == this.type : true).filter((instance) => this.bundle ? instance.bundle == this.bundle : true).map((d) => d.schema);
9838
+ this.targetSchemata = Array.from(new Set(involvedSchemata));
9839
+ this.overlays = [];
9840
+ this.targetSchemata.forEach((target) => {
9841
+ let currentOverlay = new AttributePatchingSchemaOverlay({
9842
+ typeAndBundle: target,
9843
+ attributeOverlayName: this.overlayName,
9844
+ baseSchemataFolder: this.baseSchemataFolder
9845
+ });
9846
+ this.overlays.push(currentOverlay);
9847
+ });
9848
+ }
9849
+ setRequire() {
9850
+ this.overlays.forEach((overlay) => {
9851
+ overlay.setRequiredAttribute(this.attribute);
9852
+ });
9853
+ }
9854
+ setGenericSection({
9855
+ section,
9856
+ description
9857
+ }) {
9858
+ this.overlays.forEach((overlay) => {
9859
+ overlay.setGenericSection({
9860
+ attribute: this.attribute,
9861
+ section,
9862
+ description,
9863
+ merge: false
9864
+ });
9865
+ });
9866
+ }
9867
+ setPattern({ pattern, string, description }) {
9868
+ this.overlays.forEach((overlay) => {
9869
+ overlay.setPattern({
9870
+ attribute: this.attribute,
9871
+ pattern,
9872
+ string,
9873
+ description
9874
+ });
9875
+ });
9876
+ }
9877
+ /**
9878
+ * An all involved schemata, sets the target attribute for this overlay as a constant as indicated by the given value.
9879
+ * @param {any} value -- The constant, which should be of the type allowed by the base schema for the attribute.
9880
+ * @param {string} description -- Describes the rationale behind the chosing of the value.
9881
+ */
9882
+ setConstant({ value, description }) {
9883
+ this.overlays.forEach((overlay) => {
9884
+ overlay.setConstant({
9885
+ attribute: this.attribute,
9886
+ value,
9887
+ description
9888
+ });
9889
+ });
9890
+ }
9891
+ /**
9892
+ * Sets this overlay's target attribute as a constrained to a list of possible values in all involved schemata.
9893
+ * @param {string} attribute -- Attribute name for the attribute which will be set to a constant.
9894
+ * @param {any[]} valuesArray -- The array of possible values, which should be of the type allowed by the base schema for the attribute.
9895
+ * @param {string} description -- Describes the rationale behind the chosing of the value.
9896
+ */
9897
+ setEnum({ valuesArray, description }) {
9898
+ this.overlays.forEach((overlay) => {
9899
+ overlay.setConstant({
9900
+ attribute: this.attribute,
9901
+ valuesArray,
9902
+ description
9903
+ });
9904
+ });
9905
+ }
9906
+ /**
9907
+ * Stores all involved schemas into the folder structure for `base schemata` (provided as the parameter `baseSchemataFolder`) which will be consumed by our conventions later.
9908
+ * @returns {} -- It returns objects containing all paths.
9909
+ */
9910
+ store() {
9911
+ let output = this.overlays.map((overlay) => {
9912
+ let operation = overlay.store();
9913
+ return operation;
9914
+ });
9915
+ return output;
9916
+ }
9917
+ };
9918
+ __name(_AttributeOverlay, "AttributeOverlay");
9919
+ var AttributeOverlay = _AttributeOverlay;
9708
9920
  exports.SchemaOverlay = SchemaOverlay;
9709
9921
  exports.ConventionSchema = ConventionSchema;
9922
+ exports.AttributeOverlay = AttributeOverlay;
9710
9923
  }
9711
9924
  });
9712
9925
 
9713
9926
  // src/index.js
9714
9927
  var require_src = __commonJS({
9715
9928
  "src/index.js"(exports) {
9716
- var { SchemaOverlay, ConventionSchema } = require_convention_builder();
9929
+ var { SchemaOverlay, ConventionSchema, AttributeOverlay } = require_convention_builder();
9717
9930
  var schemaUtils = require_schema_utilities();
9718
9931
  exports.SchemaOverlay = SchemaOverlay;
9932
+ exports.AttributeOverlay = AttributeOverlay;
9719
9933
  exports.ConventionSchema = ConventionSchema;
9720
9934
  exports.fixRelationshipDataField = schemaUtils.fixRelationshipDataField;
9721
9935
  exports.buildValidator = schemaUtils.buildValidator;
@@ -8427,10 +8427,15 @@ var require_schema_utilities = __commonJS({
8427
8427
  Object.assign(sharedObjectSection, acum);
8428
8428
  } else {
8429
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;
8430
+ let lastAttr = chunks.length == 0 ? chunks[0] : chunks[chunks.length - 1];
8431
+ if (lastAttr) {
8432
+ let remainingPath = path.replace(new RegExp(lastAttr.concat("$")), "").replace(/\.$/, "");
8433
+ let sharedObjectSection = dig(targetObject, remainingPath);
8434
+ sharedObjectSection[lastAttr] = value;
8435
+ } else {
8436
+ targetObject[path] = value;
8437
+ }
8438
+ ;
8434
8439
  }
8435
8440
  ;
8436
8441
  return targetObject;
@@ -8476,7 +8481,6 @@ var require_schema_utilities = __commonJS({
8476
8481
  fs.mkdirSync(`${targetFolder}/${typeKey}/${bundleKey}/`, { recursive: true }, console.error);
8477
8482
  let path = `${targetFolder}/${typeKey}/${bundleKey}/schema.json`;
8478
8483
  fs.writeFileSync(path, JSON.stringify(schema, null, " "));
8479
- console.log(`writing ${typeKey}--${bundleKey}`);
8480
8484
  });
8481
8485
  });
8482
8486
  }
@@ -8532,7 +8536,6 @@ var require_schema_utilities = __commonJS({
8532
8536
  return files;
8533
8537
  }).filter((filename) => !/\./.test(filename));
8534
8538
  conventions.forEach((conventionName) => {
8535
- console.log(`Working on ${conventionName}`);
8536
8539
  try {
8537
8540
  let schema = JSON.parse(fs.readFileSync(`${conventionsFolder}/${conventionName}/schema.json`));
8538
8541
  schemasArgument.schemas.push(schema);
@@ -8767,6 +8770,9 @@ var require_schema_utilities = __commonJS({
8767
8770
  function organizeEntitiesArrayByRelationships({ entitiesArray, conventionObject }) {
8768
8771
  let attributesTree = conventionObject.getAttributesTree();
8769
8772
  let mainAttribute = Object.keys(attributesTree);
8773
+ return {
8774
+ tree: attributesTree
8775
+ };
8770
8776
  }
8771
8777
  __name(organizeEntitiesArrayByRelationships, "organizeEntitiesArrayByRelationships");
8772
8778
  function trimNonRequiredFields(schema, attribute = false) {
@@ -8927,7 +8933,14 @@ var require_schema_utilities = __commonJS({
8927
8933
  let schema = schemataTree[typeKey][bundleKey];
8928
8934
  let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8929
8935
  let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8930
- objectTypeAttributes.forEach((attr) => {
8936
+ objectTypeAttributes.flatMap((objAttr) => {
8937
+ let output2 = [];
8938
+ let subAttrs = Object.keys(schema.properties.attributes.properties[objAttr].properties);
8939
+ subAttrs.forEach((subAttr) => {
8940
+ output2.push(`${objAttr}.properties.${subAttr}`);
8941
+ });
8942
+ return output2;
8943
+ }).forEach((attr) => {
8931
8944
  let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8932
8945
  if (entry) {
8933
8946
  entry.instances.push(
@@ -8955,6 +8968,42 @@ var require_schema_utilities = __commonJS({
8955
8968
  return output;
8956
8969
  }
8957
8970
  __name(findAllObjectAttributesInTree, "findAllObjectAttributesInTree");
8971
+ function findAllNonObjectAttributesInTree(schemataTree) {
8972
+ let output = [];
8973
+ Object.keys(schemataTree).forEach((typeKey) => {
8974
+ Object.keys(schemataTree[typeKey]).forEach((bundleKey) => {
8975
+ let schema = schemataTree[typeKey][bundleKey];
8976
+ let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8977
+ let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8978
+ let otherAttributes = Array.from(topLevelAttributes).filter((attr) => !objectTypeAttributes.includes(attr));
8979
+ otherAttributes.forEach((attr) => {
8980
+ let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8981
+ if (entry) {
8982
+ entry.instances.push(
8983
+ {
8984
+ type: typeKey,
8985
+ bundle: bundleKey,
8986
+ schema: `${typeKey}--${bundleKey}`
8987
+ }
8988
+ );
8989
+ } else {
8990
+ output.push({
8991
+ attribute: attr,
8992
+ instances: [
8993
+ {
8994
+ type: typeKey,
8995
+ bundle: bundleKey,
8996
+ schema: `${typeKey}--${bundleKey}`
8997
+ }
8998
+ ]
8999
+ });
9000
+ }
9001
+ });
9002
+ });
9003
+ });
9004
+ return output;
9005
+ }
9006
+ __name(findAllNonObjectAttributesInTree, "findAllNonObjectAttributesInTree");
8958
9007
  exports2.dig = dig;
8959
9008
  exports2.bury = bury;
8960
9009
  exports2.fixRelationshipDataField = fixRelationshipDataField;
@@ -8975,6 +9024,7 @@ var require_schema_utilities = __commonJS({
8975
9024
  exports2.flattenJSONSchema = flattenJSONSchema;
8976
9025
  exports2.listAttributesAndSubAttributes = listAttributesAndSubAttributes;
8977
9026
  exports2.findAllObjectAttributesInTree = findAllObjectAttributesInTree;
9027
+ exports2.findAllNonObjectAttributesInTree = findAllNonObjectAttributesInTree;
8978
9028
  }
8979
9029
  });
8980
9030
 
@@ -8982,7 +9032,15 @@ var require_schema_utilities = __commonJS({
8982
9032
  var require_convention_builder = __commonJS({
8983
9033
  "src/convention_builder.js"(exports2) {
8984
9034
  var fs = require("fs");
8985
- var { buildValidator, urlToStringPair, dig, bury, listAttributesAndSubAttributes } = require_schema_utilities();
9035
+ var {
9036
+ buildValidator,
9037
+ urlToStringPair,
9038
+ dig,
9039
+ bury,
9040
+ listAttributesAndSubAttributes,
9041
+ findAllObjectAttributesInTree,
9042
+ findAllNonObjectAttributesInTree
9043
+ } = require_schema_utilities();
8986
9044
  function getAttributes(root, convention) {
8987
9045
  let branches = convention.relationships.filter((rel) => rel.containerEntity == root).map((d) => d.mentionedEntity);
8988
9046
  let output = {};
@@ -9003,7 +9061,8 @@ var require_convention_builder = __commonJS({
9003
9061
  originalSrcScript,
9004
9062
  trivial = false,
9005
9063
  baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9006
- requiredFields = []
9064
+ requiredFields = [],
9065
+ modifiedAttributes
9007
9066
  }) {
9008
9067
  this.name = name;
9009
9068
  this.typeAndBundle = typeAndBundle;
@@ -9038,7 +9097,7 @@ var require_convention_builder = __commonJS({
9038
9097
  ;
9039
9098
  this.baseSchema = baseSchema;
9040
9099
  this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9041
- this.modifiedAttributes = /* @__PURE__ */ new Set([]);
9100
+ this.modifiedAttributes = modifiedAttributes ? modifiedAttributes : /* @__PURE__ */ new Set([]);
9042
9101
  if (!overlay) {
9043
9102
  this.overlay = {
9044
9103
  properties: {
@@ -9054,11 +9113,17 @@ var require_convention_builder = __commonJS({
9054
9113
  this.schema = baseSchema;
9055
9114
  let validatorObj = buildValidator();
9056
9115
  this.validate = validatorObj.compile(this.schema);
9057
- this.updateSchema();
9058
9116
  }
9059
9117
  storagePath() {
9060
9118
  return `${this.storageFolder}`;
9061
9119
  }
9120
+ /**
9121
+ * Given an attribute path (such as "attributes.properties.geometry.properties.value"), it will set is as required in the adequate containing attribute (for this example, "attributes.properties.geometry.required").
9122
+ * @param {string} path -- Attribute path.
9123
+ * @returns {}
9124
+ */
9125
+ requireFieldByPath(path) {
9126
+ }
9062
9127
  /**
9063
9128
  * User input only works over the overlay, the schema should always be the direct result of applying the overlay over the baseSchema.
9064
9129
  */
@@ -9068,9 +9133,11 @@ var require_convention_builder = __commonJS({
9068
9133
  Object.assign(this.schema, this.baseSchema);
9069
9134
  let allModifiedAttributes = listAttributesAndSubAttributes(this.overlay);
9070
9135
  this.modifiedAttributes = allModifiedAttributes;
9136
+ this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9137
+ this.modifiedAttributes.forEach((attr) => this.updateAttributeStatus(attr));
9071
9138
  Array.from(this.modifiedAttributes).forEach((attr) => {
9072
9139
  let currentValue = dig(this.overlay.properties.attributes.properties, attr);
9073
- bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, true);
9140
+ bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, false);
9074
9141
  });
9075
9142
  let originalRequiredFields = this.schema.properties.attributes.required ? this.schema.properties.attributes.required : [];
9076
9143
  let allRequiredSet = Array.from(/* @__PURE__ */ new Set([...originalRequiredFields, ...Array.from(this.requiredFields)]));
@@ -9112,12 +9179,18 @@ var require_convention_builder = __commonJS({
9112
9179
  this.requiredFields.add(attribute);
9113
9180
  this.updateSchema();
9114
9181
  }
9115
- setGenericSection({ attribute, section, description }) {
9182
+ /**
9183
+ * The section body "section" will replace the previous schema content in full.
9184
+ * @param {} attribute
9185
+ * @param {} section
9186
+ * @param {} description
9187
+ */
9188
+ setGenericSection({ attribute, section, description, merge = true }) {
9116
9189
  this.checkAttributeStatus(attribute);
9117
- bury(this.overlay.properties.attributes.properties, attribute, section, true);
9190
+ bury(this.overlay.properties.attributes.properties, attribute, section, merge);
9118
9191
  if (attribute.match(/\./)) {
9119
9192
  let firstLevel = attribute.split(/\./)[0];
9120
- this.overlay.properties.attributes.properties[firstLevel].type = "object";
9193
+ bury(this.overlay.properties.attributes.properties, `${firstLevel}.type`, "object", merge);
9121
9194
  }
9122
9195
  ;
9123
9196
  if (description) {
@@ -9254,6 +9327,42 @@ var require_convention_builder = __commonJS({
9254
9327
  };
9255
9328
  __name(_SchemaOverlay, "SchemaOverlay");
9256
9329
  var SchemaOverlay2 = _SchemaOverlay;
9330
+ var _AttributePatchingSchemaOverlay = class _AttributePatchingSchemaOverlay extends SchemaOverlay2 {
9331
+ constructor({
9332
+ typeAndBundle,
9333
+ attributeOverlayName,
9334
+ repoURL,
9335
+ strictEnums = true,
9336
+ baseSchemataFolder = `${__dirname}/../../../../input/collection`
9337
+ }) {
9338
+ super({
9339
+ typeAndBundle,
9340
+ repoURL,
9341
+ storageFolder: baseSchemataFolder,
9342
+ baseSchemataFolder,
9343
+ strictEnums
9344
+ });
9345
+ this.attributeOverlayName = attributeOverlayName;
9346
+ }
9347
+ store() {
9348
+ this.updateSchema();
9349
+ if (this.schema.appliedOverlays) {
9350
+ this.schema.appliedOverlays.push(this.attributeOverlayName);
9351
+ } else {
9352
+ this.schema.appliedOverlays = [this.attributeOverlayName];
9353
+ }
9354
+ ;
9355
+ let targetPath = `${this.storagePath()}/${this.typeAndBundle.split("--")[0]}/${this.typeAndBundle.split("--")[1]}/schema.json`;
9356
+ fs.writeFileSync(targetPath, JSON.stringify(this.schema, null, " "), console.error);
9357
+ let output = {
9358
+ path: targetPath,
9359
+ test: false
9360
+ };
9361
+ return output;
9362
+ }
9363
+ };
9364
+ __name(_AttributePatchingSchemaOverlay, "AttributePatchingSchemaOverlay");
9365
+ var AttributePatchingSchemaOverlay = _AttributePatchingSchemaOverlay;
9257
9366
  var _ConventionSchema = class _ConventionSchema {
9258
9367
  constructor({
9259
9368
  title,
@@ -9365,6 +9474,7 @@ var require_convention_builder = __commonJS({
9365
9474
  required: this.required
9366
9475
  };
9367
9476
  Object.keys(this.overlays).forEach((attributeName) => {
9477
+ this.overlays[attributeName].updateSchema();
9368
9478
  this.schema.properties[attributeName] = this.overlays[attributeName].schema;
9369
9479
  delete this.schema.properties[attributeName].$id;
9370
9480
  if (!this.overlays[attributeName].originalSrcScript) {
@@ -9698,15 +9808,119 @@ import TabItem from '@theme/TabItem';
9698
9808
  };
9699
9809
  __name(_ConventionSchema, "ConventionSchema");
9700
9810
  var ConventionSchema2 = _ConventionSchema;
9811
+ var _AttributeOverlay = class _AttributeOverlay {
9812
+ constructor({
9813
+ type,
9814
+ bundle,
9815
+ attribute,
9816
+ baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9817
+ sourceSchemataFile = `${__dirname}/../../../../input/farmos.json`,
9818
+ overlayName,
9819
+ overlayDescription
9820
+ }) {
9821
+ let integralSchemataFile = JSON.parse(fs.readFileSync(sourceSchemataFile));
9822
+ this.type = type;
9823
+ this.bundle = bundle;
9824
+ this.overlayName = overlayName;
9825
+ this.overlayDescription = overlayDescription;
9826
+ this.attribute = attribute;
9827
+ this.sourceSchemata = integralSchemataFile;
9828
+ this.lexicon = [...findAllObjectAttributesInTree(this.sourceSchemata), ...findAllNonObjectAttributesInTree(this.sourceSchemata)];
9829
+ this.baseSchemataFolder = baseSchemataFolder;
9830
+ let involvedSchemata = this.lexicon.filter((entry) => this.attribute == entry.attribute).flatMap((entry) => entry.instances).filter((instance) => this.type ? instance.type == this.type : true).filter((instance) => this.bundle ? instance.bundle == this.bundle : true).map((d) => d.schema);
9831
+ this.targetSchemata = Array.from(new Set(involvedSchemata));
9832
+ this.overlays = [];
9833
+ this.targetSchemata.forEach((target) => {
9834
+ let currentOverlay = new AttributePatchingSchemaOverlay({
9835
+ typeAndBundle: target,
9836
+ attributeOverlayName: this.overlayName,
9837
+ baseSchemataFolder: this.baseSchemataFolder
9838
+ });
9839
+ this.overlays.push(currentOverlay);
9840
+ });
9841
+ }
9842
+ setRequire() {
9843
+ this.overlays.forEach((overlay) => {
9844
+ overlay.setRequiredAttribute(this.attribute);
9845
+ });
9846
+ }
9847
+ setGenericSection({
9848
+ section,
9849
+ description
9850
+ }) {
9851
+ this.overlays.forEach((overlay) => {
9852
+ overlay.setGenericSection({
9853
+ attribute: this.attribute,
9854
+ section,
9855
+ description,
9856
+ merge: false
9857
+ });
9858
+ });
9859
+ }
9860
+ setPattern({ pattern, string, description }) {
9861
+ this.overlays.forEach((overlay) => {
9862
+ overlay.setPattern({
9863
+ attribute: this.attribute,
9864
+ pattern,
9865
+ string,
9866
+ description
9867
+ });
9868
+ });
9869
+ }
9870
+ /**
9871
+ * An all involved schemata, sets the target attribute for this overlay as a constant as indicated by the given value.
9872
+ * @param {any} value -- The constant, which should be of the type allowed by the base schema for the attribute.
9873
+ * @param {string} description -- Describes the rationale behind the chosing of the value.
9874
+ */
9875
+ setConstant({ value, description }) {
9876
+ this.overlays.forEach((overlay) => {
9877
+ overlay.setConstant({
9878
+ attribute: this.attribute,
9879
+ value,
9880
+ description
9881
+ });
9882
+ });
9883
+ }
9884
+ /**
9885
+ * Sets this overlay's target attribute as a constrained to a list of possible values in all involved schemata.
9886
+ * @param {string} attribute -- Attribute name for the attribute which will be set to a constant.
9887
+ * @param {any[]} valuesArray -- The array of possible values, which should be of the type allowed by the base schema for the attribute.
9888
+ * @param {string} description -- Describes the rationale behind the chosing of the value.
9889
+ */
9890
+ setEnum({ valuesArray, description }) {
9891
+ this.overlays.forEach((overlay) => {
9892
+ overlay.setConstant({
9893
+ attribute: this.attribute,
9894
+ valuesArray,
9895
+ description
9896
+ });
9897
+ });
9898
+ }
9899
+ /**
9900
+ * Stores all involved schemas into the folder structure for `base schemata` (provided as the parameter `baseSchemataFolder`) which will be consumed by our conventions later.
9901
+ * @returns {} -- It returns objects containing all paths.
9902
+ */
9903
+ store() {
9904
+ let output = this.overlays.map((overlay) => {
9905
+ let operation = overlay.store();
9906
+ return operation;
9907
+ });
9908
+ return output;
9909
+ }
9910
+ };
9911
+ __name(_AttributeOverlay, "AttributeOverlay");
9912
+ var AttributeOverlay2 = _AttributeOverlay;
9701
9913
  exports2.SchemaOverlay = SchemaOverlay2;
9702
9914
  exports2.ConventionSchema = ConventionSchema2;
9915
+ exports2.AttributeOverlay = AttributeOverlay2;
9703
9916
  }
9704
9917
  });
9705
9918
 
9706
9919
  // src/index.js
9707
- var { SchemaOverlay, ConventionSchema } = require_convention_builder();
9920
+ var { SchemaOverlay, ConventionSchema, AttributeOverlay } = require_convention_builder();
9708
9921
  var schemaUtils = require_schema_utilities();
9709
9922
  exports.SchemaOverlay = SchemaOverlay;
9923
+ exports.AttributeOverlay = AttributeOverlay;
9710
9924
  exports.ConventionSchema = ConventionSchema;
9711
9925
  exports.fixRelationshipDataField = schemaUtils.fixRelationshipDataField;
9712
9926
  exports.buildValidator = schemaUtils.buildValidator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convention_builder",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
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",