cda-schematron-validator 1.1.6 → 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.
package/README.md CHANGED
@@ -2,11 +2,19 @@
2
2
 
3
3
  ## Release History
4
4
 
5
+ ### 1.1.7
6
+
7
+ - Introduced a new method called `validateFileObjectAsync` which allows to supply an object with key, value pairs where key represents the identifier of the xml, and the value represents xml file content or path to validate
8
+ - An array of results will be returned from the `validateFileObjectAsync` method
9
+ - Test file `test/asyncFileList.test.js` has the example usage
10
+ - The result object has the property `fileInfo` that would return the `key` the identifier of the xml to identify and debug issues
11
+ - The result object of the array result from the method `validateFileListAsync` now has the property `fileIndex` that will give an indication of which xml file the result belongs to for debugging purposes
12
+
5
13
  ### 1.1.6
6
14
 
7
15
  - Methods have been moved to js files and relocated to lib directory
8
- - Introduced a new method called validateFileListAsync which allows to supply an array of xml file content or paths to validate
9
- - An array of results will be returned from the validateFileListAsync method
16
+ - Introduced a new method called `validateFileListAsync` which allows to supply an array of xml file content or paths to validate
17
+ - An array of results will be returned from the `validateFileListAsync` method
10
18
  - Test file `test/asyncFileList.test.js` has the example usage
11
19
  - Started to make use of Promise.all for async methods
12
20
  - Documentation of the methods completed by 40%
package/lib/checkRule.js CHANGED
@@ -1,6 +1,18 @@
1
+ const _get = require('lodash.get');
1
2
  const getSelectXmlChunk = require('./getSelectXmlChunk');
2
- const processAssertions = require('./processAssertions');
3
+ const includeExternalDocument = require('./includeExternalDocument');
4
+ const testAssertion = require('./testAssertion');
5
+ const getAssertionObject = require('./getAssertionObject');
3
6
  // Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
7
+
8
+ /**
9
+ * @method checkRule Validate assertions and extension against the provided context of the xml with the given options
10
+ * @param {string} context Used to query the xml using xpath
11
+ * @param {array} assertionsAndExtensions Each assertionsAndExtensions object is processed further based on the information in the object
12
+ * @param {object} options Options passed to the validator and any other variables passed between the methods
13
+ * @returns {array} Failed assertion objects are returned
14
+ */
15
+
4
16
  function checkRule(context, assertionsAndExtensions, options) {
5
17
  // Context cache
6
18
  const failedAssertions = [];
@@ -12,14 +24,61 @@ function checkRule(context, assertionsAndExtensions, options) {
12
24
  processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options);
13
25
  }
14
26
  else {
15
- const failedSubAssertions = processExtension(assertionAndExtensionObject, context, options);
27
+ const failedSubAssertions = processExtensions(assertionAndExtensionObject, context, options);
16
28
  failedAssertions.push(...failedSubAssertions);
17
29
  }
18
30
  }
19
31
  return failedAssertions;
20
32
  }
21
33
 
