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.
- package/.jshintrc +2 -2
- package/README.md +20 -1
- package/lib/checkIfFileOrPath.js +28 -0
- package/lib/checkRule.js +98 -0
- package/lib/getAssertionObject.js +23 -0
- package/lib/getErrorsAndWarnings.js +41 -0
- package/lib/getFailedAssertions.js +25 -0
- package/lib/getIgnored.js +27 -0
- package/lib/getResults.js +22 -0
- package/lib/getReturnObject.js +20 -0
- package/lib/getSchematronMap.js +31 -0
- package/lib/getSelectXmlChunk.js +27 -0
- package/{includeExternalDocument.js → lib/includeExternalDocument.js} +0 -0
- package/lib/initializeProcess.js +40 -0
- package/lib/processFailedAssertions.js +17 -0
- package/lib/processRules.js +20 -0
- package/lib/processRulesAsync.js +30 -0
- package/{testAssertion.js → lib/testAssertion.js} +0 -0
- package/package.json +1 -1
- package/test/async.test.js +1 -1
- package/test/asyncFileList.test.js +61 -0
- package/test/tests.js +1 -1
- package/test/vitalSigns.test.js +1 -96
- package/validateAsync.js +117 -0
- package/validateSync.js +55 -0
- package/validator.js +10 -455
package/.jshintrc
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*jshint :esversion
|
|
1
|
+
/*jshint :esversion 8 */
|
|
2
2
|
{
|
|
3
3
|
// JSHint Default Configuration File (as on JSHint website)
|
|
4
4
|
// See http://jshint.com/docs/ for more details
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"dojo": false, // Dojo Toolkit
|
|
70
70
|
"jquery": true, // jQuery
|
|
71
71
|
"mootools": false, // MooTools
|
|
72
|
-
"node":
|
|
72
|
+
"node": true, // Node.js
|
|
73
73
|
"nonstandard": false, // Widely adopted globals (escape, unescape, etc)
|
|
74
74
|
"prototypejs": false, // Prototype and Scriptaculous
|
|
75
75
|
"rhino": false, // Rhino
|
package/README.md
CHANGED
|
@@ -2,7 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
## Release History
|
|
4
4
|
|
|
5
|
-
|
|
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
|
+
|
|
13
|
+
### 1.1.6
|
|
14
|
+
|
|
15
|
+
- Methods have been moved to js files and relocated to lib directory
|
|
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
|
|
18
|
+
- Test file `test/asyncFileList.test.js` has the example usage
|
|
19
|
+
- Started to make use of Promise.all for async methods
|
|
20
|
+
- Documentation of the methods completed by 40%
|
|
21
|
+
|
|
22
|
+
### 1.1.5
|
|
23
|
+
|
|
24
|
+
- Major methods have been divided into multiple small methods to avoid redundancy across sync and async methods, and reduce maintenance
|
|
6
25
|
|
|
7
26
|
### 1.1.4 (Stable Release)
|
|
8
27
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
/**
|
|
3
|
+
* @method checkIfFileOrPath Checks if a given xml string is xml content or file path to the xml, and extracts the content and
|
|
4
|
+
* assigns it back to the variable. If no data could be found or there is an issue to access the file, an error will be returned.
|
|
5
|
+
* @param {string} givenString xml string or a path to the xml file (the xml can be an xml file or schematron file which is basically an xml)
|
|
6
|
+
* @returns If it is a valid xml, returns it. If it is a file path, extracts the content and returns it. Otherwise throws appropriate error.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
function checkIfFileOrPath(givenString) {
|
|
10
|
+
// If not valid xml, it might be a filepath
|
|
11
|
+
// Adding explicit check to make it clear
|
|
12
|
+
if (!givenString) {
|
|
13
|
+
return new Error('No data found in the xml or schematron');
|
|
14
|
+
}
|
|
15
|
+
if (givenString.trim().indexOf('<') === -1) {
|
|
16
|
+
try {
|
|
17
|
+
givenString = fs.readFileSync(givenString, 'utf-8').toString();
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
// If no valid xml found, inform user, and return immediately
|
|
21
|
+
console.log('No valid xml or schematron could be found');
|
|
22
|
+
return err;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return givenString;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = checkIfFileOrPath;
|
package/lib/checkRule.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const _get = require('lodash.get');
|
|
2
|
+
const getSelectXmlChunk = require('./getSelectXmlChunk');
|
|
3
|
+
const includeExternalDocument = require('./includeExternalDocument');
|
|
4
|
+
const testAssertion = require('./testAssertion');
|
|
5
|
+
const getAssertionObject = require('./getAssertionObject');
|
|
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
|
+
|
|
16
|
+
function checkRule(context, assertionsAndExtensions, options) {
|
|
17
|
+
// Context cache
|
|
18
|
+
const failedAssertions = [];
|
|
19
|
+
// Determine the sections within context, load selected section from cache if possible
|
|
20
|
+
const selectedXml = getSelectXmlChunk(context, options);
|
|
21
|
+
for (let i = 0; i < assertionsAndExtensions.length; i++) {
|
|
22
|
+
const assertionAndExtensionObject = assertionsAndExtensions[i];
|
|
23
|
+
if (assertionAndExtensionObject.type === 'assertion') {
|
|
24
|
+
processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const failedSubAssertions = processExtensions(assertionAndExtensionObject, context, options);
|
|
28
|
+
failedAssertions.push(...failedSubAssertions);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return failedAssertions;
|
|
32
|
+
}
|
|
33
|
+
|
|
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) {
|
|
82
|
+
const extensionRule = assertionAndExtensionObject.rule;
|
|
83
|
+
let failedSubAssertions = [];
|
|
84
|
+
if (extensionRule) {
|
|
85
|
+
const newRuleObject = options.schematronMap.ruleAssertionMap[extensionRule];
|
|
86
|
+
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
87
|
+
if (subAssertionsAndExtensions) {
|
|
88
|
+
failedSubAssertions = checkRule(context, subAssertionsAndExtensions, options);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return failedSubAssertions;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
checkRule,
|
|
96
|
+
processAssertions,
|
|
97
|
+
processExtensions
|
|
98
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
12
|
+
function getAssertionObject(level, id, originalTest, simplifiedTest, description) {
|
|
13
|
+
const assertionObject = {
|
|
14
|
+
type: level,
|
|
15
|
+
assertionId: id,
|
|
16
|
+
test: originalTest,
|
|
17
|
+
simplifiedTest: simplifiedTest,
|
|
18
|
+
description: description
|
|
19
|
+
};
|
|
20
|
+
return assertionObject;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = getAssertionObject;
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
|
|
8
|
+
function getErrorsAndWarnings(assertionObject, assertionInfo, resultInfo) {
|
|
9
|
+
const { type, assertionId, test, simplifiedTest, description, results } = assertionObject;
|
|
10
|
+
const { patternId, ruleId, context } = assertionInfo;
|
|
11
|
+
const { errors, warnings } = resultInfo;
|
|
12
|
+
for (let index = 0; index < results.length; index++) {
|
|
13
|
+
const resultObject = results[index];
|
|
14
|
+
const { result, line, path, xml } = resultObject;
|
|
15
|
+
// If the xpath did not return a result (result=false), then there should be
|
|
16
|
+
// and error or warning
|
|
17
|
+
if (!result) {
|
|
18
|
+
const obj = {
|
|
19
|
+
type,
|
|
20
|
+
test,
|
|
21
|
+
simplifiedTest,
|
|
22
|
+
description,
|
|
23
|
+
line,
|
|
24
|
+
path,
|
|
25
|
+
patternId,
|
|
26
|
+
ruleId,
|
|
27
|
+
assertionId,
|
|
28
|
+
context,
|
|
29
|
+
xml
|
|
30
|
+
};
|
|
31
|
+
if (type === 'error') {
|
|
32
|
+
errors.push(obj);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
warnings.push(obj);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = getErrorsAndWarnings;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const _get = require('lodash.get');
|
|
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
|
+
|
|
12
|
+
function getFailedAssertions(ruleId, assertionInfo, options) {
|
|
13
|
+
let failedAssertions;
|
|
14
|
+
assertionInfo.ruleId = ruleId;
|
|
15
|
+
const ruleObject = options.schematronMap.ruleAssertionMap[ruleId];
|
|
16
|
+
if (!_get(ruleObject, 'abstract')) {
|
|
17
|
+
const context = _get(ruleObject, 'context');
|
|
18
|
+
assertionInfo.context = context;
|
|
19
|
+
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
20
|
+
failedAssertions = checkRule(context, assertionsAndExtensions, options);
|
|
21
|
+
}
|
|
22
|
+
return failedAssertions;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = getFailedAssertions;
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
|
|
9
|
+
function getIgnored(assertionObject, assertionInfo, resultInfo) {
|
|
10
|
+
const { type, assertionId, test, simplifiedTest, description, errorMessage } = assertionObject;
|
|
11
|
+
const { patternId, ruleId, context } = assertionInfo;
|
|
12
|
+
const { ignored } = resultInfo;
|
|
13
|
+
const obj = {
|
|
14
|
+
errorMessage,
|
|
15
|
+
type,
|
|
16
|
+
test,
|
|
17
|
+
simplifiedTest,
|
|
18
|
+
description,
|
|
19
|
+
patternId,
|
|
20
|
+
ruleId,
|
|
21
|
+
assertionId,
|
|
22
|
+
context
|
|
23
|
+
};
|
|
24
|
+
ignored.push(obj);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = getIgnored;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const getErrorsAndWarnings = require('./getErrorsAndWarnings');
|
|
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
|
+
|
|
12
|
+
function getResults(assertionObject, assertionInfo, resultInfo) {
|
|
13
|
+
const { results } = assertionObject;
|
|
14
|
+
if (Array.isArray(results)) {
|
|
15
|
+
getErrorsAndWarnings(assertionObject, assertionInfo, resultInfo);
|
|
16
|
+
}
|
|
17
|
+
else if (results.ignored) {
|
|
18
|
+
getIgnored(assertionObject, assertionInfo, resultInfo);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = getResults;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @method getReturnObject Simple method to avoid redundancy of result object representation
|
|
3
|
+
* @param {object} resultInfo carrying the arrays errors, warnings and ignored
|
|
4
|
+
* @returns result object with information about overall errors, warnings and ignored
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function getReturnObject(resultInfo) {
|
|
8
|
+
const { errors, warnings, ignored } = resultInfo;
|
|
9
|
+
const result = {
|
|
10
|
+
errorCount: errors.length,
|
|
11
|
+
warningCount: warnings.length,
|
|
12
|
+
ignoredCount: ignored.length,
|
|
13
|
+
errors: errors,
|
|
14
|
+
warnings: warnings,
|
|
15
|
+
ignored: ignored
|
|
16
|
+
};
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = getReturnObject;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
const _get = require('lodash.get');
|
|
4
|
+
const parseSchematron = require('../parseSchematron');
|
|
5
|
+
/**
|
|
6
|
+
* @method getSchematronMap A method to get the schematron map from the provided schematron or utilize the data from cache or options object
|
|
7
|
+
* @param {string} schematron String data of schematron
|
|
8
|
+
* @param {object} options Options passed to the validator and any other variables passed between the methods
|
|
9
|
+
* @returns {object} parsed schemtaron map object
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
function getSchematronMap(schematron, options = {}) {
|
|
13
|
+
if (_get(options, 'parsedSchematronMap')) {
|
|
14
|
+
return _get(options, 'parsedSchematronMap');
|
|
15
|
+
}
|
|
16
|
+
const hash = crypto
|
|
17
|
+
.createHash('md5')
|
|
18
|
+
.update(schematron)
|
|
19
|
+
.digest('hex');
|
|
20
|
+
if (options.parsedMap[hash]) {
|
|
21
|
+
return options.parsedMap[hash];
|
|
22
|
+
}
|
|
23
|
+
// If not in cache
|
|
24
|
+
// Load schematron doc
|
|
25
|
+
const schematronDoc = new Dom().parseFromString(schematron);
|
|
26
|
+
// Parse schematron
|
|
27
|
+
options.parsedMap[hash] = parseSchematron(schematronDoc, options);
|
|
28
|
+
return options.parsedMap[hash];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = getSchematronMap;
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
|
|
8
|
+
function getSelectXmlChunk(context, options) {
|
|
9
|
+
// Determine the sections within context, load selected section from cache if possible
|
|
10
|
+
let selectedXml = options.contextMap[context];
|
|
11
|
+
let contextModified = context;
|
|
12
|
+
if (!selectedXml) {
|
|
13
|
+
if (context) {
|
|
14
|
+
if (context.indexOf('/')) {
|
|
15
|
+
contextModified = '//' + context;
|
|
16
|
+
}
|
|
17
|
+
selectedXml = options.xpathSelect(contextModified, options.xmlDoc);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
selectedXml = [options.xmlDoc];
|
|
21
|
+
}
|
|
22
|
+
options.contextMap[context] = selectedXml;
|
|
23
|
+
}
|
|
24
|
+
return selectedXml;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = getSelectXmlChunk;
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const xpath = require('xpath');
|
|
2
|
+
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
3
|
+
const _get = require('lodash.get');
|
|
4
|
+
const checkIfFileOrPath = require('./checkIfFileOrPath');
|
|
5
|
+
const getSchematronMap = require('./getSchematronMap');
|
|
6
|
+
/**
|
|
7
|
+
* @method initializeProcess Checks the validity of the supplied xml and schematron, and sets some global variables used across functions in this script.
|
|
8
|
+
* This calls the `getSchematronMap` and assigns the value to schematronMap variable.
|
|
9
|
+
* @param {string} xml string content of an xml file or path to an xml file
|
|
10
|
+
* @param {string} schematron string content of a schematron file or path to a schematron file
|
|
11
|
+
* @param {object} options object that takes parameters as seen in the `validate` method
|
|
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
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
function initializeProcess(xml, schematron, options) {
|
|
17
|
+
try {
|
|
18
|
+
xml = checkIfFileOrPath(xml);
|
|
19
|
+
schematron = checkIfFileOrPath(schematron);
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
return err;
|
|
23
|
+
}
|
|
24
|
+
// Load xml doc
|
|
25
|
+
options.xmlDoc = new Dom().parseFromString(xml);
|
|
26
|
+
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
27
|
+
options.schematronMap = getSchematronMap(schematron, options);
|
|
28
|
+
// Extract data from parsed schematron object
|
|
29
|
+
if (!_get(options, 'resourceDir')) {
|
|
30
|
+
options.resourceDir = '../';
|
|
31
|
+
}
|
|
32
|
+
if (!_get(options, 'xmlSnippetMaxLength')) {
|
|
33
|
+
options.xmlSnippetMaxLength = 200;
|
|
34
|
+
}
|
|
35
|
+
// Create selector object, initialized with namespaces
|
|
36
|
+
// Avoid using 'select' as a variable name as it is overused
|
|
37
|
+
options.xpathSelect = xpath.useNamespaces(options.schematronMap.namespaceMap);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = initializeProcess;
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
|
|
10
|
+
function processFailedAssertions(failedAssertions, assertionInfo, resultInfo) {
|
|
11
|
+
for (let index = 0; index < failedAssertions.length; index++) {
|
|
12
|
+
const assertionObject = failedAssertions[index];
|
|
13
|
+
getResults(assertionObject, assertionInfo, resultInfo);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = processFailedAssertions;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const getFailedAssertions = require('./getFailedAssertions');
|
|
2
|
+
const processFailedAssertions = require('./processFailedAssertions');
|
|
3
|
+
/**
|
|
4
|
+
* @method processRules Entry method to start processing schematron rules. Calls methods `getFailedAssertions` and `processFailedAssertions`
|
|
5
|
+
* @param {Array} rules Array of ruleIds
|
|
6
|
+
* @param {object} resultInfo Object that hosts the errors, warnings and ignored arrays to store the results
|
|
7
|
+
* @param {object} options Passing on the options object passed to the `validate` or `validateAsync` methods
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
function processRules(rules, assertionInfo, resultInfo, options) {
|
|
11
|
+
for (let index = 0; index < rules.length; index++) {
|
|
12
|
+
const ruleId = rules[index];
|
|
13
|
+
const failedAssertions = getFailedAssertions(ruleId, assertionInfo, options);
|
|
14
|
+
if (failedAssertions) {
|
|
15
|
+
processFailedAssertions(failedAssertions, assertionInfo, resultInfo);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = processRules;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const getFailedAssertions = require('./getFailedAssertions');
|
|
2
|
+
const processFailedAssertions = require('./processFailedAssertions');
|
|
3
|
+
/**
|
|
4
|
+
* @method processRulesAsync Entry method to start processing schematron rules. Calls methods `getFailedAssertions` and `processFailedAssertions`
|
|
5
|
+
* This method is same as `processRules` except that this is an Async method wrapping a promise, and returning the promise
|
|
6
|
+
* @param {Array} rules Array of ruleIds
|
|
7
|
+
* @param {object} resultInfo Object that hosts the errors, warnings and ignored arrays to store the results
|
|
8
|
+
* @param {object} options Passing on the options object passed to the `validate` or `validateAsync` methods
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
async function processRulesAsync(rules, assertionInfo, resultInfo, options) {
|
|
12
|
+
const rulesProcessed = new Promise((resolve, reject) => {
|
|
13
|
+
try {
|
|
14
|
+
for (let index = 0; index < rules.length; index++) {
|
|
15
|
+
const ruleId = rules[index];
|
|
16
|
+
const failedAssertions = getFailedAssertions(ruleId, assertionInfo, options);
|
|
17
|
+
resolve (failedAssertions);
|
|
18
|
+
if (failedAssertions) {
|
|
19
|
+
processFailedAssertions(failedAssertions, assertionInfo, resultInfo);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (promiseError) {
|
|
24
|
+
reject (new Error (`Promise Error: ${promiseError}`));
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return rulesProcessed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = processRulesAsync;
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cda-schematron-validator",
|
|
3
|
-
"version": "1.1.
|
|
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": {
|
package/test/async.test.js
CHANGED
|
@@ -11,7 +11,7 @@ const schematronPath = './test/test.sch';
|
|
|
11
11
|
const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
|
|
12
12
|
const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
13
13
|
|
|
14
|
-
describe('
|
|
14
|
+
describe('Async validator should', function() {
|
|
15
15
|
let results;
|
|
16
16
|
it('return results', async function() {
|
|
17
17
|
results = await validator.validateAsync(xml, schematron, { includeWarnings: true });
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const expect = require('chai').expect;
|
|
3
|
+
|
|
4
|
+
const validator = require('../validator');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
// Which xml file to test
|
|
8
|
+
const xmlPath = path.resolve(__dirname, './vitalSigns.xml');
|
|
9
|
+
// Which schematron to test against
|
|
10
|
+
const schematronPath = path.resolve(__dirname, './CDAR2_IG_CCDA_CLINNOTES_R1_DSTU2.1_2015AUG_Vol2_2019JUNwith_errata.sch');
|
|
11
|
+
|
|
12
|
+
const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
|
|
13
|
+
const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
14
|
+
const options = {
|
|
15
|
+
resourceDir: path.resolve(__dirname, './'),
|
|
16
|
+
includeWarnings: true
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe('Async file list schematron validation should', function() {
|
|
20
|
+
let results;
|
|
21
|
+
it('return results', async function() {
|
|
22
|
+
results = await validator.validateFileListAsync([xml, xml], schematron, options);
|
|
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);
|
|
48
|
+
});
|
|
49
|
+
it('return correct number of errors', function() {
|
|
50
|
+
expect(results[0].errorCount).to.be.equal(1);
|
|
51
|
+
expect(results[1].errorCount).to.be.equal(1);
|
|
52
|
+
});
|
|
53
|
+
it('return correct number of warnings', function() {
|
|
54
|
+
expect(results[0].warningCount).to.be.equal(6);
|
|
55
|
+
expect(results[1].warningCount).to.be.equal(6);
|
|
56
|
+
});
|
|
57
|
+
it('return correct number of ignored', function() {
|
|
58
|
+
expect(results[0].ignoredCount).to.be.equal(0);
|
|
59
|
+
expect(results[1].ignoredCount).to.be.equal(0);
|
|
60
|
+
});
|
|
61
|
+
});
|
package/test/tests.js
CHANGED
|
@@ -10,7 +10,7 @@ const schematronPath = path.resolve(__dirname, './test.sch');
|
|
|
10
10
|
const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
|
|
11
11
|
const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
12
12
|
|
|
13
|
-
describe('
|
|
13
|
+
describe('Classic validator should', function() {
|
|
14
14
|
let results;
|
|
15
15
|
it('return results', function(done) {
|
|
16
16
|
results = validator.validate(xml, schematron, { includeWarnings: true });
|