cda-schematron-validator 1.1.4 → 1.1.7

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.
@@ -16,39 +16,12 @@ const options = {
16
16
  includeWarnings: true
17
17
  };
18
18
 
19
- describe('Validator should', function() {
19
+ describe('Vital Sign async schematron validation should', function() {
20
20
  let results;
21
21
  it('return results', async function() {
22
22
  results = await validator.validateAsync(xml, schematron, options);
23
23
  expect(results).to.be.an('object');
24
24
  });
25
- it('return errorCount', function() {
26
- expect(results.errorCount).to.be.a('number');
27
- });
28
- it('return warningCount', function() {
29
- expect(results.warningCount).to.be.a('number');
30
- });
31
- it('return ignoredCount', function() {
32
- expect(results.ignoredCount).to.be.a('number');
33
- });
34
- it('return errors array', function() {
35
- expect(results.errors).to.be.a('array');
36
- });
37
- it('return warnings array', function() {
38
- expect(results.warnings).to.be.a('array');
39
- });
40
- it('return ignored array', function() {
41
- expect(results.ignored).to.be.a('array');
42
- });
43
- it('return matching errorCount', function() {
44
- expect(results.errorCount).to.be.equal(results.errors.length);
45
- });
46
- it('return matching warningCount', function() {
47
- expect(results.warningCount).to.be.equal(results.warnings.length);
48
- });
49
- it('return matching ignoredCount', function() {
50
- expect(results.ignoredCount).to.be.equal(results.ignored.length);
51
- });
52
25
  it('return correct number of errors', function() {
53
26
  expect(results.errorCount).to.be.equal(1);
54
27
  });
@@ -58,72 +31,4 @@ describe('Validator should', function() {
58
31
  it('return correct number of ignored', function() {
59
32
  expect(results.ignoredCount).to.be.equal(0);
60
33
  });
61
-
62
- it('return similar results without warnings', async function() {
63
- results = await validator.validateAsync(xml, schematron, { resourceDir: options.resourceDir });
64
- expect(results).to.be.an('object');
65
- expect(results.errorCount).to.be.a('number');
66
- expect(results.warningCount).to.be.a('number');
67
- expect(results.ignoredCount).to.be.a('number');
68
- expect(results.errors).to.be.a('array');
69
- expect(results.warnings).to.be.a('array');
70
- expect(results.ignored).to.be.a('array');
71
- expect(results.errorCount).to.be.equal(results.errors.length);
72
- expect(results.warningCount).to.be.equal(results.warnings.length);
73
- expect(results.ignoredCount).to.be.equal(results.ignored.length);
74
- expect(results.errorCount).to.be.equal(1);
75
- expect(results.warningCount).to.be.equal(0);
76
- expect(results.ignoredCount).to.be.equal(0);
77
- });
78
-
79
- it('return similar results given xml filepath', async function() {
80
- results = await validator.validateAsync(xmlPath, schematron, options);
81
- expect(results).to.be.an('object');
82
- expect(results.errorCount).to.be.a('number');
83
- expect(results.warningCount).to.be.a('number');
84
- expect(results.ignoredCount).to.be.a('number');
85
- expect(results.errors).to.be.a('array');
86
- expect(results.warnings).to.be.a('array');
87
- expect(results.ignored).to.be.a('array');
88
- expect(results.errorCount).to.be.equal(results.errors.length);
89
- expect(results.warningCount).to.be.equal(results.warnings.length);
90
- expect(results.ignoredCount).to.be.equal(results.ignored.length);
91
- expect(results.errorCount).to.be.equal(1);
92
- expect(results.warningCount).to.be.equal(6);
93
- expect(results.ignoredCount).to.be.equal(0);
94
- });
95
-
96
- it('return similar results given schematron filepath', async function() {
97
- results = await validator.validateAsync(xml, schematronPath, options);
98
- expect(results).to.be.an('object');
99
- expect(results.errorCount).to.be.a('number');
100
- expect(results.warningCount).to.be.a('number');
101
- expect(results.ignoredCount).to.be.a('number');
102
- expect(results.errors).to.be.a('array');
103
- expect(results.warnings).to.be.a('array');
104
- expect(results.ignored).to.be.a('array');
105
- expect(results.errorCount).to.be.equal(results.errors.length);
106
- expect(results.warningCount).to.be.equal(results.warnings.length);
107
- expect(results.ignoredCount).to.be.equal(results.ignored.length);
108
- expect(results.errorCount).to.be.equal(1);
109
- expect(results.warningCount).to.be.equal(6);
110
- expect(results.ignoredCount).to.be.equal(0);
111
- });
112
-
113
- it('return similar results given xml filepath and schematron filepath', async function() {
114
- results = await validator.validateAsync(xmlPath, schematronPath, options);
115
- expect(results).to.be.an('object');
116
- expect(results.errorCount).to.be.a('number');
117
- expect(results.warningCount).to.be.a('number');
118
- expect(results.ignoredCount).to.be.a('number');
119
- expect(results.errors).to.be.a('array');
120
- expect(results.warnings).to.be.a('array');
121
- expect(results.ignored).to.be.a('array');
122
- expect(results.errorCount).to.be.equal(results.errors.length);
123
- expect(results.warningCount).to.be.equal(results.warnings.length);
124
- expect(results.ignoredCount).to.be.equal(results.ignored.length);
125
- expect(results.errorCount).to.be.equal(1);
126
- expect(results.warningCount).to.be.equal(6);
127
- expect(results.ignoredCount).to.be.equal(0);
128
- });
129
34
  });
@@ -0,0 +1,117 @@
1
+ // crypto is now built-in node
2
+ const initializeProcess = require('./lib/initializeProcess');
3
+ const processRulesAsync = require('./lib/processRulesAsync');
4
+ const getReturnObject = require('./lib/getReturnObject');
5
+
6
+ // Parsed object cache
7
+ let parsedMap = Object.create({});
8
+ let contextMap = Object.create({});
9
+ let schematronMap = Object.create({});
10
+
11
+ function clearCache() {
12
+ parsedMap = Object.create({});
13
+ contextMap = Object.create({});
14
+ schematronMap = Object.create({});
15
+ }
16
+
17
+ /**
18
+ * @method validateAsync The entry method called to validate an xml with schematron. Calls `initializeProcess` and `processRulesAsync` methods, and gets the results.
19
+ * This is similar to `validate` method except that it uses asynchronous `processRulesAsync` method.
20
+ * @param {string} xml string content of an xml file or path to an xml file
21
+ * @param {string} schematron string content of a schematron file or path to a schematron file
22
+ * @param {object} options object that takes parameters
23
+ * `resourceDir`: Where additional documents with added rules such as value set restrictions are present. Default value is the same directory of the code being executed './'.,
24
+ * `xmlSnippetMaxLength`: Restrict the length of the xpath mapped xml snippet when returning the error/warning data to the user. Default value is 200 characters.,
25
+ * `includeWarnings`: User can choose to look for warnings and include them in the report. The default value is false.,
26
+ * `parsedSchematronMap`: User can choose to supply the schematron map instead of a schematron file avoiding the parsing of the schematron file for multiple xml files or check for cache.
27
+ * @returns Calls `getReturnObject` and gets the results (errors, warning, and ignored) as javascript objects
28
+ */
29
+
30
+ async function validateAsync(xml, schematron, options = {}) {
31
+ const resultInfo = {
32
+ errors: [],
33
+ warnings: [],
34
+ ignored: []
35
+ };
36
+ options.parsedMap = parsedMap;
37
+ options.contextMap = contextMap;
38
+ options.schematronMap = schematronMap;
39
+ initializeProcess(xml, schematron, options);
40
+ return Promise.all(
41
+ Object.keys(options.schematronMap.patternRuleMap).map(async (patternId) => {
42
+ if (options.schematronMap.patternRuleMap.hasOwnProperty(patternId)) {
43
+ const assertionInfo = Object.create({});
44
+ assertionInfo.patternId = patternId;
45
+ const rules = options.schematronMap.patternRuleMap[patternId];
46
+ await processRulesAsync(rules, assertionInfo, resultInfo, options);
47
+ }
48
+ })
49
+ ).then(() => {
50
+ return getReturnObject(resultInfo);
51
+ });
52
+ }
53
+
54
+ /**
55
+ * @method validateFileListAsync Run the validateAsync method on each xml file of the array provided, and combine the results into an array and return it.
56
+ * It is important to know that the results could be in a different order than the submitted files. We can't reliably identify which error belongs to which file.
57
+ * You should use this if all that you care about is the type of error, but not about in which file this error occurred.
58
+ * @param {array} xmlArray array of string content of an xml file or path to an xml file
59
+ * @param {string} schematron string content of a schematron file or path to a schematron file
60
+ * @param {object} options object that takes parameters
61
+ * `resourceDir`: Where additional documents with added rules such as value set restrictions are present. Default value is the same directory of the code being executed './'.,
62
+ * `xmlSnippetMaxLength`: Restrict the length of the xpath mapped xml snippet when returning the error/warning data to the user. Default value is 200 characters.,
63
+ * `includeWarnings`: User can choose to look for warnings and include them in the report. The default value is false.,
64
+ * `parsedSchematronMap`: User can choose to supply the schematron map instead of a schematron file avoiding the parsing of the schematron file for multiple xml files or check for cache.
65
+ * @returns {array} gets array of return objects with results
66
+ */
67
+
68
+ async function validateFileListAsync(xmlArray, schematron, options = {}) {
69
+ const resultInfo = [];
70
+ return Promise.all(
71
+ xmlArray.map(async (xml, index) => {
72
+ const resultObject = await validateAsync(xml, schematron, options);
73
+ resultObject.fileIndex = index;
74
+ resultInfo.push(resultObject);
75
+ console.log(`Number of files processed: ${resultInfo.length}`);
76
+ console.log(`Processed file index: ${index}`);
77
+ })
78
+ ).then(() => {
79
+ return resultInfo;
80
+ });
81
+ }
82
+
83
+ /**
84
+ * @method validateFileObjectAsync Run the validateAsync method on each xml file of the xml data object provided, and combine the results into an array and return it.
85
+ * It is important to know that the results could be in a different order than the submitted files. We can't reliably identify which error belongs to which file.
86
+ * You should use this if all that you care about is the type of error, but not about in which file this error occurred.
87
+ * @param {object} xmlDataObject object with key identifying the xml and value to be the xml file data or xml file path
88
+ * @param {string} schematron string content of a schematron file or path to a schematron file
89
+ * @param {object} options object that takes parameters
90
+ * `resourceDir`: Where additional documents with added rules such as value set restrictions are present. Default value is the same directory of the code being executed './'.,
91
+ * `xmlSnippetMaxLength`: Restrict the length of the xpath mapped xml snippet when returning the error/warning data to the user. Default value is 200 characters.,
92
+ * `includeWarnings`: User can choose to look for warnings and include them in the report. The default value is false.,
93
+ * `parsedSchematronMap`: User can choose to supply the schematron map instead of a schematron file avoiding the parsing of the schematron file for multiple xml files or check for cache.
94
+ * @returns {array} gets array of return objects with results
95
+ */
96
+
97
+ async function validateFileObjectAsync(xmlDataObject, schematron, options = {}) {
98
+ const resultInfo = [];
99
+ return Promise.all(
100
+ Object.entries(xmlDataObject).map(async ([xmlFileInfo, xml]) => {
101
+ const resultObject = await validateAsync(xml, schematron, options);
102
+ resultObject.fileInfo = xmlFileInfo;
103
+ resultInfo.push(resultObject);
104
+ console.log(`Number of files processed: ${resultInfo.length}`);
105
+ console.log(`Processed file info: ${xmlFileInfo}`);
106
+ })
107
+ ).then(() => {
108
+ return resultInfo;
109
+ });
110
+ }
111
+
112
+ module.exports = {
113
+ validateAsync,
114
+ validateFileListAsync,
115
+ validateFileObjectAsync,
116
+ clearCache
117
+ };
@@ -0,0 +1,55 @@
1
+ // crypto is now built-in node
2
+ const initializeProcess = require('./lib/initializeProcess');
3
+ const processRules = require('./lib/processRules');
4
+ const getReturnObject = require('./lib/getReturnObject');
5
+
6
+ // Parsed object cache
7
+ let parsedMap = Object.create({});
8
+ let contextMap = Object.create({});
9
+ let schematronMap = Object.create({});
10
+
11
+ function clearCache() {
12
+ parsedMap = Object.create({});
13
+ contextMap = Object.create({});
14
+ schematronMap = Object.create({});
15
+ }
16
+
17
+
18
+ /**
19
+ * @method validate The entry method called to validate an xml with schematron. Calls `initializeProcess` and `processRules` methods, and gets the results
20
+ * @param {string} xml string content of an xml file or path to an xml file
21
+ * @param {string} schematron string content of a schematron file or path to a schematron file
22
+ * @param {object} options object that takes parameters
23
+ * `resourceDir`: Where additional documents with added rules such as value set restrictions are present. Default value is the same directory of the code being executed './'.,
24
+ * `xmlSnippetMaxLength`: Restrict the length of the xpath mapped xml snippet when returning the error/warning data to the user. Default value is 200 characters.,
25
+ * `includeWarnings`: User can choose to look for warnings and include them in the report. The default value is false.,
26
+ * `parsedSchematronMap`: User can choose to supply the schematron map instead of a schematron file avoiding the parsing of the schematron file for multiple xml files or check for cache.
27
+ * @returns Calls `getReturnObject` and gets the results (errors, warning, and ignored) as javascript objects
28
+ */
29
+
30
+ function validateSync(xml, schematron, options = {}) {
31
+ const resultInfo = {
32
+ errors: [],
33
+ warnings: [],
34
+ ignored: []
35
+ };
36
+ options.parsedMap = parsedMap;
37
+ options.contextMap = contextMap;
38
+ options.schematronMap = schematronMap;
39
+ initializeProcess(xml, schematron, options);
40
+ for (const patternId in options.schematronMap.patternRuleMap) {
41
+ if (!options.schematronMap.patternRuleMap.hasOwnProperty(patternId)) {
42
+ continue;
43
+ }
44
+ const assertionInfo = Object.create({});
45
+ assertionInfo.patternId = patternId;
46
+ const rules = options.schematronMap.patternRuleMap[patternId];
47
+ processRules(rules, assertionInfo, resultInfo, options);
48
+ }
49
+ return getReturnObject(resultInfo);
50
+ }
51
+
52
+ module.exports = {
53
+ validateSync,
54
+ clearCache
55
+ };