22
- function processExtension(assertionAndExtensionObject, context, options) {
34
+ /**
35
+ * @method processAssertions When the assertionAndExtensionObject type is assertion process it
36
+ * @param {object} assertionAndExtensionObject Assertion object with information to test against the selectedXml using testAssertion
37
+ * @param {array} selectedXml array of selected xml based on context
38
+ * @param {array} failedAssertions array to hold failedAssertions
39
+ * @param {object} options Options passed to the validator and any other variables passed between the methods
40
+ * @returns {array} Failed assertion objects are returned
41
+ */
42
+
43
+ function processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options) {
44
+ // const failedAssertions = [];
45
+ let { level, test, id, description } = assertionAndExtensionObject;
46
+ // Extract values from external document and modify test if a document call is made
47
+ const originalTest = test;
48
+ let simplifiedTest = null;
49
+ try {
50
+ test = includeExternalDocument(test, options.resourceDir);
51
+ if (originalTest !== test) {
52
+ simplifiedTest = test;
53
+ }
54
+ if (level === 'error' || _get(options, 'includeWarnings')) {
55
+ const testAssertionResponse = testAssertion(test, selectedXml, options.xpathSelect, options.xmlDoc, options.resourceDir, options.xmlSnippetMaxLength);
56
+ if ((Array.isArray(testAssertionResponse) && testAssertionResponse.length) || _get(testAssertionResponse, 'ignored')) {
57
+ const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
58
+ assertionObject.results = testAssertionResponse;
59
+ failedAssertions.push(assertionObject);
60
+ }
61
+ }
62
+ }
63
+ catch (err) {
64
+ const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
65
+ assertionObject.results = { ignored: true, errorMessage: err.message };
66
+ failedAssertions.push(assertionObject);
67
+ }
68
+ return failedAssertions;
69
+ }
70
+
71
+ /**
72
+ * @method processExtension When the assertionAndExtensionObject type is 'not an assertion' (extension) process it
73
+ * Cannot have this as a separate file with a circular reference to checkRule export in a separate file. That will cause
74
+ * the code to fail. So, these methods (checkRule and processExtension) should be in the same file.
75
+ * @param {object} assertionAndExtensionObject Extension object with a rule is processed with checkRule
76
+ * @param {string} context Used to query the xml using xpath
77
+ * @param {object} options Options passed to the validator and any other variables passed between the methods
78
+ * @returns {array} Failed assertion objects are returned
79
+ */
80
+
81
+ function processExtensions(assertionAndExtensionObject, context, options) {
23
82
  const extensionRule = assertionAndExtensionObject.rule;
24
83
  let failedSubAssertions = [];
25
84
  if (extensionRule) {
@@ -32,4 +91,8 @@ function processExtension(assertionAndExtensionObject, context, options) {
32
91
  return failedSubAssertions;
33
92
  }
34
93
 
35
- module.exports = checkRule;
94
+ module.exports = {
95
+ checkRule,
96
+ processAssertions,
97
+ processExtensions
98
+ };
@@ -1,3 +1,14 @@
1
+ /**
2
+ * @method getAssertionObject A simple wrapper to output assertionObject with the right properties as it is used by
3
+ * multiple mthods
4
+ * @param {string} level 'error' or 'warning'
5
+ * @param {string} id rule id
6
+ * @param {string} originalTest test information
7
+ * @param {string} simplifiedTest if the originalTest has an addition from an external file
8
+ * @param {string} description description of the error or warning
9
+ * @returns {object} returns object with the properties type, assertionId, test, simplifiedTest, description
10
+ */
11
+
1
12
  function getAssertionObject(level, id, originalTest, simplifiedTest, description) {
2
13
  const assertionObject = {
3
14
  type: level,
@@ -1,3 +1,10 @@
1
+ /**
2
+ * @method getErrorsAndWarnings Given the information update errors and warnings array in resultInfo
3
+ * @param {object} assertionObject Provides the assertion and the results of the test
4
+ * @param {object} assertionInfo Information about the assertion
5
+ * @param {object} resultInfo Arrays to store error and warning info
6
+ */
7
+
1
8
  function getErrorsAndWarnings(assertionObject, assertionInfo, resultInfo) {
2
9
  const { type, assertionId, test, simplifiedTest, description, results } = assertionObject;
3
10
  const { patternId, ruleId, context } = assertionInfo;
@@ -1,5 +1,14 @@
1
1
  const _get = require('lodash.get');
2
- const checkRule = require('./checkRule');
2
+ const { checkRule } = require('./checkRule');
3
+
4
+ /**
5
+ * @method getFailedAssertions Get assertions and extension from the rule object and check the rule against the context
6
+ * @param {string} ruleId Rule identifier
7
+ * @param {object} assertionInfo Object to store assertion info
8
+ * @param {object} options Options passed to the validator and any other variables passed between the methods
9
+ * @returns {array} array of failedAssertionObjects
10
+ */
11
+
3
12
  function getFailedAssertions(ruleId, assertionInfo, options) {
4
13
  let failedAssertions;
5
14
  assertionInfo.ruleId = ruleId;
package/lib/getIgnored.js CHANGED
@@ -1,3 +1,11 @@
1
+ /**
2
+ * @method getIgnored If there ws an erroneous rule, it results in the rule being ignored.
3
+ * This method will add the ignored assertion info to the ignored array.
4
+ * @param {object} assertionObject Carries the ignored assertion info
5
+ * @param {object} assertionInfo carries the assertion info from pattern, rule and context
6
+ * @param {object} resultInfo carries the ignored array
7
+ */
8
+
1
9
  function getIgnored(assertionObject, assertionInfo, resultInfo) {
2
10
  const { type, assertionId, test, simplifiedTest, description, errorMessage } = assertionObject;
3
11
  const { patternId, ruleId, context } = assertionInfo;
package/lib/getResults.js CHANGED
@@ -1,5 +1,14 @@
1
1
  const getErrorsAndWarnings = require('./getErrorsAndWarnings');
2
2
  const getIgnored = require('./getIgnored');
3
+
4
+ /**
5
+ * @method getResults Get the results from the assertionObject and store the information in
6
+ * errors, warnings and ignored arrays
7
+ * @param {object} assertionObject Object with the assertion check results
8
+ * @param {object} assertionInfo Object with the assertion information
9
+ * @param {object} resultInfo Object that hosts the errors, warnings and ignored arrays to store the results
10
+ */
11
+
3
12
  function getResults(assertionObject, assertionInfo, resultInfo) {
4
13
  const { results } = assertionObject;
5
14
  if (Array.isArray(results)) {
@@ -5,7 +5,7 @@ const parseSchematron = require('../parseSchematron');
5
5
  /**
6
6
  * @method getSchematronMap A method to get the schematron map from the provided schematron or utilize the data from cache or options object
7
7
  * @param {string} schematron String data of schematron
8
- * @param {object} options options object passed to `validate` or `validateAsync` method
8
+ * @param {object} options Options passed to the validator and any other variables passed between the methods
9
9
  * @returns {object} parsed schemtaron map object
10
10
  */
11
11
 
@@ -1,3 +1,10 @@
1
+ /**
2
+ * @method getSelectXmlChunk Use xpath to get xml chunks from the xml based on the queried context
3
+ * @param {string} context Used to query the xml using xpath
4
+ * @param {object} options Options passed to the validator and any other variables passed between the methods
5
+ * @returns {array} xpath selected xml chunks array
6
+ */
7
+
1
8
  function getSelectXmlChunk(context, options) {
2
9
  // Determine the sections within context, load selected section from cache if possible
3
10
  let selectedXml = options.contextMap[context];
@@ -9,13 +9,8 @@ const getSchematronMap = require('./getSchematronMap');
9
9
  * @param {string} xml string content of an xml file or path to an xml file
10
10
  * @param {string} schematron string content of a schematron file or path to a schematron file
11
11
  * @param {object} options object that takes parameters as seen in the `validate` method
12
- * @returns Sets the following variables with file scope
13
- * xml, schematron, xmlDoc,
14
- * schematronMap, namespaceMap,
15
- * patternRuleMap, ruleAssertionMap,
16
- * resourceDir, xmlSnippetMaxLength,
17
- * xpathSelect, errors, warnings,
18
- * ignored, and resultInfo
12
+ * Sets some method specific variables that will be passed to other methods using the options object
13
+ * options.xmlDoc, options.schematronMap, options.resourceDir, options.xmlSnippetMaxLength, and options.xpathSelect
19
14
  */
20
15
 
21
16
  function initializeProcess(xml, schematron, options) {
@@ -1,4 +1,12 @@
1
1
  const getResults = require('./getResults');
2
+
3
+ /**
4
+ * @method processFailedAssertions Get failed assertion object, get results and store in resultInfo
5
+ * @param {array} failedAssertions Array of failed assertions
6
+ * @param {object} assertionInfo Object with the assertion information
7
+ * @param {object} resultInfo Object that hosts the errors, warnings and ignored arrays to store the results
8
+ */
9
+
2
10
  function processFailedAssertions(failedAssertions, assertionInfo, resultInfo) {
3
11
  for (let index = 0; index < failedAssertions.length; index++) {
4
12
  const assertionObject = failedAssertions[index];
@@ -3,7 +3,7 @@ const processFailedAssertions = require('./processFailedAssertions');
3
3
  /**
4
4
  * @method processRules Entry method to start processing schematron rules. Calls methods `getFailedAssertions` and `processFailedAssertions`
5
5
  * @param {Array} rules Array of ruleIds
6
- * @param {object} resultInfo Capturing the information of the rule to pass it on to reporting object
6
+ * @param {object} resultInfo Object that hosts the errors, warnings and ignored arrays to store the results
7
7
  * @param {object} options Passing on the options object passed to the `validate` or `validateAsync` methods
8
8
  */
9
9
 
@@ -4,7 +4,7 @@ const processFailedAssertions = require('./processFailedAssertions');
4
4
  * @method processRulesAsync Entry method to start processing schematron rules. Calls methods `getFailedAssertions` and `processFailedAssertions`
5
5
  * This method is same as `processRules` except that this is an Async method wrapping a promise, and returning the promise
6
6
  * @param {Array} rules Array of ruleIds
7
- * @param {object} resultInfo Capturing the information of the rule to pass it on to reporting object
7
+ * @param {object} resultInfo Object that hosts the errors, warnings and ignored arrays to store the results
8
8
  * @param {object} options Passing on the options object passed to the `validate` or `validateAsync` methods
9
9
  */
10
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cda-schematron-validator",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Fork of Eric Wadkins' javascript implementation of schematron testing for C-CDA XML documents. This includes bug fixes and some house keeping.",
5
5
  "main": "validator.js",
6
6
  "scripts": {
@@ -21,6 +21,30 @@ describe('Async file list schematron validation should', function() {
21
21
  it('return results', async function() {
22
22
  results = await validator.validateFileListAsync([xml, xml], schematron, options);
23
23
  expect(results.length).to.be.equal(2);
24
+ expect([0, 1]).includes(results[0].fileIndex);
25
+ expect([0, 1]).includes(results[1].fileIndex);
26
+ });
27
+ it('return correct number of errors', function() {
28
+ expect(results[0].errorCount).to.be.equal(1);
29
+ expect(results[1].errorCount).to.be.equal(1);
30
+ });
31
+ it('return correct number of warnings', function() {
32
+ expect(results[0].warningCount).to.be.equal(6);
33
+ expect(results[1].warningCount).to.be.equal(6);
34
+ });
35
+ it('return correct number of ignored', function() {
36
+ expect(results[0].ignoredCount).to.be.equal(0);
37
+ expect(results[1].ignoredCount).to.be.equal(0);
38
+ });
39
+ });
40
+
41
+ describe('Async xml data object schematron validation should', function() {
42
+ let results;
43
+ it('return results', async function() {
44
+ results = await validator.validateFileObjectAsync({file_one: xml, file_two: xml}, schematron, options);
45
+ expect(results.length).to.be.equal(2);
46
+ expect(['file_one', 'file_two']).includes(results[0].fileInfo);
47
+ expect(['file_one', 'file_two']).includes(results[1].fileInfo);
24
48
  });
25
49
  it('return correct number of errors', function() {
26
50
  expect(results[0].errorCount).to.be.equal(1);
package/validateAsync.js CHANGED
@@ -51,12 +51,58 @@ async function validateAsync(xml, schematron, options = {}) {
51
51
  });
52
52
  }
53
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
+
54
68
  async function validateFileListAsync(xmlArray, schematron, options = {}) {
55
69
  const resultInfo = [];
56
70
  return Promise.all(
57
- xmlArray.map(async (xml) => {
58
- resultInfo.push(await validateAsync(xml, schematron, options));
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);
59
104
  console.log(`Number of files processed: ${resultInfo.length}`);
105
+ console.log(`Processed file info: ${xmlFileInfo}`);
60
106
  })
61
107
  ).then(() => {
62
108
  return resultInfo;
@@ -66,5 +112,6 @@ async function validateFileListAsync(xmlArray, schematron, options = {}) {
66
112
  module.exports = {
67
113
  validateAsync,
68
114
  validateFileListAsync,
115
+ validateFileObjectAsync,
69
116
  clearCache
70
117
  };
package/validator.js CHANGED
@@ -1,10 +1,15 @@
1
1
  // jshint node:true
2
2
  // jshint shadow:true
3
3
 
4
+ /**
5
+ * Export all the main methods
6
+ */
7
+
4
8
  module.exports = {
5
9
  validate: require('./validateSync').validateSync,
6
10
  validateAsync: require('./validateAsync').validateAsync,
7
11
  validateFileListAsync: require('./validateAsync').validateFileListAsync,
12
+ validateFileObjectAsync: require('./validateAsync').validateFileObjectAsync,
8
13
  clearCache: require('./validateSync').clearCache,
9
14
  clearCacheAsync: require('./validateAsync').clearCache,
10
15
  parseSchematron: require('./parseSchematron')
@@ -1,33 +0,0 @@
1
- const _get = require('lodash.get');
2
- const includeExternalDocument = require('./includeExternalDocument');
3
- const testAssertion = require('./testAssertion');
4
- const getAssertionObject = require('./getAssertionObject');
5
- function processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options) {
6
- // const failedAssertions = [];
7
- let { level, test, id, description } = assertionAndExtensionObject;
8
- // Extract values from external document and modify test if a document call is made
9
- const originalTest = test;
10
- let simplifiedTest = null;
11
- try {
12
- test = includeExternalDocument(test, options.resourceDir);
13
- if (originalTest !== test) {
14
- simplifiedTest = test;
15
- }
16
- if (level === 'error' || _get(options, 'includeWarnings')) {
17
- const testAssertionResponse = testAssertion(test, selectedXml, options.xpathSelect, options.xmlDoc, options.resourceDir, options.xmlSnippetMaxLength);
18
- if ((Array.isArray(testAssertionResponse) && testAssertionResponse.length) || _get(testAssertionResponse, 'ignored')) {
19
- const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
20
- assertionObject.results = testAssertionResponse;
21
- failedAssertions.push(assertionObject);
22
- }
23
- }
24
- }
25
- catch (err) {
26
- const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
27
- assertionObject.results = { ignored: true, errorMessage: err.message };
28
- failedAssertions.push(assertionObject);
29
- }
30
- return failedAssertions;
31
- }
32
-
33
- module.exports = processAssertions;