cda-schematron-validator 1.1.5 → 1.1.6
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 +8 -1
- package/lib/checkIfFileOrPath.js +28 -0
- package/lib/checkRule.js +35 -0
- package/lib/getAssertionObject.js +12 -0
- package/lib/getErrorsAndWarnings.js +34 -0
- package/lib/getFailedAssertions.js +16 -0
- package/lib/getIgnored.js +19 -0
- package/lib/getResults.js +13 -0
- package/lib/getReturnObject.js +20 -0
- package/lib/getSchematronMap.js +31 -0
- package/lib/getSelectXmlChunk.js +20 -0
- package/{includeExternalDocument.js → lib/includeExternalDocument.js} +0 -0
- package/lib/initializeProcess.js +45 -0
- package/lib/processAssertions.js +33 -0
- package/lib/processFailedAssertions.js +9 -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 +37 -0
- package/test/tests.js +1 -1
- package/test/vitalSigns.test.js +1 -96
- package/validateAsync.js +70 -0
- package/validateSync.js +55 -0
- package/validator.js +5 -329
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,14 @@
|
|
|
2
2
|
|
|
3
3
|
## Release History
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### 1.1.6
|
|
6
|
+
|
|
7
|
+
- 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
|
|
10
|
+
- Test file `test/asyncFileList.test.js` has the example usage
|
|
11
|
+
- Started to make use of Promise.all for async methods
|
|
12
|
+
- Documentation of the methods completed by 40%
|
|
6
13
|
|
|
7
14
|
### 1.1.5
|
|
8
15
|
|
|
@@ -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,35 @@
|
|
|
1
|
+
const getSelectXmlChunk = require('./getSelectXmlChunk');
|
|
2
|
+
const processAssertions = require('./processAssertions');
|
|
3
|
+
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
4
|
+
function checkRule(context, assertionsAndExtensions, options) {
|
|
5
|
+
// Context cache
|
|
6
|
+
const failedAssertions = [];
|
|
7
|
+
// Determine the sections within context, load selected section from cache if possible
|
|
8
|
+
const selectedXml = getSelectXmlChunk(context, options);
|
|
9
|
+
for (let i = 0; i < assertionsAndExtensions.length; i++) {
|
|
10
|
+
const assertionAndExtensionObject = assertionsAndExtensions[i];
|
|
11
|
+
if (assertionAndExtensionObject.type === 'assertion') {
|
|
12
|
+
processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const failedSubAssertions = processExtension(assertionAndExtensionObject, context, options);
|
|
16
|
+
failedAssertions.push(...failedSubAssertions);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return failedAssertions;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function processExtension(assertionAndExtensionObject, context, options) {
|
|
23
|
+
const extensionRule = assertionAndExtensionObject.rule;
|
|
24
|
+
let failedSubAssertions = [];
|
|
25
|
+
if (extensionRule) {
|
|
26
|
+
const newRuleObject = options.schematronMap.ruleAssertionMap[extensionRule];
|
|
27
|
+
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
28
|
+
if (subAssertionsAndExtensions) {
|
|
29
|
+
failedSubAssertions = checkRule(context, subAssertionsAndExtensions, options);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return failedSubAssertions;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = checkRule;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
function getAssertionObject(level, id, originalTest, simplifiedTest, description) {
|
|
2
|
+
const assertionObject = {
|
|
3
|
+
type: level,
|
|
4
|
+
assertionId: id,
|
|
5
|
+
test: originalTest,
|
|
6
|
+
simplifiedTest: simplifiedTest,
|
|
7
|
+
description: description
|
|
8
|
+
};
|
|
9
|
+
return assertionObject;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = getAssertionObject;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
function getErrorsAndWarnings(assertionObject, assertionInfo, resultInfo) {
|
|
2
|
+
const { type, assertionId, test, simplifiedTest, description, results } = assertionObject;
|
|
3
|
+
const { patternId, ruleId, context } = assertionInfo;
|
|
4
|
+
const { errors, warnings } = resultInfo;
|
|
5
|
+
for (let index = 0; index < results.length; index++) {
|
|
6
|
+
const resultObject = results[index];
|
|
7
|
+
const { result, line, path, xml } = resultObject;
|
|
8
|
+
// If the xpath did not return a result (result=false), then there should be
|
|
9
|
+
// and error or warning
|
|
10
|
+
if (!result) {
|
|
11
|
+
const obj = {
|
|
12
|
+
type,
|
|
13
|
+
test,
|
|
14
|
+
simplifiedTest,
|
|
15
|
+
description,
|
|
16
|
+
line,
|
|
17
|
+
path,
|
|
18
|
+
patternId,
|
|
19
|
+
ruleId,
|
|
20
|
+
assertionId,
|
|
21
|
+
context,
|
|
22
|
+
xml
|
|
23
|
+
};
|
|
24
|
+
if (type === 'error') {
|
|
25
|
+
errors.push(obj);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
warnings.push(obj);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = getErrorsAndWarnings;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const _get = require('lodash.get');
|
|
2
|
+
const checkRule = require('./checkRule');
|
|
3
|
+
function getFailedAssertions(ruleId, assertionInfo, options) {
|
|
4
|
+
let failedAssertions;
|
|
5
|
+
assertionInfo.ruleId = ruleId;
|
|
6
|
+
const ruleObject = options.schematronMap.ruleAssertionMap[ruleId];
|
|
7
|
+
if (!_get(ruleObject, 'abstract')) {
|
|
8
|
+
const context = _get(ruleObject, 'context');
|
|
9
|
+
assertionInfo.context = context;
|
|
10
|
+
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
11
|
+
failedAssertions = checkRule(context, assertionsAndExtensions, options);
|
|
12
|
+
}
|
|
13
|
+
return failedAssertions;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = getFailedAssertions;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function getIgnored(assertionObject, assertionInfo, resultInfo) {
|
|
2
|
+
const { type, assertionId, test, simplifiedTest, description, errorMessage } = assertionObject;
|
|
3
|
+
const { patternId, ruleId, context } = assertionInfo;
|
|
4
|
+
const { ignored } = resultInfo;
|
|
5
|
+
const obj = {
|
|
6
|
+
errorMessage,
|
|
7
|
+
type,
|
|
8
|
+
test,
|
|
9
|
+
simplifiedTest,
|
|
10
|
+
description,
|
|
11
|
+
patternId,
|
|
12
|
+
ruleId,
|
|
13
|
+
assertionId,
|
|
14
|
+
context
|
|
15
|
+
};
|
|
16
|
+
ignored.push(obj);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = getIgnored;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const getErrorsAndWarnings = require('./getErrorsAndWarnings');
|
|
2
|
+
const getIgnored = require('./getIgnored');
|
|
3
|
+
function getResults(assertionObject, assertionInfo, resultInfo) {
|
|
4
|
+
const { results } = assertionObject;
|
|
5
|
+
if (Array.isArray(results)) {
|
|
6
|
+
getErrorsAndWarnings(assertionObject, assertionInfo, resultInfo);
|
|
7
|
+
}
|
|
8
|
+
else if (results.ignored) {
|
|
9
|
+
getIgnored(assertionObject, assertionInfo, resultInfo);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
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 object passed to `validate` or `validateAsync` method
|
|
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,20 @@
|
|
|
1
|
+
function getSelectXmlChunk(context, options) {
|
|
2
|
+
// Determine the sections within context, load selected section from cache if possible
|
|
3
|
+
let selectedXml = options.contextMap[context];
|
|
4
|
+
let contextModified = context;
|
|
5
|
+
if (!selectedXml) {
|
|
6
|
+
if (context) {
|
|
7
|
+
if (context.indexOf('/')) {
|
|
8
|
+
contextModified = '//' + context;
|
|
9
|
+
}
|
|
10
|
+
selectedXml = options.xpathSelect(contextModified, options.xmlDoc);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
selectedXml = [options.xmlDoc];
|
|
14
|
+
}
|
|
15
|
+
options.contextMap[context] = selectedXml;
|
|
16
|
+
}
|
|
17
|
+
return selectedXml;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = getSelectXmlChunk;
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
* @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
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
function initializeProcess(xml, schematron, options) {
|
|
22
|
+
try {
|
|
23
|
+
xml = checkIfFileOrPath(xml);
|
|
24
|
+
schematron = checkIfFileOrPath(schematron);
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
return err;
|
|
28
|
+
}
|
|
29
|
+
// Load xml doc
|
|
30
|
+
options.xmlDoc = new Dom().parseFromString(xml);
|
|
31
|
+
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
32
|
+
options.schematronMap = getSchematronMap(schematron, options);
|
|
33
|
+
// Extract data from parsed schematron object
|
|
34
|
+
if (!_get(options, 'resourceDir')) {
|
|
35
|
+
options.resourceDir = '../';
|
|
36
|
+
}
|
|
37
|
+
if (!_get(options, 'xmlSnippetMaxLength')) {
|
|
38
|
+
options.xmlSnippetMaxLength = 200;
|
|
39
|
+
}
|
|
40
|
+
// Create selector object, initialized with namespaces
|
|
41
|
+
// Avoid using 'select' as a variable name as it is overused
|
|
42
|
+
options.xpathSelect = xpath.useNamespaces(options.schematronMap.namespaceMap);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = initializeProcess;
|
|
@@ -0,0 +1,33 @@
|
|
|
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;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const getResults = require('./getResults');
|
|
2
|
+
function processFailedAssertions(failedAssertions, assertionInfo, resultInfo) {
|
|
3
|
+
for (let index = 0; index < failedAssertions.length; index++) {
|
|
4
|
+
const assertionObject = failedAssertions[index];
|
|
5
|
+
getResults(assertionObject, assertionInfo, resultInfo);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
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 Capturing the information of the rule to pass it on to reporting object
|
|
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 Capturing the information of the rule to pass it on to reporting object
|
|
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.6",
|
|
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,37 @@
|
|
|
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
|
+
});
|
|
25
|
+
it('return correct number of errors', function() {
|
|
26
|
+
expect(results[0].errorCount).to.be.equal(1);
|
|
27
|
+
expect(results[1].errorCount).to.be.equal(1);
|
|
28
|
+
});
|
|
29
|
+
it('return correct number of warnings', function() {
|
|
30
|
+
expect(results[0].warningCount).to.be.equal(6);
|
|
31
|
+
expect(results[1].warningCount).to.be.equal(6);
|
|
32
|
+
});
|
|
33
|
+
it('return correct number of ignored', function() {
|
|
34
|
+
expect(results[0].ignoredCount).to.be.equal(0);
|
|
35
|
+
expect(results[1].ignoredCount).to.be.equal(0);
|
|
36
|
+
});
|
|
37
|
+
});
|
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 });
|
package/test/vitalSigns.test.js
CHANGED
|
@@ -16,39 +16,12 @@ const options = {
|
|
|
16
16
|
includeWarnings: true
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
describe('
|
|
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
|
});
|
package/validateAsync.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
async function validateFileListAsync(xmlArray, schematron, options = {}) {
|
|
55
|
+
const resultInfo = [];
|
|
56
|
+
return Promise.all(
|
|
57
|
+
xmlArray.map(async (xml) => {
|
|
58
|
+
resultInfo.push(await validateAsync(xml, schematron, options));
|
|
59
|
+
console.log(`Number of files processed: ${resultInfo.length}`);
|
|
60
|
+
})
|
|
61
|
+
).then(() => {
|
|
62
|
+
return resultInfo;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = {
|
|
67
|
+
validateAsync,
|
|
68
|
+
validateFileListAsync,
|
|
69
|
+
clearCache
|
|
70
|
+
};
|
package/validateSync.js
ADDED
|
@@ -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
|
+
};
|
package/validator.js
CHANGED
|
@@ -2,334 +2,10 @@
|
|
|
2
2
|
// jshint shadow:true
|
|
3
3
|
|
|
4
4
|
module.exports = {
|
|
5
|
-
validate:
|
|
6
|
-
validateAsync: validateAsync,
|
|
7
|
-
|
|
5
|
+
validate: require('./validateSync').validateSync,
|
|
6
|
+
validateAsync: require('./validateAsync').validateAsync,
|
|
7
|
+
validateFileListAsync: require('./validateAsync').validateFileListAsync,
|
|
8
|
+
clearCache: require('./validateSync').clearCache,
|
|
9
|
+
clearCacheAsync: require('./validateAsync').clearCache,
|
|
8
10
|
parseSchematron: require('./parseSchematron')
|
|
9
11
|
};
|
|
10
|
-
|
|
11
|
-
const fs = require('fs');
|
|
12
|
-
const xpath = require('xpath');
|
|
13
|
-
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
14
|
-
// crypto is now built-in node
|
|
15
|
-
const crypto = require('crypto');
|
|
16
|
-
const _get = require('lodash.get');
|
|
17
|
-
const parseSchematron = require('./parseSchematron');
|
|
18
|
-
const testAssertion = require('./testAssertion');
|
|
19
|
-
const includeExternalDocument = require('./includeExternalDocument');
|
|
20
|
-
|
|
21
|
-
// Parsed object cache
|
|
22
|
-
let parsedMap = Object.create({});
|
|
23
|
-
let contextMap = Object.create({});
|
|
24
|
-
|
|
25
|
-
function clearCache() {
|
|
26
|
-
parsedMap = Object.create({});
|
|
27
|
-
contextMap = Object.create({});
|
|
28
|
-
}
|
|
29
|
-
function checkIfFileOrPath(givenString) {
|
|
30
|
-
// If not valid xml, it might be a filepath
|
|
31
|
-
// Adding explicit check to make it clear
|
|
32
|
-
if (!givenString) {
|
|
33
|
-
return new Error('No data found in the xml or schematron');
|
|
34
|
-
}
|
|
35
|
-
if (givenString.trim().indexOf('<') === -1) {
|
|
36
|
-
try {
|
|
37
|
-
givenString = fs.readFileSync(givenString, 'utf-8').toString();
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
// If no valid xml found, inform user, and return immediately
|
|
41
|
-
console.log('No valid xml or schematron could be found');
|
|
42
|
-
return err;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return givenString;
|
|
46
|
-
}
|
|
47
|
-
function getSchematronMap(schematron, options = {}) {
|
|
48
|
-
if (_get(options, 'parsedSchematronMap')) {
|
|
49
|
-
return _get(options, 'parsedSchematronMap');
|
|
50
|
-
}
|
|
51
|
-
const hash = crypto
|
|
52
|
-
.createHash('md5')
|
|
53
|
-
.update(schematron)
|
|
54
|
-
.digest('hex');
|
|
55
|
-
if (parsedMap[hash]) {
|
|
56
|
-
return parsedMap[hash];
|
|
57
|
-
}
|
|
58
|
-
// If not in cache
|
|
59
|
-
// Load schematron doc
|
|
60
|
-
const schematronDoc = new Dom().parseFromString(schematron);
|
|
61
|
-
// Parse schematron
|
|
62
|
-
parsedMap[hash] = parseSchematron(schematronDoc, options);
|
|
63
|
-
return parsedMap[hash];
|
|
64
|
-
}
|
|
65
|
-
function getErrorsAndWarnings(assertionObject, assertionInfo) {
|
|
66
|
-
const { type, assertionId, test, simplifiedTest, description, results } = assertionObject;
|
|
67
|
-
const { patternId, ruleId, context, errors, warnings } = assertionInfo;
|
|
68
|
-
for (let index = 0; index < results.length; index++) {
|
|
69
|
-
const resultObject = results[index];
|
|
70
|
-
const { result, line, path, xml } = resultObject;
|
|
71
|
-
// If the xpath did not return a result (result=false), then there should be
|
|
72
|
-
// and error or warning
|
|
73
|
-
if (!result) {
|
|
74
|
-
const obj = {
|
|
75
|
-
type,
|
|
76
|
-
test,
|
|
77
|
-
simplifiedTest,
|
|
78
|
-
description,
|
|
79
|
-
line,
|
|
80
|
-
path,
|
|
81
|
-
patternId,
|
|
82
|
-
ruleId,
|
|
83
|
-
assertionId,
|
|
84
|
-
context,
|
|
85
|
-
xml
|
|
86
|
-
};
|
|
87
|
-
if (type === 'error') {
|
|
88
|
-
errors.push(obj);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
warnings.push(obj);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
function getIgnored(assertionObject, assertionInfo) {
|
|
97
|
-
const { type, assertionId, test, simplifiedTest, description, errorMessage } = assertionObject;
|
|
98
|
-
const { patternId, ruleId, context, ignored } = assertionInfo;
|
|
99
|
-
const obj = {
|
|
100
|
-
errorMessage,
|
|
101
|
-
type,
|
|
102
|
-
test,
|
|
103
|
-
simplifiedTest,
|
|
104
|
-
description,
|
|
105
|
-
patternId,
|
|
106
|
-
ruleId,
|
|
107
|
-
assertionId,
|
|
108
|
-
context
|
|
109
|
-
};
|
|
110
|
-
ignored.push(obj);
|
|
111
|
-
}
|
|
112
|
-
function getResults(assertionObject, assertionInfo) {
|
|
113
|
-
const { results } = assertionObject;
|
|
114
|
-
if (Array.isArray(results)) {
|
|
115
|
-
getErrorsAndWarnings(assertionObject, assertionInfo);
|
|
116
|
-
}
|
|
117
|
-
else if (results.ignored) {
|
|
118
|
-
getIgnored(assertionObject, assertionInfo);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
function processFailedAssertions(failedAssertions, assertionInfo) {
|
|
122
|
-
for (let index = 0; index < failedAssertions.length; index++) {
|
|
123
|
-
const assertionObject = failedAssertions[index];
|
|
124
|
-
getResults(assertionObject, assertionInfo);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function getFailedAssertions(ruleId, options) {
|
|
128
|
-
let failedAssertions;
|
|
129
|
-
assertionInfo.ruleId = ruleId;
|
|
130
|
-
const ruleObject = ruleAssertionMap[ruleId];
|
|
131
|
-
if (!_get(ruleObject, 'abstract')) {
|
|
132
|
-
const context = _get(ruleObject, 'context');
|
|
133
|
-
assertionInfo.context = context;
|
|
134
|
-
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
135
|
-
failedAssertions = checkRule(context, assertionsAndExtensions, options);
|
|
136
|
-
}
|
|
137
|
-
return failedAssertions;
|
|
138
|
-
}
|
|
139
|
-
function processRules(rules, assertionInfo, options) {
|
|
140
|
-
for (let index = 0; index < rules.length; index++) {
|
|
141
|
-
const ruleId = rules[index];
|
|
142
|
-
const failedAssertions = getFailedAssertions(ruleId, options);
|
|
143
|
-
if (failedAssertions) {
|
|
144
|
-
processFailedAssertions(failedAssertions, assertionInfo);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
async function processRulesAsync(rules, assertionInfo, options) {
|
|
149
|
-
const rulesProcessed = new Promise((resolve, reject) => {
|
|
150
|
-
try {
|
|
151
|
-
for (let index = 0; index < rules.length; index++) {
|
|
152
|
-
const ruleId = rules[index];
|
|
153
|
-
const failedAssertions = getFailedAssertions(ruleId, options);
|
|
154
|
-
resolve (failedAssertions);
|
|
155
|
-
if (failedAssertions) {
|
|
156
|
-
processFailedAssertions(failedAssertions, assertionInfo);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
catch (promiseError) {
|
|
161
|
-
reject (new Error (`Promise Error: ${promiseError}`));
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
return rulesProcessed;
|
|
165
|
-
}
|
|
166
|
-
function getReturnObject(assertionInfo) {
|
|
167
|
-
const { errors, warnings, ignored } = assertionInfo;
|
|
168
|
-
const result = {
|
|
169
|
-
errorCount: errors.length,
|
|
170
|
-
warningCount: warnings.length,
|
|
171
|
-
ignoredCount: ignored.length,
|
|
172
|
-
errors: errors,
|
|
173
|
-
warnings: warnings,
|
|
174
|
-
ignored: ignored
|
|
175
|
-
};
|
|
176
|
-
return result;
|
|
177
|
-
}
|
|
178
|
-
let namespaceMap;
|
|
179
|
-
let patternRuleMap;
|
|
180
|
-
let ruleAssertionMap;
|
|
181
|
-
let xpathSelect;
|
|
182
|
-
let resourceDir;
|
|
183
|
-
let xmlSnippetMaxLength;
|
|
184
|
-
let xmlDoc;
|
|
185
|
-
let schematronMap;
|
|
186
|
-
let errors;
|
|
187
|
-
let warnings;
|
|
188
|
-
let ignored;
|
|
189
|
-
let assertionInfo;
|
|
190
|
-
function initializeProcess(xml, schematron, options) {
|
|
191
|
-
try {
|
|
192
|
-
xml = checkIfFileOrPath(xml);
|
|
193
|
-
schematron = checkIfFileOrPath(schematron);
|
|
194
|
-
}
|
|
195
|
-
catch (err) {
|
|
196
|
-
return err;
|
|
197
|
-
}
|
|
198
|
-
// Load xml doc
|
|
199
|
-
xmlDoc = new Dom().parseFromString(xml);
|
|
200
|
-
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
201
|
-
schematronMap = getSchematronMap(schematron, options);
|
|
202
|
-
// Extract data from parsed schematron object
|
|
203
|
-
namespaceMap = schematronMap.namespaceMap;
|
|
204
|
-
patternRuleMap = schematronMap.patternRuleMap;
|
|
205
|
-
ruleAssertionMap = schematronMap.ruleAssertionMap;
|
|
206
|
-
resourceDir = './';
|
|
207
|
-
xmlSnippetMaxLength = 200;
|
|
208
|
-
if (_get(options, 'resourceDir')) {
|
|
209
|
-
resourceDir = _get(options, 'resourceDir');
|
|
210
|
-
}
|
|
211
|
-
if (_get(options, 'xmlSnippetMaxLength')) {
|
|
212
|
-
xmlSnippetMaxLength = _get(options, 'xmlSnippetMaxLength');
|
|
213
|
-
}
|
|
214
|
-
// Create selector object, initialized with namespaces
|
|
215
|
-
// Avoid using 'select' as a variable name as it is overused
|
|
216
|
-
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
217
|
-
errors = [];
|
|
218
|
-
warnings = [];
|
|
219
|
-
ignored = [];
|
|
220
|
-
assertionInfo = {
|
|
221
|
-
errors,
|
|
222
|
-
warnings,
|
|
223
|
-
ignored
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
function validate(xml, schematron, options = {}) {
|
|
227
|
-
initializeProcess(xml, schematron, options);
|
|
228
|
-
for (const patternId in patternRuleMap) {
|
|
229
|
-
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
230
|
-
continue;
|
|
231
|
-
}
|
|
232
|
-
assertionInfo.patternId = patternId;
|
|
233
|
-
const rules = patternRuleMap[patternId];
|
|
234
|
-
processRules(rules, assertionInfo, options);
|
|
235
|
-
}
|
|
236
|
-
return getReturnObject(assertionInfo);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
async function validateAsync(xml, schematron, options = {}) {
|
|
240
|
-
initializeProcess(xml, schematron, options);
|
|
241
|
-
for (const patternId in patternRuleMap) {
|
|
242
|
-
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
243
|
-
continue;
|
|
244
|
-
}
|
|
245
|
-
assertionInfo.patternId = patternId;
|
|
246
|
-
const rules = patternRuleMap[patternId];
|
|
247
|
-
await processRulesAsync(rules, assertionInfo, options);
|
|
248
|
-
}
|
|
249
|
-
return getReturnObject(assertionInfo);
|
|
250
|
-
}
|
|
251
|
-
function getSelectXmlChunk(context) {
|
|
252
|
-
// Determine the sections within context, load selected section from cache if possible
|
|
253
|
-
let selectedXml = contextMap[context];
|
|
254
|
-
let contextModified = context;
|
|
255
|
-
if (!selectedXml) {
|
|
256
|
-
if (context) {
|
|
257
|
-
if (context.indexOf('/')) {
|
|
258
|
-
contextModified = '//' + context;
|
|
259
|
-
}
|
|
260
|
-
selectedXml = xpathSelect(contextModified, xmlDoc);
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
selectedXml = [xmlDoc];
|
|
264
|
-
}
|
|
265
|
-
contextMap[context] = selectedXml;
|
|
266
|
-
}
|
|
267
|
-
return selectedXml;
|
|
268
|
-
}
|
|
269
|
-
function getAssertionObject(level, id, originalTest, simplifiedTest, description) {
|
|
270
|
-
const assertionObject = {
|
|
271
|
-
type: level,
|
|
272
|
-
assertionId: id,
|
|
273
|
-
test: originalTest,
|
|
274
|
-
simplifiedTest: simplifiedTest,
|
|
275
|
-
description: description
|
|
276
|
-
};
|
|
277
|
-
return assertionObject;
|
|
278
|
-
}
|
|
279
|
-
function processExtension(assertionAndExtensionObject, context, options) {
|
|
280
|
-
const extensionRule = assertionAndExtensionObject.rule;
|
|
281
|
-
let failedSubAssertions = [];
|
|
282
|
-
if (extensionRule) {
|
|
283
|
-
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
284
|
-
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
285
|
-
if (subAssertionsAndExtensions) {
|
|
286
|
-
failedSubAssertions = checkRule(context, subAssertionsAndExtensions, options);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return failedSubAssertions;
|
|
290
|
-
}
|
|
291
|
-
function processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options) {
|
|
292
|
-
// const failedAssertions = [];
|
|
293
|
-
let { level, test, id, description } = assertionAndExtensionObject;
|
|
294
|
-
// Extract values from external document and modify test if a document call is made
|
|
295
|
-
const originalTest = test;
|
|
296
|
-
let simplifiedTest = null;
|
|
297
|
-
try {
|
|
298
|
-
test = includeExternalDocument(test, resourceDir);
|
|
299
|
-
if (originalTest !== test) {
|
|
300
|
-
simplifiedTest = test;
|
|
301
|
-
}
|
|
302
|
-
if (level === 'error' || _get(options, 'includeWarnings')) {
|
|
303
|
-
const testAssertionResponse = testAssertion(test, selectedXml, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength);
|
|
304
|
-
if ((Array.isArray(testAssertionResponse) && testAssertionResponse.length) || _get(testAssertionResponse, 'ignored')) {
|
|
305
|
-
const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
|
|
306
|
-
assertionObject.results = testAssertionResponse;
|
|
307
|
-
failedAssertions.push(assertionObject);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
catch (err) {
|
|
312
|
-
const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
|
|
313
|
-
assertionObject.results = { ignored: true, errorMessage: err.message };
|
|
314
|
-
failedAssertions.push(assertionObject);
|
|
315
|
-
}
|
|
316
|
-
return failedAssertions;
|
|
317
|
-
}
|
|
318
|
-
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
319
|
-
function checkRule(context, assertionsAndExtensions, options) {
|
|
320
|
-
// Context cache
|
|
321
|
-
const failedAssertions = [];
|
|
322
|
-
// Determine the sections within context, load selected section from cache if possible
|
|
323
|
-
const selectedXml = getSelectXmlChunk(context);
|
|
324
|
-
for (let i = 0; i < assertionsAndExtensions.length; i++) {
|
|
325
|
-
const assertionAndExtensionObject = assertionsAndExtensions[i];
|
|
326
|
-
if (assertionAndExtensionObject.type === 'assertion') {
|
|
327
|
-
processAssertions(assertionAndExtensionObject, selectedXml, failedAssertions, options);
|
|
328
|
-
}
|
|
329
|
-
else {
|
|
330
|
-
const failedSubAssertions = processExtension(assertionAndExtensionObject, context, options);
|
|
331
|
-
failedAssertions.push(...failedSubAssertions);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return failedAssertions;
|
|
335
|
-
}
|