convention_builder 1.4.2 → 1.4.4

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.
@@ -8782,6 +8782,47 @@ var require_schema_utilities = __commonJS({
8782
8782
  };
8783
8783
  }
8784
8784
  __name(organizeEntitiesArrayByRelationships, "organizeEntitiesArrayByRelationships");
8785
+ function arrayToStructuredConvention({ entitiesArray, conventionObject }) {
8786
+ let entities = entitiesArray.map((entity) => {
8787
+ let validations = Object.keys(conventionObject.overlays).map((key) => {
8788
+ var _a;
8789
+ conventionObject.overlays[key].validate(entity);
8790
+ let errors = (_a = conventionObject.overlays[key].validate.errors) == null ? void 0 : _a.filter((d) => !/relationships/.test(d.instancePath));
8791
+ return { overlay: key, errors: errors ? errors : [] };
8792
+ });
8793
+ let passedTests = validations.filter((d) => d.errors.length == 0);
8794
+ let amountPassed = passedTests.length;
8795
+ let output2 = {
8796
+ passedTests,
8797
+ possibleOverlays: passedTests ? passedTests.map((d) => d.overlay) : void 0,
8798
+ allTests: validations,
8799
+ amountPassed,
8800
+ entity
8801
+ };
8802
+ return output2;
8803
+ });
8804
+ let output = {
8805
+ identifiedEntities: entities.filter((d) => d.amountPassed == 1),
8806
+ unrecognizedEntities: entities.filter((d) => d.amountPassed == 0),
8807
+ ambiguousEntities: entities.filter((d) => d.amountPassed > 1)
8808
+ };
8809
+ let assembledEntity = {
8810
+ id: randomUUID(),
8811
+ type: "object"
8812
+ };
8813
+ entities.filter((d) => d.possibleOverlays.length == 1).forEach((entityObj) => {
8814
+ assembledEntity[entityObj.possibleOverlays[0]] = entityObj.entity;
8815
+ });
8816
+ let assembledValidation = conventionObject.validate(assembledEntity);
8817
+ if (conventionObject.validate.errors) {
8818
+ output.errors = conventionObject.validate.errors;
8819
+ }
8820
+ ;
8821
+ output.is_valid = assembledValidation;
8822
+ output.assembled_entity = assembledEntity;
8823
+ return output;
8824
+ }
8825
+ __name(arrayToStructuredConvention, "arrayToStructuredConvention");
8785
8826
  function trimNonRequiredFields(schema, attribute = false) {
8786
8827
  let data;
8787
8828
  if (attribute) {
@@ -9025,6 +9066,7 @@ var require_schema_utilities = __commonJS({
9025
9066
  exports.findMissingEntitiesInExample = findMissingEntitiesInExample;
9026
9067
  exports.organizeEntitiesArrayIntoConvention = organizeEntitiesArrayIntoConvention;
9027
9068
  exports.organizeEntitiesArrayByRelationships = organizeEntitiesArrayByRelationships;
9069
+ exports.arrayToStructuredConvention = arrayToStructuredConvention;
9028
9070
  exports.trimNonRequiredFields = trimNonRequiredFields;
9029
9071
  exports.reduceSchemaToRequiredFields = reduceSchemaToRequiredFields;
9030
9072
  exports.flattenObjectBranch = flattenObjectBranch;
@@ -9377,8 +9419,8 @@ var require_convention_builder = __commonJS({
9377
9419
  version,
9378
9420
  repoURL,
9379
9421
  description,
9380
- validExamples,
9381
- erroredExamples,
9422
+ validExamples = [],
9423
+ erroredExamples = [],
9382
9424
  storageFolder = `${__dirname}/../../../../output/collection`,
9383
9425
  baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9384
9426
  strictEnums = false,
@@ -9405,7 +9447,10 @@ var require_convention_builder = __commonJS({
9405
9447
  if (overlays) {
9406
9448
  let overlayNames = Object.keys(overlays);
9407
9449
  this.overlays = {};
9408
- overlayNames.forEach((ovrl) => this.overlays[ovrl] = new SchemaOverlay(overlays[ovrl]));
9450
+ overlayNames.forEach((ovrl) => {
9451
+ this.overlays[ovrl] = new SchemaOverlay(overlays[ovrl]);
9452
+ this.overlays[ovrl].updateSchema();
9453
+ });
9409
9454
  }
9410
9455
  ;
9411
9456
  if (relationships) {
@@ -9699,17 +9744,73 @@ import TabItem from '@theme/TabItem';
9699
9744
  let body = [header, mainText, "# View Schema\n", "<Tabs>", schemaPluginInvocation, schemaPluginInvocationRequiredOnly, codeBlock, conventionAsText, "</Tabs>"].join("\n");
9700
9745
  return body;
9701
9746
  }
9747
+ getExamplesFromFile({ path }) {
9748
+ let isFolder = fs.lstatSync(path).isDirectory();
9749
+ let isJSONFile = new RegExp(/\.json$/i).test(path);
9750
+ let noFilesError = `The provided path (${path}) does not lead to JSON file nor a folder containing JSON files. Folder: ${isFolder}, JSON File: ${isJSONFile}`;
9751
+ let output = [];
9752
+ if (isFolder) {
9753
+ let folderExamples = fs.readdirSync(path, (error, files) => {
9754
+ return files;
9755
+ }).filter((path2) => new RegExp(/\.json$/i).test(path2)).map((filename) => {
9756
+ let examples = JSON.parse(fs.readFileSync(`${path}/${filename}`));
9757
+ return examples;
9758
+ });
9759
+ if (folderExamples.length == 0) {
9760
+ throw new Error(noFilesError);
9761
+ }
9762
+ ;
9763
+ output.push(...folderExamples);
9764
+ } else if (isJSONFile) {
9765
+ let example = JSON.parse(fs.readFileSync(path));
9766
+ output.push(example);
9767
+ } else {
9768
+ throw new Error(noFilesError);
9769
+ }
9770
+ ;
9771
+ return output;
9772
+ }
9773
+ /**
9774
+ * Will retrieve an individual example from a JSON file or all examples in a given folder. They all need to be either valid or invalid examples, and that should be indicated by the 'is_valid_example' attribute.
9775
+ * @param {string} path -- A string representing an absolute path, leading to either a JSON file containing an individual example or a folder containing several examples.
9776
+ * @param {object|array} object -- Either an array of examples or an individual example.
9777
+ * @param {boolean} is_valid_example -- Whether to store the provided examples as valid examples or errors, for the tests.
9778
+ */
9779
+ addExample({ path, object, is_valid_example }) {
9780
+ if (!path && !object) {
9781
+ throw new Error(`Either an object or a path leading to a folder or a json file need to be provided. Both arguments are empty.`);
9782
+ }
9783
+ ;
9784
+ let newExamples = [];
9785
+ if (object) {
9786
+ if (!Array.isArray(object)) {
9787
+ newExamples.push(object);
9788
+ } else {
9789
+ newExamples.push(...object);
9790
+ }
9791
+ ;
9792
+ } else if (path) {
9793
+ newExamples = this.getExamplesFromFile({ path });
9794
+ }
9795
+ ;
9796
+ if (is_valid_example) {
9797
+ this.validExamples.push(...newExamples);
9798
+ } else {
9799
+ this.erroredExamples.push(...newExamples);
9800
+ }
9801
+ ;
9802
+ }
9702
9803
  /**
9703
9804
  * 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).
9704
9805
  */
9705
9806
  testExamples() {
9706
9807
  let generalOutput = {};
9707
9808
  if (!this.validExamples || !this.erroredExamples) {
9708
- throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute`);
9809
+ throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute. Valid examples amount: ${this.validExamples ? this.validExamples.length : "validExamples is not an array"}, errored examples amount: ${this.erroredExamples ? this.erroredExamples.length : "erroredExamples is not an array"}.`);
9709
9810
  }
9710
9811
  ;
9711
9812
  if (this.validExamples.length == 0 || this.erroredExamples.length == 0) {
9712
- throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute`);
9813
+ throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute. Valid examples amount: ${this.validExamples ? this.validExamples.length : "validExamples is not an array"}, errored examples amount: ${this.erroredExamples ? this.erroredExamples.length : "erroredExamples is not an array"}.`);
9713
9814
  }
9714
9815
  ;
9715
9816
  this.updateSchema();
@@ -9942,7 +10043,7 @@ var require_src = __commonJS({
9942
10043
  exports.exampleImporter = schemaUtils.exampleImporter;
9943
10044
  exports.findMissingEntitiesInExample = schemaUtils.findMissingEntitiesInExample;
9944
10045
  exports.organizeEntitiesArrayIntoConvention = schemaUtils.organizeEntitiesArrayIntoConvention;
9945
- exports.organizeEntitiesArrayByRelationships = schemaUtils.organizeEntitiesArrayByRelationships;
10046
+ exports.arrayToStructuredConvention = schemaUtils.arrayToStructuredConvention;
9946
10047
  exports.trimNonRequiredFields = schemaUtils.trimNonRequiredFields;
9947
10048
  exports.reduceSchemaToRequiredFields = schemaUtils.reduceSchemaToRequiredFields;
9948
10049
  exports.flattenJSONSchema = schemaUtils.flattenJSONSchema;
@@ -8775,6 +8775,47 @@ var require_schema_utilities = __commonJS({
8775
8775
  };
8776
8776
  }
8777
8777
  __name(organizeEntitiesArrayByRelationships, "organizeEntitiesArrayByRelationships");
8778
+ function arrayToStructuredConvention({ entitiesArray, conventionObject }) {
8779
+ let entities = entitiesArray.map((entity) => {
8780
+ let validations = Object.keys(conventionObject.overlays).map((key) => {
8781
+ var _a;
8782
+ conventionObject.overlays[key].validate(entity);
8783
+ let errors = (_a = conventionObject.overlays[key].validate.errors) == null ? void 0 : _a.filter((d) => !/relationships/.test(d.instancePath));
8784
+ return { overlay: key, errors: errors ? errors : [] };
8785
+ });
8786
+ let passedTests = validations.filter((d) => d.errors.length == 0);
8787
+ let amountPassed = passedTests.length;
8788
+ let output2 = {
8789
+ passedTests,
8790
+ possibleOverlays: passedTests ? passedTests.map((d) => d.overlay) : void 0,
8791
+ allTests: validations,
8792
+ amountPassed,
8793
+ entity
8794
+ };
8795
+ return output2;
8796
+ });
8797
+ let output = {
8798
+ identifiedEntities: entities.filter((d) => d.amountPassed == 1),
8799
+ unrecognizedEntities: entities.filter((d) => d.amountPassed == 0),
8800
+ ambiguousEntities: entities.filter((d) => d.amountPassed > 1)
8801
+ };
8802
+ let assembledEntity = {
8803
+ id: randomUUID(),
8804
+ type: "object"
8805
+ };
8806
+ entities.filter((d) => d.possibleOverlays.length == 1).forEach((entityObj) => {
8807
+ assembledEntity[entityObj.possibleOverlays[0]] = entityObj.entity;
8808
+ });
8809
+ let assembledValidation = conventionObject.validate(assembledEntity);
8810
+ if (conventionObject.validate.errors) {
8811
+ output.errors = conventionObject.validate.errors;
8812
+ }
8813
+ ;
8814
+ output.is_valid = assembledValidation;
8815
+ output.assembled_entity = assembledEntity;
8816
+ return output;
8817
+ }
8818
+ __name(arrayToStructuredConvention, "arrayToStructuredConvention");
8778
8819
  function trimNonRequiredFields(schema, attribute = false) {
8779
8820
  let data;
8780
8821
  if (attribute) {
@@ -9018,6 +9059,7 @@ var require_schema_utilities = __commonJS({
9018
9059
  exports2.findMissingEntitiesInExample = findMissingEntitiesInExample;
9019
9060
  exports2.organizeEntitiesArrayIntoConvention = organizeEntitiesArrayIntoConvention;
9020
9061
  exports2.organizeEntitiesArrayByRelationships = organizeEntitiesArrayByRelationships;
9062
+ exports2.arrayToStructuredConvention = arrayToStructuredConvention;
9021
9063
  exports2.trimNonRequiredFields = trimNonRequiredFields;
9022
9064
  exports2.reduceSchemaToRequiredFields = reduceSchemaToRequiredFields;
9023
9065
  exports2.flattenObjectBranch = flattenObjectBranch;
@@ -9370,8 +9412,8 @@ var require_convention_builder = __commonJS({
9370
9412
  version,
9371
9413
  repoURL,
9372
9414
  description,
9373
- validExamples,
9374
- erroredExamples,
9415
+ validExamples = [],
9416
+ erroredExamples = [],
9375
9417
  storageFolder = `${__dirname}/../../../../output/collection`,
9376
9418
  baseSchemataFolder = `${__dirname}/../../../../input/collection`,
9377
9419
  strictEnums = false,
@@ -9398,7 +9440,10 @@ var require_convention_builder = __commonJS({
9398
9440
  if (overlays) {
9399
9441
  let overlayNames = Object.keys(overlays);
9400
9442
  this.overlays = {};
9401
- overlayNames.forEach((ovrl) => this.overlays[ovrl] = new SchemaOverlay2(overlays[ovrl]));
9443
+ overlayNames.forEach((ovrl) => {
9444
+ this.overlays[ovrl] = new SchemaOverlay2(overlays[ovrl]);
9445
+ this.overlays[ovrl].updateSchema();
9446
+ });
9402
9447
  }
9403
9448
  ;
9404
9449
  if (relationships) {
@@ -9692,17 +9737,73 @@ import TabItem from '@theme/TabItem';
9692
9737
  let body = [header, mainText, "# View Schema\n", "<Tabs>", schemaPluginInvocation, schemaPluginInvocationRequiredOnly, codeBlock, conventionAsText, "</Tabs>"].join("\n");
9693
9738
  return body;
9694
9739
  }
9740
+ getExamplesFromFile({ path }) {
9741
+ let isFolder = fs.lstatSync(path).isDirectory();
9742
+ let isJSONFile = new RegExp(/\.json$/i).test(path);
9743
+ let noFilesError = `The provided path (${path}) does not lead to JSON file nor a folder containing JSON files. Folder: ${isFolder}, JSON File: ${isJSONFile}`;
9744
+ let output = [];
9745
+ if (isFolder) {
9746
+ let folderExamples = fs.readdirSync(path, (error, files) => {
9747
+ return files;
9748
+ }).filter((path2) => new RegExp(/\.json$/i).test(path2)).map((filename) => {
9749
+ let examples = JSON.parse(fs.readFileSync(`${path}/${filename}`));
9750
+ return examples;
9751
+ });
9752
+ if (folderExamples.length == 0) {
9753
+ throw new Error(noFilesError);
9754
+ }
9755
+ ;
9756
+ output.push(...folderExamples);
9757
+ } else if (isJSONFile) {
9758
+ let example = JSON.parse(fs.readFileSync(path));
9759
+ output.push(example);
9760
+ } else {
9761
+ throw new Error(noFilesError);
9762
+ }
9763
+ ;
9764
+ return output;
9765
+ }
9766
+ /**
9767
+ * Will retrieve an individual example from a JSON file or all examples in a given folder. They all need to be either valid or invalid examples, and that should be indicated by the 'is_valid_example' attribute.
9768
+ * @param {string} path -- A string representing an absolute path, leading to either a JSON file containing an individual example or a folder containing several examples.
9769
+ * @param {object|array} object -- Either an array of examples or an individual example.
9770
+ * @param {boolean} is_valid_example -- Whether to store the provided examples as valid examples or errors, for the tests.
9771
+ */
9772
+ addExample({ path, object, is_valid_example }) {
9773
+ if (!path && !object) {
9774
+ throw new Error(`Either an object or a path leading to a folder or a json file need to be provided. Both arguments are empty.`);
9775
+ }
9776
+ ;
9777
+ let newExamples = [];
9778
+ if (object) {
9779
+ if (!Array.isArray(object)) {
9780
+ newExamples.push(object);
9781
+ } else {
9782
+ newExamples.push(...object);
9783
+ }
9784
+ ;
9785
+ } else if (path) {
9786
+ newExamples = this.getExamplesFromFile({ path });
9787
+ }
9788
+ ;
9789
+ if (is_valid_example) {
9790
+ this.validExamples.push(...newExamples);
9791
+ } else {
9792
+ this.erroredExamples.push(...newExamples);
9793
+ }
9794
+ ;
9795
+ }
9695
9796
  /**
9696
9797
  * 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).
9697
9798
  */
9698
9799
  testExamples() {
9699
9800
  let generalOutput = {};
9700
9801
  if (!this.validExamples || !this.erroredExamples) {
9701
- throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute`);
9802
+ throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute. Valid examples amount: ${this.validExamples ? this.validExamples.length : "validExamples is not an array"}, errored examples amount: ${this.erroredExamples ? this.erroredExamples.length : "erroredExamples is not an array"}.`);
9702
9803
  }
9703
9804
  ;
9704
9805
  if (this.validExamples.length == 0 || this.erroredExamples.length == 0) {
9705
- throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute`);
9806
+ throw new Error(`Testing can't happen because examples are missing either in the valid or errored attribute. Valid examples amount: ${this.validExamples ? this.validExamples.length : "validExamples is not an array"}, errored examples amount: ${this.erroredExamples ? this.erroredExamples.length : "erroredExamples is not an array"}.`);
9706
9807
  }
9707
9808
  ;
9708
9809
  this.updateSchema();
@@ -9933,7 +10034,7 @@ exports.apiComposeTaxonomiesTransformation = schemaUtils.apiComposeTaxonomiesTra
9933
10034
  exports.exampleImporter = schemaUtils.exampleImporter;
9934
10035
  exports.findMissingEntitiesInExample = schemaUtils.findMissingEntitiesInExample;
9935
10036
  exports.organizeEntitiesArrayIntoConvention = schemaUtils.organizeEntitiesArrayIntoConvention;
9936
- exports.organizeEntitiesArrayByRelationships = schemaUtils.organizeEntitiesArrayByRelationships;
10037
+ exports.arrayToStructuredConvention = schemaUtils.arrayToStructuredConvention;
9937
10038
  exports.trimNonRequiredFields = schemaUtils.trimNonRequiredFields;
9938
10039
  exports.reduceSchemaToRequiredFields = schemaUtils.reduceSchemaToRequiredFields;
9939
10040
  exports.flattenJSONSchema = schemaUtils.flattenJSONSchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "convention_builder",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
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",