cda-schematron-validator 1.1.11 → 2.0.0-preview.1
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/LICENSE.md +9 -9
- package/README.md +222 -239
- package/bin/cda-validate.js +36 -0
- package/docs/api.md +134 -0
- package/docs/architecture.md +102 -0
- package/docs/coverage.md +112 -0
- package/docs/deployment.md +126 -0
- package/docs/history.md +245 -0
- package/docs/legacy-api.md +70 -0
- package/lib/checkIfFileOrPath.js +27 -28
- package/lib/checkRule.js +97 -98
- package/lib/createClient.js +35 -0
- package/lib/getAssertionObject.js +23 -23
- package/lib/getErrorsAndWarnings.js +41 -41
- package/lib/getFailedAssertions.js +24 -25
- package/lib/getIgnored.js +27 -27
- package/lib/getResults.js +22 -22
- package/lib/getReturnObject.js +20 -20
- package/lib/getSchematronMap.js +34 -31
- package/lib/getSelectXmlChunk.js +27 -27
- package/lib/includeExternalDocument.js +113 -107
- package/lib/initializeProcess.js +34 -40
- package/lib/parseSchematron.js +122 -122
- package/lib/processFailedAssertions.js +17 -17
- package/lib/processRules.js +20 -20
- package/lib/processRulesAsync.js +18 -30
- package/lib/testAssertion.js +74 -74
- package/package.json +67 -59
- package/validateAsync.js +103 -117
- package/validateSync.js +51 -55
- package/validator.d.ts +87 -0
- package/validator.js +17 -16
- package/.eslintrc.js +0 -30
- package/.github/workflows/codeql-analysis.yml +0 -70
- package/.husky/pre-commit +0 -5
- package/.jshintrc +0 -96
- package/.vscode/launch.json +0 -37
- package/Gruntfile.js +0 -28
- package/SECURITY.md +0 -21
- package/test/CDAR2_IG_CCDA_CLINNOTES_R1_DSTU2.1_2015AUG_Vol2_2019JUNwith_errata.sch +0 -71
- package/test/VitalSigns.xml +0 -856
- package/test/async.test.js +0 -124
- package/test/asyncFileList.test.js +0 -61
- package/test/test.sch +0 -47
- package/test/test.xml +0 -80
- package/test/tests.js +0 -140
- package/test/vitalSigns.test.js +0 -34
- package/test/voc.xml +0 -44
|
@@ -1,107 +1,113 @@
|
|
|
1
|
-
// jshint node:true
|
|
2
|
-
// jshint shadow:true
|
|
3
|
-
module.exports = modifyTest;
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const xpath = require('xpath');
|
|
7
|
-
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
8
|
-
const path = require('path');
|
|
9
|
-
|
|
10
|
-
const loadedExternalDocuments = Object.create(
|
|
11
|
-
|
|
12
|
-
function modifyTest(test, resourceDir) {
|
|
13
|
-
let matches = /=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
14
|
-
while (matches) {
|
|
15
|
-
// String processing to select the non-regular predicate expression
|
|
16
|
-
const equalInd = test.indexOf(matches[0]);
|
|
17
|
-
let start = equalInd;
|
|
18
|
-
let bracketDepth = 0;
|
|
19
|
-
for (let i = equalInd; i >= 0; i--) {
|
|
20
|
-
if (!bracketDepth && (test[i] === '[' || test[i] === ' ')) {
|
|
21
|
-
start = i + 1;
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
24
|
-
if (test[i] === ']') {
|
|
25
|
-
bracketDepth++;
|
|
26
|
-
}
|
|
27
|
-
else if (test[i] === '[') {
|
|
28
|
-
bracketDepth--;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let end = test.length;
|
|
33
|
-
bracketDepth = 0;
|
|
34
|
-
for (let i = start + matches[0].length; i < test.length; i++) {
|
|
35
|
-
if (!bracketDepth && (test[i] === ']' || test[i] === ' ')) {
|
|
36
|
-
end = i;
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
if (test[i] === '[') {
|
|
40
|
-
bracketDepth++;
|
|
41
|
-
}
|
|
42
|
-
else if (test[i] === ']') {
|
|
43
|
-
bracketDepth--;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const predicate = test.slice(start, end);
|
|
48
|
-
|
|
49
|
-
// Load external doc (load from "cache" if already loaded)
|
|
50
|
-
const filepath = matches[1].slice(1, -1);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
1
|
+
// jshint node:true
|
|
2
|
+
// jshint shadow:true
|
|
3
|
+
module.exports = modifyTest;
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const xpath = require('xpath');
|
|
7
|
+
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
8
|
+
const path = require('path');
|
|
9
|
+
|
|
10
|
+
const loadedExternalDocuments = Object.create(null);
|
|
11
|
+
|
|
12
|
+
function modifyTest(test, resourceDir) {
|
|
13
|
+
let matches = /=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
14
|
+
while (matches) {
|
|
15
|
+
// String processing to select the non-regular predicate expression
|
|
16
|
+
const equalInd = test.indexOf(matches[0]);
|
|
17
|
+
let start = equalInd;
|
|
18
|
+
let bracketDepth = 0;
|
|
19
|
+
for (let i = equalInd; i >= 0; i--) {
|
|
20
|
+
if (!bracketDepth && (test[i] === '[' || test[i] === ' ')) {
|
|
21
|
+
start = i + 1;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
if (test[i] === ']') {
|
|
25
|
+
bracketDepth++;
|
|
26
|
+
}
|
|
27
|
+
else if (test[i] === '[') {
|
|
28
|
+
bracketDepth--;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let end = test.length;
|
|
33
|
+
bracketDepth = 0;
|
|
34
|
+
for (let i = start + matches[0].length; i < test.length; i++) {
|
|
35
|
+
if (!bracketDepth && (test[i] === ']' || test[i] === ' ')) {
|
|
36
|
+
end = i;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (test[i] === '[') {
|
|
40
|
+
bracketDepth++;
|
|
41
|
+
}
|
|
42
|
+
else if (test[i] === ']') {
|
|
43
|
+
bracketDepth--;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const predicate = test.slice(start, end);
|
|
48
|
+
|
|
49
|
+
// Load external doc (load from "cache" if already loaded)
|
|
50
|
+
const filepath = matches[1].slice(1, -1);
|
|
51
|
+
const absolutePath = path.resolve(resourceDir, filepath);
|
|
52
|
+
const stat = fs.statSync(absolutePath);
|
|
53
|
+
const cacheKey = absolutePath + ':' + stat.mtimeMs + ':' + stat.size;
|
|
54
|
+
let externalDoc;
|
|
55
|
+
if (!loadedExternalDocuments[cacheKey]) {
|
|
56
|
+
let externalXml = null;
|
|
57
|
+
try {
|
|
58
|
+
externalXml = fs.readFileSync(path.join(resourceDir, filepath), 'utf-8').toString();
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
throw new Error('No such file \'' + filepath + '\'');
|
|
62
|
+
}
|
|
63
|
+
externalDoc = new Dom().parseFromString(externalXml);
|
|
64
|
+
if (Object.keys(loadedExternalDocuments).length >= 32) {
|
|
65
|
+
delete loadedExternalDocuments[Object.keys(loadedExternalDocuments)[0]];
|
|
66
|
+
}
|
|
67
|
+
loadedExternalDocuments[cacheKey] = externalDoc;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
externalDoc = loadedExternalDocuments[cacheKey];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const externalXpath = test.slice(equalInd + matches[0].length, end);
|
|
74
|
+
|
|
75
|
+
// Extract namespaces
|
|
76
|
+
const defaultNamespaceKey = /([^(<>.\/)]+):[^(<>.\/)]+/.exec(externalXpath)[1];
|
|
77
|
+
const externalNamespaceMap = externalDoc.lastChild._nsMap;
|
|
78
|
+
const namespaceMap = {};
|
|
79
|
+
for (const key in externalNamespaceMap) {
|
|
80
|
+
if (Object.hasOwn(externalNamespaceMap, key)) {
|
|
81
|
+
if (key) {
|
|
82
|
+
namespaceMap[key] = externalNamespaceMap[key];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
namespaceMap[defaultNamespaceKey] = externalNamespaceMap[''];
|
|
87
|
+
|
|
88
|
+
const externalSelect = xpath.useNamespaces(namespaceMap);
|
|
89
|
+
|
|
90
|
+
// Create new predicate from extract values
|
|
91
|
+
const values = [];
|
|
92
|
+
const externalResults = externalSelect(externalXpath, externalDoc);
|
|
93
|
+
for (let i = 0; i < externalResults.length; i++) {
|
|
94
|
+
values.push(externalResults[i].value);
|
|
95
|
+
}
|
|
96
|
+
const lhv = predicate.slice(0, predicate.indexOf('=document('));
|
|
97
|
+
let newPredicate = '(';
|
|
98
|
+
for (let i = 0; i < values.length; i++) {
|
|
99
|
+
newPredicate += lhv + '=\'' + values[i] + '\'';
|
|
100
|
+
if (i < values.length - 1) {
|
|
101
|
+
newPredicate += ' or ';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
newPredicate += ')';
|
|
105
|
+
|
|
106
|
+
// Replace test
|
|
107
|
+
test = test.slice(0, start) + newPredicate + test.slice(end);
|
|
108
|
+
|
|
109
|
+
matches = /@[^\[\]]+=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return test;
|
|
113
|
+
}
|
package/lib/initializeProcess.js
CHANGED
|
@@ -1,40 +1,34 @@
|
|
|
1
|
-
const xpath = require('xpath');
|
|
2
|
-
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @param {string}
|
|
10
|
-
* @param {
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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;
|
|
1
|
+
const xpath = require('xpath');
|
|
2
|
+
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
3
|
+
const checkIfFileOrPath = require('./checkIfFileOrPath');
|
|
4
|
+
const getSchematronMap = require('./getSchematronMap');
|
|
5
|
+
/**
|
|
6
|
+
* @method initializeProcess Checks the validity of the supplied xml and schematron, and sets some global variables used across functions in this script.
|
|
7
|
+
* This calls the `getSchematronMap` and assigns the value to schematronMap variable.
|
|
8
|
+
* @param {string} xml string content of an xml file or path to an xml file
|
|
9
|
+
* @param {string} schematron string content of a schematron file or path to a schematron file
|
|
10
|
+
* @param {object} options object that takes parameters as seen in the `validate` method
|
|
11
|
+
* Sets some method specific variables that will be passed to other methods using the options object
|
|
12
|
+
* options.xmlDoc, options.schematronMap, options.resourceDir, options.xmlSnippetMaxLength, and options.xpathSelect
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function initializeProcess(xml, schematron, options) {
|
|
16
|
+
xml = checkIfFileOrPath(xml);
|
|
17
|
+
if (!options.parsedSchematronMap) schematron = checkIfFileOrPath(schematron);
|
|
18
|
+
// Load xml doc
|
|
19
|
+
options.xmlDoc = new Dom().parseFromString(xml);
|
|
20
|
+
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
21
|
+
options.schematronMap = getSchematronMap(schematron, options);
|
|
22
|
+
// Extract data from parsed schematron object
|
|
23
|
+
if (!options?.resourceDir) {
|
|
24
|
+
options.resourceDir = '../';
|
|
25
|
+
}
|
|
26
|
+
if (options.xmlSnippetMaxLength === undefined) {
|
|
27
|
+
options.xmlSnippetMaxLength = 200;
|
|
28
|
+
}
|
|
29
|
+
// Create selector object, initialized with namespaces
|
|
30
|
+
// Avoid using 'select' as a variable name as it is overused
|
|
31
|
+
options.xpathSelect = xpath.useNamespaces(options.schematronMap.namespaceMap);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = initializeProcess;
|
package/lib/parseSchematron.js
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
// jshint node:true
|
|
2
|
-
// jshint shadow:true
|
|
3
|
-
module.exports = parseSchematron;
|
|
4
|
-
|
|
5
|
-
const xpath = require('xpath');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
patternRuleMap[patternId].push(
|
|
65
|
-
ruleAssertionMap[
|
|
66
|
-
abstract: parseAbstract(rules[j].getAttribute('abstract')),
|
|
67
|
-
context: parseContext(rules[j].getAttribute('context')),
|
|
68
|
-
assertionsAndExtensions: getAssertionsAndExtensions(rules[j], defaultLevel)
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
namespaceMap: namespaceMap,
|
|
75
|
-
patternRuleMap: patternRuleMap,
|
|
76
|
-
ruleAssertionMap: ruleAssertionMap
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function getAssertionsAndExtensions(rule, defaultLevel) {
|
|
81
|
-
const assertionsAndExtensions = [];
|
|
82
|
-
|
|
83
|
-
// Find and store assertions
|
|
84
|
-
const assertions = xpath.select('./*[local-name()="assert"]', rule);
|
|
85
|
-
for (let i = 0; i < assertions.length; i++) {
|
|
86
|
-
const description = assertions[i].childNodes[0] ? assertions[i].childNodes[0].data : '';
|
|
87
|
-
let level = defaultLevel;
|
|
88
|
-
if (description.indexOf('SHALL') !== -1 &&
|
|
89
|
-
(description.indexOf('SHOULD') === -1 || description.indexOf('SHALL') < description.indexOf('SHOULD'))) {
|
|
90
|
-
level = 'error';
|
|
91
|
-
}
|
|
92
|
-
assertionsAndExtensions.push({
|
|
93
|
-
type: 'assertion',
|
|
94
|
-
level: level,
|
|
95
|
-
id: assertions[i].getAttribute('id'),
|
|
96
|
-
test: assertions[i].getAttribute('test'),
|
|
97
|
-
description: description
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Find and store extensions
|
|
102
|
-
const extensions = xpath.select('./*[local-name()="extends"]', rule);
|
|
103
|
-
for (let i = 0; i < extensions.length; i++) {
|
|
104
|
-
assertionsAndExtensions.push({
|
|
105
|
-
type: 'extension',
|
|
106
|
-
rule: extensions[i].getAttribute('rule')
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return assertionsAndExtensions;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function parseAbstract(str) {
|
|
114
|
-
if (str === 'true' || str === 'yes') {
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function parseContext(str) {
|
|
121
|
-
return str || null;
|
|
122
|
-
}
|
|
1
|
+
// jshint node:true
|
|
2
|
+
// jshint shadow:true
|
|
3
|
+
module.exports = parseSchematron;
|
|
4
|
+
|
|
5
|
+
const xpath = require('xpath');
|
|
6
|
+
|
|
7
|
+
function parseSchematron(doc, options) {
|
|
8
|
+
// Extract data from schematron
|
|
9
|
+
const schematronData = extract(doc, options);
|
|
10
|
+
|
|
11
|
+
return schematronData;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function extract(doc, options) {
|
|
15
|
+
const namespaceMap = Object.create(null);
|
|
16
|
+
const patternLevelMap = Object.create(null);
|
|
17
|
+
const patternRuleMap = Object.create(null);
|
|
18
|
+
const ruleAssertionMap = Object.create(null);
|
|
19
|
+
// // Namespace mapping
|
|
20
|
+
const namespaces = xpath.select('//*[local-name()="ns"]', doc);
|
|
21
|
+
for (let i = 0; i < namespaces.length; i++) {
|
|
22
|
+
namespaceMap[namespaces[i].getAttribute('prefix')] = namespaces[i].getAttribute('uri');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// // Pattern to level mapping
|
|
26
|
+
|
|
27
|
+
// Find errors phases
|
|
28
|
+
const errorPhase = xpath.select('//*[local-name()="phase" and @id="errors"]', doc);
|
|
29
|
+
|
|
30
|
+
// Store error patterns
|
|
31
|
+
if (errorPhase.length) {
|
|
32
|
+
for (let i = 0; i < errorPhase[0].childNodes.length; i++) {
|
|
33
|
+
if (errorPhase[0].childNodes[i].localName === 'active') {
|
|
34
|
+
patternLevelMap[errorPhase[0].childNodes[i].getAttribute('pattern')] = 'error';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Find errors phases
|
|
40
|
+
const warningPhase = xpath.select('//*[local-name()="phase" and @id="warnings"]', doc);
|
|
41
|
+
|
|
42
|
+
// Store warning patterns
|
|
43
|
+
if (options?.includeWarnings && warningPhase.length) {
|
|
44
|
+
for (let i = 0; i < warningPhase[0].childNodes.length; i++) {
|
|
45
|
+
if (warningPhase[0].childNodes[i].localName === 'active') {
|
|
46
|
+
patternLevelMap[warningPhase[0].childNodes[i].getAttribute('pattern')] = 'warning';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// // Pattern to rule and rule to assertion mapping
|
|
52
|
+
|
|
53
|
+
// Find patterns
|
|
54
|
+
const patterns = xpath.select('//*[local-name()="pattern"]', doc);
|
|
55
|
+
|
|
56
|
+
// Map patterns to rules
|
|
57
|
+
for (let i = 0; i < patterns.length; i++) {
|
|
58
|
+
const patternId = patterns[i].getAttribute('id') || '__pattern_' + i;
|
|
59
|
+
const defaultLevel = patternLevelMap[patternId] || 'warning';
|
|
60
|
+
patternRuleMap[patternId] = [];
|
|
61
|
+
const rules = xpath.select('./*[local-name()="rule"]', patterns[i]);
|
|
62
|
+
for (let j = 0; j < rules.length; j++) {
|
|
63
|
+
const ruleId = rules[j].getAttribute('id') || '__rule_' + i + '_' + j;
|
|
64
|
+
patternRuleMap[patternId].push(ruleId);
|
|
65
|
+
ruleAssertionMap[ruleId] = {
|
|
66
|
+
abstract: parseAbstract(rules[j].getAttribute('abstract')),
|
|
67
|
+
context: parseContext(rules[j].getAttribute('context')),
|
|
68
|
+
assertionsAndExtensions: getAssertionsAndExtensions(rules[j], defaultLevel)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
namespaceMap: namespaceMap,
|
|
75
|
+
patternRuleMap: patternRuleMap,
|
|
76
|
+
ruleAssertionMap: ruleAssertionMap
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getAssertionsAndExtensions(rule, defaultLevel) {
|
|
81
|
+
const assertionsAndExtensions = [];
|
|
82
|
+
|
|
83
|
+
// Find and store assertions
|
|
84
|
+
const assertions = xpath.select('./*[local-name()="assert"]', rule);
|
|
85
|
+
for (let i = 0; i < assertions.length; i++) {
|
|
86
|
+
const description = assertions[i].childNodes[0] ? assertions[i].childNodes[0].data : '';
|
|
87
|
+
let level = defaultLevel;
|
|
88
|
+
if (description.indexOf('SHALL') !== -1 &&
|
|
89
|
+
(description.indexOf('SHOULD') === -1 || description.indexOf('SHALL') < description.indexOf('SHOULD'))) {
|
|
90
|
+
level = 'error';
|
|
91
|
+
}
|
|
92
|
+
assertionsAndExtensions.push({
|
|
93
|
+
type: 'assertion',
|
|
94
|
+
level: level,
|
|
95
|
+
id: assertions[i].getAttribute('id'),
|
|
96
|
+
test: assertions[i].getAttribute('test'),
|
|
97
|
+
description: description
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Find and store extensions
|
|
102
|
+
const extensions = xpath.select('./*[local-name()="extends"]', rule);
|
|
103
|
+
for (let i = 0; i < extensions.length; i++) {
|
|
104
|
+
assertionsAndExtensions.push({
|
|
105
|
+
type: 'extension',
|
|
106
|
+
rule: extensions[i].getAttribute('rule')
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return assertionsAndExtensions;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function parseAbstract(str) {
|
|
114
|
+
if (str === 'true' || str === 'yes') {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function parseContext(str) {
|
|
121
|
+
return str || null;
|
|
122
|
+
}
|
|
@@ -1,17 +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;
|
|
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;
|
package/lib/processRules.js
CHANGED
|
@@ -1,20 +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;
|
|
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;
|