convention_builder 1.4.3 → 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.
- package/dist/module/index.js +60 -4
- package/dist/node/index.js +60 -4
- package/package.json +1 -1
package/dist/module/index.js
CHANGED
|
@@ -9419,8 +9419,8 @@ var require_convention_builder = __commonJS({
|
|
|
9419
9419
|
version,
|
|
9420
9420
|
repoURL,
|
|
9421
9421
|
description,
|
|
9422
|
-
validExamples,
|
|
9423
|
-
erroredExamples,
|
|
9422
|
+
validExamples = [],
|
|
9423
|
+
erroredExamples = [],
|
|
9424
9424
|
storageFolder = `${__dirname}/../../../../output/collection`,
|
|
9425
9425
|
baseSchemataFolder = `${__dirname}/../../../../input/collection`,
|
|
9426
9426
|
strictEnums = false,
|
|
@@ -9744,17 +9744,73 @@ import TabItem from '@theme/TabItem';
|
|
|
9744
9744
|
let body = [header, mainText, "# View Schema\n", "<Tabs>", schemaPluginInvocation, schemaPluginInvocationRequiredOnly, codeBlock, conventionAsText, "</Tabs>"].join("\n");
|
|
9745
9745
|
return body;
|
|
9746
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
|
+
}
|
|
9747
9803
|
/**
|
|
9748
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).
|
|
9749
9805
|
*/
|
|
9750
9806
|
testExamples() {
|
|
9751
9807
|
let generalOutput = {};
|
|
9752
9808
|
if (!this.validExamples || !this.erroredExamples) {
|
|
9753
|
-
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"}.`);
|
|
9754
9810
|
}
|
|
9755
9811
|
;
|
|
9756
9812
|
if (this.validExamples.length == 0 || this.erroredExamples.length == 0) {
|
|
9757
|
-
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"}.`);
|
|
9758
9814
|
}
|
|
9759
9815
|
;
|
|
9760
9816
|
this.updateSchema();
|
package/dist/node/index.js
CHANGED
|
@@ -9412,8 +9412,8 @@ var require_convention_builder = __commonJS({
|
|
|
9412
9412
|
version,
|
|
9413
9413
|
repoURL,
|
|
9414
9414
|
description,
|
|
9415
|
-
validExamples,
|
|
9416
|
-
erroredExamples,
|
|
9415
|
+
validExamples = [],
|
|
9416
|
+
erroredExamples = [],
|
|
9417
9417
|
storageFolder = `${__dirname}/../../../../output/collection`,
|
|
9418
9418
|
baseSchemataFolder = `${__dirname}/../../../../input/collection`,
|
|
9419
9419
|
strictEnums = false,
|
|
@@ -9737,17 +9737,73 @@ import TabItem from '@theme/TabItem';
|
|
|
9737
9737
|
let body = [header, mainText, "# View Schema\n", "<Tabs>", schemaPluginInvocation, schemaPluginInvocationRequiredOnly, codeBlock, conventionAsText, "</Tabs>"].join("\n");
|
|
9738
9738
|
return body;
|
|
9739
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
|
+
}
|
|
9740
9796
|
/**
|
|
9741
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).
|
|
9742
9798
|
*/
|
|
9743
9799
|
testExamples() {
|
|
9744
9800
|
let generalOutput = {};
|
|
9745
9801
|
if (!this.validExamples || !this.erroredExamples) {
|
|
9746
|
-
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"}.`);
|
|
9747
9803
|
}
|
|
9748
9804
|
;
|
|
9749
9805
|
if (this.validExamples.length == 0 || this.erroredExamples.length == 0) {
|
|
9750
|
-
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"}.`);
|
|
9751
9807
|
}
|
|
9752
9808
|
;
|
|
9753
9809
|
this.updateSchema();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "convention_builder",
|
|
3
|
-
"version": "1.4.
|
|
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",
|