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.
@@ -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
  }
@@ -8537,12 +8541,15 @@ var require_schema_utilities = __commonJS({
8537
8541
  let mapping = {};
8538
8542
  let conventions = fs.readdirSync(`${conventionsFolder}`, (error, files) => {
8539
8543
  return files;
8540
- });
8544
+ }).filter((filename) => !/\./.test(filename));
8541
8545
  conventions.forEach((conventionName) => {
8542
- console.log(`Working on ${conventionName}`);
8543
- let schema = JSON.parse(fs.readFileSync(`${conventionsFolder}/${conventionName}/schema.json`));
8544
- schemasArgument.schemas.push(schema);
8545
- mapping[conventionName] = schema.$id;
8546
+ try {
8547
+ let schema = JSON.parse(fs.readFileSync(`${conventionsFolder}/${conventionName}/schema.json`));
8548
+ schemasArgument.schemas.push(schema);
8549
+ mapping[conventionName] = schema.$id;
8550
+ } catch (e) {
8551
+ }
8552
+ ;
8546
8553
  });
8547
8554
  let validatorObj = buildValidator(extraAJVArguments = schemasArgument);
8548
8555
  let moduleCode = standaloneCode(validatorObj, mapping);
@@ -8770,6 +8777,9 @@ var require_schema_utilities = __commonJS({
8770
8777
  function organizeEntitiesArrayByRelationships({ entitiesArray, conventionObject }) {
8771
8778
  let attributesTree = conventionObject.getAttributesTree();
8772
8779
  let mainAttribute = Object.keys(attributesTree);
8780
+ return {
8781
+ tree: attributesTree
8782
+ };
8773
8783
  }
8774
8784
  __name(organizeEntitiesArrayByRelationships, "organizeEntitiesArrayByRelationships");
8775
8785
  function trimNonRequiredFields(schema, attribute = false) {
@@ -8918,7 +8928,6 @@ var require_schema_utilities = __commonJS({
8918
8928
  let subAttrs = Object.keys(schema.properties.attributes.properties[objAttr].properties);
8919
8929
  subAttrs.forEach((subAttr) => {
8920
8930
  output.add(`${objAttr}.properties.${subAttr}`);
8921
- output.delete(`${objAttr}`);
8922
8931
  });
8923
8932
  });
8924
8933
  return output;
@@ -8931,7 +8940,14 @@ var require_schema_utilities = __commonJS({
8931
8940
  let schema = schemataTree[typeKey][bundleKey];
8932
8941
  let topLevelAttributes = new Set(Object.keys(schema.properties.attributes.properties));
8933
8942
  let objectTypeAttributes = Array.from(topLevelAttributes).filter((attr) => schema.properties.attributes.properties[attr].type == "object");
8934
- 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) => {
8935
8951
  let entry = output.find((attrDescription) => attrDescription.attribute == attr);
8936
8952
  if (entry) {
8937
8953
  entry.instances.push(
@@ -8959,6 +8975,42 @@ var require_schema_utilities = __commonJS({
8959
8975
  return output;
8960
8976
  }
8961
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");
8962
9014
  exports.dig = dig;
8963
9015
  exports.bury = bury;
8964
9016
  exports.fixRelationshipDataField = fixRelationshipDataField;
@@ -8979,6 +9031,7 @@ var require_schema_utilities = __commonJS({
8979
9031
  exports.flattenJSONSchema = flattenJSONSchema;
8980
9032
  exports.listAttributesAndSubAttributes = listAttributesAndSubAttributes;
8981
9033
  exports.findAllObjectAttributesInTree = findAllObjectAttributesInTree;
9034
+ exports.findAllNonObjectAttributesInTree = findAllNonObjectAttributesInTree;
8982
9035
  }
8983
9036
  });
8984
9037
 
@@ -8986,7 +9039,15 @@ var require_schema_utilities = __commonJS({
8986
9039
  var require_convention_builder = __commonJS({
8987
9040
  "src/convention_builder.js"(exports) {
8988
9041
  var fs = __require("fs");
8989
- 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();
8990
9051
  function getAttributes(root, convention) {
8991
9052
  let branches = convention.relationships.filter((rel) => rel.containerEntity == root).map((d) => d.mentionedEntity);
8992
9053
  let output = {};
@@ -9007,7 +9068,8 @@ var require_convention_builder = __commonJS({
9007
9068
  originalSrcScript,
9008
9069
  trivial = false,
9009
9070
  baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9010
- requiredFields = []
9071
+ requiredFields = [],
9072
+ modifiedAttributes
9011
9073
  }) {
9012
9074
  this.name = name;
9013
9075
  this.typeAndBundle = typeAndBundle;
@@ -9042,7 +9104,7 @@ var require_convention_builder = __commonJS({
9042
9104
  ;
9043
9105
  this.baseSchema = baseSchema;
9044
9106
  this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9045
- this.modifiedAttributes = /* @__PURE__ */ new Set([]);
9107
+ this.modifiedAttributes = modifiedAttributes ? modifiedAttributes : /* @__PURE__ */ new Set([]);
9046
9108
  if (!overlay) {
9047
9109
  this.overlay = {
9048
9110
  properties: {
@@ -9058,11 +9120,17 @@ var require_convention_builder = __commonJS({
9058
9120
  this.schema = baseSchema;
9059
9121
  let validatorObj = buildValidator();
9060
9122
  this.validate = validatorObj.compile(this.schema);
9061
- this.updateSchema();
9062
9123
  }
9063
9124
  storagePath() {
9064
9125
  return `${this.storageFolder}`;
9065
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
+ }
9066
9134
  /**
9067
9135
  * User input only works over the overlay, the schema should always be the direct result of applying the overlay over the baseSchema.
9068
9136
  */
@@ -9072,9 +9140,11 @@ var require_convention_builder = __commonJS({
9072
9140
  Object.assign(this.schema, this.baseSchema);
9073
9141
  let allModifiedAttributes = listAttributesAndSubAttributes(this.overlay);
9074
9142
  this.modifiedAttributes = allModifiedAttributes;
9143
+ this.unmodifiedAttributes = listAttributesAndSubAttributes(this.baseSchema);
9144
+ this.modifiedAttributes.forEach((attr) => this.updateAttributeStatus(attr));
9075
9145
  Array.from(this.modifiedAttributes).forEach((attr) => {
9076
9146
  let currentValue = dig(this.overlay.properties.attributes.properties, attr);
9077
- bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, true);
9147
+ bury(this.schema, `properties.attributes.properties.${attr}`, currentValue, false);
9078
9148
  });
9079
9149
  let originalRequiredFields = this.schema.properties.attributes.required ? this.schema.properties.attributes.required : [];
9080
9150
  let allRequiredSet = Array.from(/* @__PURE__ */ new Set([...originalRequiredFields, ...Array.from(this.requiredFields)]));
@@ -9116,12 +9186,18 @@ var require_convention_builder = __commonJS({
9116
9186
  this.requiredFields.add(attribute);
9117
9187
  this.updateSchema();
9118
9188
  }
9119
- 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 }) {
9120
9196
  this.checkAttributeStatus(attribute);
9121
- bury(this.overlay.properties.attributes.properties, attribute, section, true);
9197
+ bury(this.overlay.properties.attributes.properties, attribute, section, merge);
9122
9198
  if (attribute.match(/\./)) {
9123
9199
  let firstLevel = attribute.split(/\./)[0];
9124
- this.overlay.properties.attributes.properties[firstLevel].type = "object";
9200
+ bury(this.overlay.properties.attributes.properties, `${firstLevel}.type`, "object", merge);
9125
9201
  }
9126
9202
  ;
9127
9203
  if (description) {
@@ -9224,7 +9300,10 @@ var require_convention_builder = __commonJS({
9224
9300
  let test2 = this.testExamples();
9225
9301
  valid = test2.success;
9226
9302
  if (!valid) {
9227
- throw new Error(`The 'testExamples' method failed. The overlay either lacks examples, or is rejecting a valid example or accepting an invalid example.`);
9303
+ 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.
9304
+ * Valid examples provided: ${test2.validExamples.length}
9305
+ * Error examples provided: ${test2.erroredExamples.length}
9306
+ * Failed examples observed: ${test2.failedExamples.length}.`);
9228
9307
  }
9229
9308
  ;
9230
9309
  if (!this.trivial) {
@@ -9255,6 +9334,42 @@ var require_convention_builder = __commonJS({
9255
9334
  };
9256
9335
  __name(_SchemaOverlay, "SchemaOverlay");
9257
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;
9258
9373
  var _ConventionSchema = class _ConventionSchema {
9259
9374
  constructor({
9260
9375
  title,
@@ -9366,6 +9481,7 @@ var require_convention_builder = __commonJS({
9366
9481
  required: this.required
9367
9482
  };
9368
9483
  Object.keys(this.overlays).forEach((attributeName) => {
9484
+ this.overlays[attributeName].updateSchema();
9369
9485
  this.schema.properties[attributeName] = this.overlays[attributeName].schema;
9370
9486
  delete this.schema.properties[attributeName].$id;
9371
9487
  if (!this.overlays[attributeName].originalSrcScript) {
@@ -9699,17 +9815,121 @@ import TabItem from '@theme/TabItem';
9699
9815
  };
9700
9816
  __name(_ConventionSchema, "ConventionSchema");
9701
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;
9702
9920
  exports.SchemaOverlay = SchemaOverlay;
9703
9921
  exports.ConventionSchema = ConventionSchema;
9922
+ exports.AttributeOverlay = AttributeOverlay;
9704
9923
  }
9705
9924
  });
9706
9925
 
9707
9926
  // src/index.js
9708
9927
  var require_src = __commonJS({
9709
9928
  "src/index.js"(exports) {
9710
- var { SchemaOverlay, ConventionSchema } = require_convention_builder();
9929
+ var { SchemaOverlay, ConventionSchema, AttributeOverlay } = require_convention_builder();
9711
9930
  var schemaUtils = require_schema_utilities();
9712
9931
  exports.SchemaOverlay = SchemaOverlay;
9932
+ exports.AttributeOverlay = AttributeOverlay;
9713
9933
  exports.ConventionSchema = ConventionSchema;
9714
9934
  exports.fixRelationshipDataField = schemaUtils.fixRelationshipDataField;
9715
9935
  exports.buildValidator = schemaUtils.buildValidator;