convention_builder 1.4.0 → 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.
@@ -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
  }
@@ -8530,12 +8534,15 @@ var require_schema_utilities = __commonJS({
8530
8534
  let mapping = {};
8531
8535
  let conventions = fs.readdirSync(`${conventionsFolder}`, (error, files) => {
8532
8536
  return files;
8533
- });
8537
+ }).filter((filename) => !/\./.test(filename));
8534
8538
  conventions.forEach((conventionName) => {
8535
- console.log(`Working on ${conventionName}`);
8536
- let schema = JSON.parse(fs.readFileSync(`${conventionsFolder}/${conventionName}/schema.json`));
8537
- schemasArgument.schemas.push(schema);
8538
- mapping[conventionName] = schema.$id;
8539
+ try {
8540
+ let schema = JSON.parse(fs.readFileSync(`${conventionsFolder}/${conventionName}/schema.json`));
8541
+ schemasArgument.schemas.push(schema);
8542
+ mapping[conventionName] = schema.$id;
8543
+ } catch (e) {
8544
+ }
8545
+ ;
8539
8546
  });
8540
8547
  let validatorObj = buildValidator(extraAJVArguments = schemasArgument);
8541
8548
  let moduleCode = standaloneCode(validatorObj, mapping);
@@ -8763,6 +8770,9 @@ var require_schema_utilities = __commonJS({
8763
8770
  function organizeEntitiesArrayByRelationships({ entitiesArray, conventionObject }) {
8764
8771
  let attributesTree = conventionObject.getAttributesTree();
8765
8772
  let mainAttribute = Object.keys(attributesTree);
8773
+ return {
8774
+ tree: attributesTree
8775
+ };
8766
8776
  }
8767
8777
  __name(organizeEntitiesArrayByRelationships, "organizeEntitiesArrayByRelationships");
8768
8778
  function trimNonRequiredFields(schema, attribute = false) {
@@ -8911,7 +8921,6 @@ var require_schema_utilities = __commonJS({
8911
8921
  let subAttrs = Object.keys(schema.properties.attributes.properties[objAttr].properties);
8912
8922
  subAttrs.forEach((subAttr) => {
8913
8923
  output.add(`${objAttr}.properties.${subAttr}`);
8914
- output.delete(`${objAttr}`);
8915
8924
  });
8916
8925
  });
8917
8926
  return output;
@@ -8924,7 +8933,14 @@ var require_schema_utilities = __commonJS({
8924
8933
  let schema = schemataTree[typeKey][bundleKey];
8925
8934
  let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8926
8935
  let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8927
- 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) => {
8928
8944
  let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8929
8945
  if (entry) {
8930
8946
  entry.instances.push(
@@ -8952,6 +8968,42 @@ var require_schema_utilities = __commonJS({
8952
8968
  return output;
8953
8969
  }
8954
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");
8955
9007
  exports2.dig = dig;
8956
9008
  exports2.bury = bury;
8957
9009
  exports2.fixRelationshipDataField = fixRelationshipDataField;
@@ -8972,6 +9024,7 @@ var require_schema_utilities = __commonJS({
8972
9024
  exports2.flattenJSONSchema = flattenJSONSchema;
8973
9025
  exports2.listAttributesAndSubAttributes = listAttributesAndSubAttributes;
8974
9026
  exports2.findAllObjectAttributesInTree = findAllObjectAttributesInTree;
9027
+ exports2.findAllNonObjectAttributesInTree = findAllNonObjectAttributesInTree;
8975
9028
  }
8976
9029
  });
8977
9030
 
@@ -8979,7 +9032,15 @@ var require_schema_utilities = __commonJS({
8979
9032
  var require_convention_builder = __commonJS({
8980
9033
  "src/convention_builder.js"(exports2) {
8981
9034
  var fs = require("fs");
8982
- 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();
8983
9044
  function getAttributes(root, convention) {
8984
9045
  let branches = convention.relationships.filter((rel) => rel.containerEntity == root).map((d) => d.mentionedEntity);
8985
9046
  let output = {};
@@ -9000,7 +9061,8 @@ var require_convention_builder = __commonJS({
9000
9061
  originalSrcScript,
9001
9062
  trivial = false,
9002
9063
  baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9003
- requiredFields = []
9064
+ requiredFields = [],
9065
+ modifiedAttributes
9004
9066
  }) {
9005
9067
  this.name = name;
9006
9068
  this.typeAndBundle = typeAndBundle;
@@ -9035,7 +9097,7 @@ var require_convention_builder = __commonJS({
9035
9097
  ;
9036
9098
  this.baseSchema = baseSchema;
9037
9099
  this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9038
- this.modifiedAttributes = /* @__PURE__ */ new Set([]);
9100
+ this.modifiedAttributes = modifiedAttributes ? modifiedAttributes : /* @__PURE__ */ new Set([]);
9039
9101
  if (!overlay) {
9040
9102
  this.overlay = {
9041
9103
  properties: {
@@ -9051,11 +9113,17 @@ var require_convention_builder = __commonJS({
9051
9113
  this.schema = baseSchema;
9052
9114
  let validatorObj = buildValidator();
9053
9115
  this.validate = validatorObj.compile(this.schema);
9054
- this.updateSchema();
9055
9116
  }
9056
9117
  storagePath() {
9057
9118
  return `${this.storageFolder}`;
9058
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
+ }
9059
9127
  /**
9060
9128
  * User input only works over the overlay, the schema should always be the direct result of applying the overlay over the baseSchema.
9061
9129
  */
@@ -9065,9 +9133,11 @@ var require_convention_builder = __commonJS({
9065
9133
  Object.assign(this.schema, this.baseSchema);
9066
9134
  let allModifiedAttributes = listAttributesAndSubAttributes(this.overlay);
9067
9135
  this.modifiedAttributes = allModifiedAttributes;
9136
+ this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9137
+ this.modifiedAttributes.forEach((attr) => this.updateAttributeStatus(attr));
9068
9138
  Array.from(this.modifiedAttributes).forEach((attr) => {
9069
9139
  let currentValue = dig(this.overlay.properties.attributes.properties, attr);
9070
- bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, true);
9140
+ bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, false);
9071
9141
  });
9072
9142
  let originalRequiredFields = this.schema.properties.attributes.required ? this.schema.properties.attributes.required : [];
9073
9143
  let allRequiredSet = Array.from(/* @__PURE__ */ new Set([...originalRequiredFields, ...Array.from(this.requiredFields)]));
@@ -9109,12 +9179,18 @@ var require_convention_builder = __commonJS({
9109
9179
  this.requiredFields.add(attribute);
9110
9180
  this.updateSchema();
9111
9181
  }
9112
- 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 }) {
9113
9189
  this.checkAttributeStatus(attribute);
9114
- bury(this.overlay.properties.attributes.properties, attribute, section, true);
9190
+ bury(this.overlay.properties.attributes.properties, attribute, section, merge);
9115
9191
  if (attribute.match(/\./)) {
9116
9192
  let firstLevel = attribute.split(/\./)[0];
9117
- this.overlay.properties.attributes.properties[firstLevel].type = "object";
9193
+ bury(this.overlay.properties.attributes.properties, `${firstLevel}.type`, "object", merge);
9118
9194
  }
9119
9195
  ;
9120
9196
  if (description) {
@@ -9217,7 +9293,10 @@ var require_convention_builder = __commonJS({
9217
9293
  let test2 = this.testExamples();
9218
9294
  valid = test2.success;
9219
9295
  if (!valid) {
9220
- throw new Error(`The 'testExamples' method failed. The overlay either lacks examples, or is rejecting a valid example or accepting an invalid example.`);
9296
+ throw new Error(`The 'testExamples' method failed. The schema ${this.name} either lacks examples, or is rejecting a valid example or accepting an invalid example.
9297
+ * Valid examples provided: ${test2.validExamples.length}
9298
+ * Error examples provided: ${test2.erroredExamples.length}
9299
+ * Failed examples observed: ${test2.failedExamples.length}.`);
9221
9300
  }
9222
9301
  ;
9223
9302
  if (!this.trivial) {
@@ -9248,6 +9327,42 @@ var require_convention_builder = __commonJS({
9248
9327
  };
9249
9328
  __name(_SchemaOverlay, "SchemaOverlay");
9250
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;
9251
9366
  var _ConventionSchema = class _ConventionSchema {
9252
9367
  constructor({
9253
9368
  title,
@@ -9359,6 +9474,7 @@ var require_convention_builder = __commonJS({
9359
9474
  required: this.required
9360
9475
  };
9361
9476
  Object.keys(this.overlays).forEach((attributeName) => {
9477
+ this.overlays[attributeName].updateSchema();
9362
9478
  this.schema.properties[attributeName] = this.overlays[attributeName].schema;
9363
9479
  delete this.schema.properties[attributeName].$id;
9364
9480
  if (!this.overlays[attributeName].originalSrcScript) {
@@ -9692,15 +9808,119 @@ import TabItem from '@theme/TabItem';
9692
9808
  };
9693
9809
  __name(_ConventionSchema, "ConventionSchema");
9694
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;
9695
9913
  exports2.SchemaOverlay = SchemaOverlay2;
9696
9914
  exports2.ConventionSchema = ConventionSchema2;
9915
+ exports2.AttributeOverlay = AttributeOverlay2;
9697
9916
  }
9698
9917
  });
9699
9918
 
9700
9919
  // src/index.js
9701
- var { SchemaOverlay, ConventionSchema } = require_convention_builder();
9920
+ var { SchemaOverlay, ConventionSchema, AttributeOverlay } = require_convention_builder();
9702
9921
  var schemaUtils = require_schema_utilities();
9703
9922
  exports.SchemaOverlay = SchemaOverlay;
9923
+ exports.AttributeOverlay = AttributeOverlay;
9704
9924
  exports.ConventionSchema = ConventionSchema;
9705
9925
  exports.fixRelationshipDataField = schemaUtils.fixRelationshipDataField;
9706
9926
  exports.buildValidator = schemaUtils.buildValidator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convention_builder",
3
- "version": "1.4.0",
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",