cda-schematron-validator 1.1.2 → 1.1.4
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/.eslintrc.js +30 -0
- package/.husky/pre-commit +5 -0
- package/.jshintrc +1 -1
- package/Gruntfile.js +8 -11
- package/README.md +59 -13
- package/includeExternalDocument.js +28 -30
- package/package.json +9 -3
- package/parseSchematron.js +41 -41
- package/test/async.test.js +9 -9
- package/test/tests.js +12 -13
- package/test/vitalSigns.test.js +8 -7
- package/testAssertion.js +10 -9
- package/validator.js +44 -44
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
'env': {
|
|
3
|
+
'browser': true,
|
|
4
|
+
'commonjs': true,
|
|
5
|
+
'es2021': true
|
|
6
|
+
},
|
|
7
|
+
'extends': [
|
|
8
|
+
'google'
|
|
9
|
+
],
|
|
10
|
+
'parserOptions': {
|
|
11
|
+
'ecmaVersion': 'latest'
|
|
12
|
+
},
|
|
13
|
+
'rules': {
|
|
14
|
+
'comma-dangle': ['error', 'never'],
|
|
15
|
+
'linebreak-style': 0,
|
|
16
|
+
'indent': ['error', 4],
|
|
17
|
+
'require-jsdoc': 0,
|
|
18
|
+
'object-curly-spacing': 0,
|
|
19
|
+
'func-call-spacing': 0,
|
|
20
|
+
'max-len': [2, {
|
|
21
|
+
'code': 200,
|
|
22
|
+
'tabWidth': 4,
|
|
23
|
+
'ignoreUrls': true,
|
|
24
|
+
'ignorePattern': 'goog\.(module|require)',
|
|
25
|
+
'ignoreStrings': true,
|
|
26
|
+
'ignoreComments': true
|
|
27
|
+
}],
|
|
28
|
+
'brace-style': ['error', 'stroustrup']
|
|
29
|
+
}
|
|
30
|
+
};
|
package/.jshintrc
CHANGED
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"debug": true, // true: Allow debugger statements e.g. browser breakpoints.
|
|
41
41
|
"eqnull": false, // true: Tolerate use of `== null`
|
|
42
42
|
"es5": false, // true: Allow ES5 syntax (ex: getters and setters)
|
|
43
|
-
"esversion":
|
|
43
|
+
"esversion": 8,
|
|
44
44
|
"esnext": false, // true: Allow ES.next (ES6) syntax (ex: `const`)
|
|
45
45
|
"moz": false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
|
|
46
46
|
// (ex: `for each`, multiple try/catch, function expression...)
|
package/Gruntfile.js
CHANGED
|
@@ -1,31 +1,28 @@
|
|
|
1
1
|
// jshint node:true
|
|
2
|
-
|
|
2
|
+
const path = require('path');
|
|
3
|
+
module.exports = function(grunt) {
|
|
3
4
|
grunt.initConfig({
|
|
4
|
-
|
|
5
|
+
jshint: {
|
|
5
6
|
options: {
|
|
6
|
-
jshintrc: '.jshintrc'
|
|
7
|
+
jshintrc: path.resolve(__dirname, '.jshintrc')
|
|
7
8
|
},
|
|
8
9
|
files: {
|
|
9
|
-
src: ['*.js']},
|
|
10
|
+
src: [path.resolve(__dirname, '*.js')]},
|
|
10
11
|
gruntfile: {
|
|
11
|
-
src: 'Gruntfile.js'
|
|
12
|
+
src: path.resolve(__dirname, 'Gruntfile.js')
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
mochaTest: {
|
|
15
16
|
api: {
|
|
17
|
+
src: [path.resolve(__dirname, 'test/*test*.js')],
|
|
16
18
|
options: {
|
|
17
19
|
reporter: 'spec'
|
|
18
|
-
}
|
|
19
|
-
src: ['test/tests.js']
|
|
20
|
+
}
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
});
|
|
23
|
-
|
|
24
24
|
grunt.loadNpmTasks('grunt-contrib-jshint');
|
|
25
25
|
grunt.loadNpmTasks('grunt-mocha-test');
|
|
26
|
-
|
|
27
26
|
grunt.registerTask('default', ['jshint', 'mochaTest']);
|
|
28
27
|
grunt.registerTask('api', ['mochaTest']);
|
|
29
|
-
|
|
30
28
|
};
|
|
31
|
-
|
package/README.md
CHANGED
|
@@ -1,34 +1,68 @@
|
|
|
1
1
|
# [cda-schematron-validator](https://github.com/priyaranjan-tokachichu/cda-schematron-validator)
|
|
2
|
+
|
|
2
3
|
## Release History
|
|
4
|
+
|
|
3
5
|
Starting release history with 1.0.7
|
|
4
|
-
|
|
6
|
+
|
|
7
|
+
### 1.1.4 (Stable Release)
|
|
8
|
+
|
|
9
|
+
- Major deviations from the source repository
|
|
10
|
+
|
|
11
|
+
- By default the warning will be turned off in contrast to the parent repository
|
|
12
|
+
|
|
13
|
+
- To get warnings, users have to explicitly specify the option `includeWarnings` with value boolean `true`
|
|
14
|
+
|
|
15
|
+
- We also deviate from the previous behavior where all the checks for warnings were done at the assertion
|
|
16
|
+
level just before testing the xpath. We now would not even parse the phase with @id="warnings" which should
|
|
17
|
+
improve the performance.
|
|
18
|
+
|
|
19
|
+
- Minor cosmetic changes to the code
|
|
20
|
+
|
|
21
|
+
### 1.1.1 to 1.1.3
|
|
22
|
+
|
|
5
23
|
- Only keeping the necessary data in the test files taken from HL7 C-CDA schematron repository to avoid bloating the module
|
|
24
|
+
|
|
25
|
+
- Added pre-commit hooks with husky
|
|
26
|
+
|
|
27
|
+
- Added code standardization with prettier, ESLint, and lint-staged
|
|
28
|
+
|
|
6
29
|
### 1.1.0
|
|
30
|
+
|
|
7
31
|
- Fix the issue with the rule check which was working in the original repository, but not in the fork
|
|
8
|
-
- Added HL7 C-CDA schematron file and resource file from https://github.com/HL7/CDA-ccda-2.1/releases/tag/2019JUN
|
|
32
|
+
- Added HL7 C-CDA schematron file and resource file from <https://github.com/HL7/CDA-ccda-2.1/releases/tag/2019JUN>
|
|
9
33
|
- Added a test case vitalSigns.test.js that tests the resource file and sub assertions
|
|
10
|
-
- Used resources https://github.com/jddamore/ccda-samples and https://github.com/HL7/C-CDA-Examples/tree/master/Vital%20Signs to create the test file vitalSigns.xml
|
|
34
|
+
- Used resources <https://github.com/jddamore/ccda-samples> and <https://github.com/HL7/C-CDA-Examples/tree/master/Vital%20Signs> to create the test file vitalSigns.xml
|
|
35
|
+
|
|
11
36
|
### 1.0.10
|
|
37
|
+
|
|
12
38
|
- Fix null context issue associated with assertions within assertions
|
|
13
39
|
- Skip abstract rules associated with assertions within assertions
|
|
40
|
+
|
|
14
41
|
### 1.0.9
|
|
42
|
+
|
|
15
43
|
- Fixed the bug introduced in 1.0.8 trying to adjust to different data types of the return value
|
|
16
44
|
- Changed variable names to improve understanding of the process
|
|
45
|
+
|
|
17
46
|
### 1.0.8
|
|
47
|
+
|
|
18
48
|
- Bug fix identifying the ignored errors
|
|
49
|
+
|
|
19
50
|
### 1.0.7
|
|
51
|
+
|
|
20
52
|
- Added async validate function `validateAsync`
|
|
21
53
|
- Added `testsAsync` to test `validateAsync`
|
|
22
54
|
- Plan to use common functions between sync and async methods in the future release
|
|
23
55
|
|
|
24
56
|
### 1.0.0 to 1.0.6
|
|
25
|
-
|
|
57
|
+
|
|
58
|
+
A fork to the original cda-schematron. All credit to Eric Wadkins. Visit the github <https://github.com/ewadkins/cda-schematron> for the original repo.
|
|
26
59
|
|
|
27
60
|
This is just a cosmetic change with updated node modules, a couple of minor bugs observed, and esversion 6 changes.
|
|
28
61
|
|
|
29
|
-
The changes are fully backward compatible. So, leaving the original documentation as is below.
|
|
62
|
+
The changes are fully backward compatible. So, leaving the original documentation as is below.
|
|
30
63
|
|
|
31
64
|
### New option parameter parsedSchematronMap
|
|
65
|
+
|
|
32
66
|
In addition to everything that was possible,
|
|
33
67
|
you can now supply a new option parameter `parsedSchematronMap`. You can create a schematron map as follows.
|
|
34
68
|
|
|
@@ -44,24 +78,25 @@ const options = {
|
|
|
44
78
|
};
|
|
45
79
|
const results = validate('xml string or file path', 'schematron sting, file path or null', options);
|
|
46
80
|
```
|
|
81
|
+
|
|
47
82
|
If a schematron map is provided, the schematron string or path will not be read.
|
|
48
83
|
|
|
49
84
|
To install this npm module, it will be
|
|
50
85
|
|
|
86
|
+
```javascript
|
|
87
|
+
npm i cda-schematron-validator
|
|
51
88
|
```
|
|
52
|
-
npm install cda-schematron-validator
|
|
53
|
-
```
|
|
54
|
-
Mocha VS Code configuratin added to directly execute the test file in `test/tests.js` using VS Code.
|
|
55
89
|
|
|
90
|
+
Mocha VS Code configuratin added to directly execute the test file in `test/tests.js` using VS Code.
|
|
56
91
|
|
|
57
|
-
|
|
58
|
-
## [cda-schematron](https://github.com/ewadkins/cda-schematron)
|
|
92
|
+
## Documentation of the parent repo [cda-schematron](https://github.com/ewadkins/cda-schematron)
|
|
59
93
|
|
|
60
94
|
A javascript implementation of schematron testing for XML documents. This specifically resolves a need for a package that allows a quick, reliable install for validating HL7 clinical documents, such as C-CDA.
|
|
61
95
|
|
|
62
96
|
Check out [cda-schematron-server](https://github.com/ewadkins/cda-schematron-server), a server wrapper of **cda-schematron**, for easy schematron validation.
|
|
63
97
|
|
|
64
98
|
### Validating xml
|
|
99
|
+
|
|
65
100
|
```javascript
|
|
66
101
|
var validator = require('cda-schematron');
|
|
67
102
|
|
|
@@ -74,21 +109,27 @@ var schematron = fs.readFileSync(schematronPath).toString();
|
|
|
74
109
|
|
|
75
110
|
var results = validator.validate(xml, schematron);
|
|
76
111
|
```
|
|
112
|
+
|
|
77
113
|
File paths can also be passed to the validator directly. The following lines all return the same results:
|
|
114
|
+
|
|
78
115
|
```javascript
|
|
79
116
|
var results = validator.validate(xml, schematronPath);
|
|
80
117
|
```
|
|
118
|
+
|
|
81
119
|
```javascript
|
|
82
120
|
var results = validator.validate(xmlPath, schematron);
|
|
83
121
|
```
|
|
122
|
+
|
|
84
123
|
```javascript
|
|
85
124
|
var results = validator.validate(xmlPath, schematronPath);
|
|
86
125
|
```
|
|
87
126
|
|
|
88
127
|
### Results
|
|
128
|
+
|
|
89
129
|
```results``` is an object containing arrays ```errors```, ```warnings```, and ```ignoreds```.
|
|
90
130
|
|
|
91
131
|
**Errors** and **warnings** are reported as determined by the schematron and test descriptions. They are of the following form:
|
|
132
|
+
|
|
92
133
|
```javascript
|
|
93
134
|
{
|
|
94
135
|
type: type, // "error" or "warning"
|
|
@@ -106,6 +147,7 @@ var results = validator.validate(xmlPath, schematronPath);
|
|
|
106
147
|
```
|
|
107
148
|
|
|
108
149
|
**Ignored** tests are those that resulted in an exception while running (eg. the test is invalid xpath and could not be parsed properly) and require manual inspection. They are of the following form:
|
|
150
|
+
|
|
109
151
|
```javascript
|
|
110
152
|
{
|
|
111
153
|
errorMessage: errorMessage, // reason for the exception/ignoring the test
|
|
@@ -121,13 +163,14 @@ var results = validator.validate(xmlPath, schematronPath);
|
|
|
121
163
|
```
|
|
122
164
|
|
|
123
165
|
### Options
|
|
166
|
+
|
|
124
167
|
The ```validate``` function takes in an ```options``` object as an optional third argument. The three fields that can be included in ```options``` are as follows:
|
|
125
168
|
|
|
126
|
-
|
|
169
|
+
- **```includeWarnings```**: ```true``` or ```false```, this determines whether or not warnings should be tested and returned. Defaults to ```true```.
|
|
127
170
|
|
|
128
|
-
|
|
171
|
+
- **```resourceDir```**: the path to a directory containing resource files (eg. voc.xml) which may be necessary for some schematron tests. Defaults to ```'./'```, the current directory.
|
|
129
172
|
|
|
130
|
-
|
|
173
|
+
- **```xmlSnippetMaxLength```**: an integer, which is the maximum length of the ```xml``` field in validation results. Defaults to ```200```. Set to ```0``` for unlimited length.
|
|
131
174
|
|
|
132
175
|
Here is an example with warnings disabled:
|
|
133
176
|
|
|
@@ -136,12 +179,15 @@ var results = validator.validate(xml, schematron, { includeWarnings: false });
|
|
|
136
179
|
```
|
|
137
180
|
|
|
138
181
|
### Cache
|
|
182
|
+
|
|
139
183
|
The validator uses a cache to store parsed schematrons, an intermediate data structure used to store revelant schematron information. This reduces the runtime of the validator when validating against the same schematron multiple times. You can clear the cache at any time with:
|
|
184
|
+
|
|
140
185
|
```javascript
|
|
141
186
|
validator.clearCache();
|
|
142
187
|
```
|
|
143
188
|
|
|
144
189
|
---
|
|
190
|
+
|
|
145
191
|
## License (MIT)
|
|
146
192
|
|
|
147
193
|
Copyright © 2017 [Eric Wadkins](http://www.ericwadkins.com/)
|
|
@@ -4,18 +4,16 @@ module.exports = modifyTest;
|
|
|
4
4
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const xpath = require('xpath');
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
8
|
+
const path = require('path');
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const loadedExternalDocuments = Object.create({});
|
|
11
11
|
|
|
12
12
|
function modifyTest(test, resourceDir) {
|
|
13
|
-
|
|
14
13
|
let matches = /=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
15
14
|
while (matches) {
|
|
16
|
-
|
|
17
15
|
// String processing to select the non-regular predicate expression
|
|
18
|
-
|
|
16
|
+
const equalInd = test.indexOf(matches[0]);
|
|
19
17
|
let start = equalInd;
|
|
20
18
|
let bracketDepth = 0;
|
|
21
19
|
for (let i = equalInd; i >= 0; i--) {
|
|
@@ -30,7 +28,7 @@ function modifyTest(test, resourceDir) {
|
|
|
30
28
|
bracketDepth--;
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
|
|
31
|
+
|
|
34
32
|
let end = test.length;
|
|
35
33
|
bracketDepth = 0;
|
|
36
34
|
for (let i = start + matches[0].length; i < test.length; i++) {
|
|
@@ -45,34 +43,34 @@ function modifyTest(test, resourceDir) {
|
|
|
45
43
|
bracketDepth--;
|
|
46
44
|
}
|
|
47
45
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
|
|
47
|
+
const predicate = test.slice(start, end);
|
|
48
|
+
|
|
51
49
|
// Load external doc (load from "cache" if already loaded)
|
|
52
|
-
|
|
50
|
+
const filepath = matches[1].slice(1, -1);
|
|
53
51
|
let externalDoc;
|
|
54
52
|
if (!loadedExternalDocuments[filepath]) {
|
|
55
53
|
let externalXml = null;
|
|
56
54
|
try {
|
|
57
|
-
externalXml = fs.readFileSync(
|
|
55
|
+
externalXml = fs.readFileSync(path.join(resourceDir, filepath), 'utf-8').toString();
|
|
58
56
|
}
|
|
59
57
|
catch (err) {
|
|
60
58
|
throw new Error('No such file \'' + filepath + '\'');
|
|
61
59
|
}
|
|
62
|
-
externalDoc = new
|
|
60
|
+
externalDoc = new Dom().parseFromString(externalXml);
|
|
63
61
|
loadedExternalDocuments[filepath] = externalDoc;
|
|
64
62
|
}
|
|
65
63
|
else {
|
|
66
64
|
externalDoc = loadedExternalDocuments[filepath];
|
|
67
65
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
|
|
67
|
+
const externalXpath = test.slice(equalInd + matches[0].length, end);
|
|
68
|
+
|
|
71
69
|
// Extract namespaces
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
for (
|
|
70
|
+
const defaultNamespaceKey = /([^(<>.\/)]+):[^(<>.\/)]+/.exec(externalXpath)[1];
|
|
71
|
+
const externalNamespaceMap = externalDoc.lastChild._nsMap;
|
|
72
|
+
const namespaceMap = {};
|
|
73
|
+
for (const key in externalNamespaceMap) {
|
|
76
74
|
if (externalNamespaceMap.hasOwnProperty(key)) {
|
|
77
75
|
if (key) {
|
|
78
76
|
namespaceMap[key] = externalNamespaceMap[key];
|
|
@@ -80,16 +78,16 @@ function modifyTest(test, resourceDir) {
|
|
|
80
78
|
}
|
|
81
79
|
}
|
|
82
80
|
namespaceMap[defaultNamespaceKey] = externalNamespaceMap[''];
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
|
|
82
|
+
const externalSelect = xpath.useNamespaces(namespaceMap);
|
|
83
|
+
|
|
86
84
|
// Create new predicate from extract values
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
const values = [];
|
|
86
|
+
const externalResults = externalSelect(externalXpath, externalDoc);
|
|
89
87
|
for (let i = 0; i < externalResults.length; i++) {
|
|
90
88
|
values.push(externalResults[i].value);
|
|
91
89
|
}
|
|
92
|
-
|
|
90
|
+
const lhv = predicate.slice(0, predicate.indexOf('=document('));
|
|
93
91
|
let newPredicate = '(';
|
|
94
92
|
for (let i = 0; i < values.length; i++) {
|
|
95
93
|
newPredicate += lhv + '=\'' + values[i] + '\'';
|
|
@@ -98,12 +96,12 @@ function modifyTest(test, resourceDir) {
|
|
|
98
96
|
}
|
|
99
97
|
}
|
|
100
98
|
newPredicate += ')';
|
|
101
|
-
|
|
99
|
+
|
|
102
100
|
// Replace test
|
|
103
101
|
test = test.slice(0, start) + newPredicate + test.slice(end);
|
|
104
|
-
|
|
102
|
+
|
|
105
103
|
matches = /@[^\[\]]+=document\((\'[-_.A-Za-z0-9]+\'|\"[-_.A-Za-z0-9]+\")\)/.exec(test);
|
|
106
104
|
}
|
|
107
|
-
|
|
105
|
+
|
|
108
106
|
return test;
|
|
109
|
-
}
|
|
107
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cda-schematron-validator",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
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": {
|
|
7
|
-
"test": "grunt"
|
|
7
|
+
"test": "grunt",
|
|
8
|
+
"prepare": "husky install"
|
|
8
9
|
},
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
|
@@ -29,10 +30,15 @@
|
|
|
29
30
|
"homepage": "https://github.com/priyaranjan-tokachichu/cda-schematron-validator#readme",
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"chai": "~4.3.6",
|
|
33
|
+
"eslint": "^8.12.0",
|
|
34
|
+
"eslint-config-google": "^0.14.0",
|
|
32
35
|
"grunt": "~1.4.1",
|
|
33
36
|
"grunt-contrib-jshint": "~3.2.0",
|
|
34
37
|
"grunt-mocha-test": "~0.13.3",
|
|
35
|
-
"
|
|
38
|
+
"husky": "~7.0.4",
|
|
39
|
+
"lint-staged": "~12.3.7",
|
|
40
|
+
"mocha": "~9.2.2",
|
|
41
|
+
"prettier": "~2.6.2"
|
|
36
42
|
},
|
|
37
43
|
"dependencies": {
|
|
38
44
|
"@xmldom/xmldom": "~0.7.2",
|
package/parseSchematron.js
CHANGED
|
@@ -3,31 +3,31 @@
|
|
|
3
3
|
module.exports = parseSchematron;
|
|
4
4
|
|
|
5
5
|
const xpath = require('xpath');
|
|
6
|
+
const _get = require('lodash.get');
|
|
6
7
|
|
|
7
|
-
function parseSchematron(doc) {
|
|
8
|
+
function parseSchematron(doc, options) {
|
|
8
9
|
// Extract data from schematron
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
const schematronData = extract(doc, options);
|
|
11
|
+
|
|
11
12
|
return schematronData;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
function extract(doc) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let namespaces = xpath.select('//*[local-name()="ns"]', doc);
|
|
15
|
+
function extract(doc, options) {
|
|
16
|
+
const namespaceMap = Object.create({});
|
|
17
|
+
const patternLevelMap = Object.create({});
|
|
18
|
+
const patternRuleMap = Object.create({});
|
|
19
|
+
const ruleAssertionMap = Object.create({});
|
|
20
|
+
// // Namespace mapping
|
|
21
|
+
const namespaces = xpath.select('//*[local-name()="ns"]', doc);
|
|
22
22
|
for (let i = 0; i < namespaces.length; i++) {
|
|
23
23
|
namespaceMap[namespaces[i].getAttribute('prefix')] = namespaces[i].getAttribute('uri');
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
|
|
26
|
+
// // Pattern to level mapping
|
|
27
|
+
|
|
28
28
|
// Find errors phases
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const errorPhase = xpath.select('//*[local-name()="phase" and @id="errors"]', doc);
|
|
30
|
+
|
|
31
31
|
// Store error patterns
|
|
32
32
|
if (errorPhase.length) {
|
|
33
33
|
for (let i = 0; i < errorPhase[0].childNodes.length; i++) {
|
|
@@ -36,30 +36,30 @@ function extract(doc) {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
// Find errors phases
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
const warningPhase = xpath.select('//*[local-name()="phase" and @id="warnings"]', doc);
|
|
42
|
+
|
|
43
43
|
// Store warning patterns
|
|
44
|
-
if (warningPhase.length) {
|
|
44
|
+
if (_get(options, 'includeWarnings') && warningPhase.length) {
|
|
45
45
|
for (let i = 0; i < warningPhase[0].childNodes.length; i++) {
|
|
46
46
|
if (warningPhase[0].childNodes[i].localName === 'active') {
|
|
47
47
|
patternLevelMap[warningPhase[0].childNodes[i].getAttribute('pattern')] = 'warning';
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
|
|
52
|
+
// // Pattern to rule and rule to assertion mapping
|
|
53
|
+
|
|
54
54
|
// Find patterns
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
const patterns = xpath.select('//*[local-name()="pattern"]', doc);
|
|
56
|
+
|
|
57
57
|
// Map patterns to rules
|
|
58
58
|
for (let i = 0; i < patterns.length; i++) {
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const patternId = patterns[i].getAttribute('id');
|
|
60
|
+
const defaultLevel = patternLevelMap[patternId] || 'warning';
|
|
61
61
|
patternRuleMap[patternId] = [];
|
|
62
|
-
|
|
62
|
+
const rules = xpath.select('./*[local-name()="rule"]', patterns[i]);
|
|
63
63
|
for (let j = 0; j < rules.length; j++) {
|
|
64
64
|
patternRuleMap[patternId].push(rules[j].getAttribute('id'));
|
|
65
65
|
ruleAssertionMap[rules[j].getAttribute('id')] = {
|
|
@@ -67,9 +67,9 @@ function extract(doc) {
|
|
|
67
67
|
context: parseContext(rules[j].getAttribute('context')),
|
|
68
68
|
assertionsAndExtensions: getAssertionsAndExtensions(rules[j], defaultLevel)
|
|
69
69
|
};
|
|
70
|
-
}
|
|
70
|
+
}
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
return {
|
|
74
74
|
namespaceMap: namespaceMap,
|
|
75
75
|
patternRuleMap: patternRuleMap,
|
|
@@ -78,15 +78,15 @@ function extract(doc) {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
function getAssertionsAndExtensions(rule, defaultLevel) {
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
const assertionsAndExtensions = [];
|
|
82
|
+
|
|
83
83
|
// Find and store assertions
|
|
84
|
-
|
|
84
|
+
const assertions = xpath.select('./*[local-name()="assert"]', rule);
|
|
85
85
|
for (let i = 0; i < assertions.length; i++) {
|
|
86
|
-
|
|
86
|
+
const description = assertions[i].childNodes[0] ? assertions[i].childNodes[0].data : '';
|
|
87
87
|
let level = defaultLevel;
|
|
88
|
-
if (description.indexOf('SHALL') !== -1
|
|
89
|
-
|
|
88
|
+
if (description.indexOf('SHALL') !== -1 &&
|
|
89
|
+
(description.indexOf('SHOULD') === -1 || description.indexOf('SHALL') < description.indexOf('SHOULD'))) {
|
|
90
90
|
level = 'error';
|
|
91
91
|
}
|
|
92
92
|
assertionsAndExtensions.push({
|
|
@@ -97,16 +97,16 @@ function getAssertionsAndExtensions(rule, defaultLevel) {
|
|
|
97
97
|
description: description
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
|
-
|
|
100
|
+
|
|
101
101
|
// Find and store extensions
|
|
102
|
-
|
|
102
|
+
const extensions = xpath.select('./*[local-name()="extends"]', rule);
|
|
103
103
|
for (let i = 0; i < extensions.length; i++) {
|
|
104
104
|
assertionsAndExtensions.push({
|
|
105
105
|
type: 'extension',
|
|
106
|
-
rule: extensions[i].getAttribute('rule')
|
|
106
|
+
rule: extensions[i].getAttribute('rule')
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
|
-
|
|
109
|
+
|
|
110
110
|
return assertionsAndExtensions;
|
|
111
111
|
}
|
|
112
112
|
|
|
@@ -119,4 +119,4 @@ function parseAbstract(str) {
|
|
|
119
119
|
|
|
120
120
|
function parseContext(str) {
|
|
121
121
|
return str || null;
|
|
122
|
-
}
|
|
122
|
+
}
|
package/test/async.test.js
CHANGED
|
@@ -14,7 +14,7 @@ const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
|
14
14
|
describe('Validator should', function() {
|
|
15
15
|
let results;
|
|
16
16
|
it('return results', async function() {
|
|
17
|
-
results = await validator.validateAsync(xml, schematron);
|
|
17
|
+
results = await validator.validateAsync(xml, schematron, { includeWarnings: true });
|
|
18
18
|
expect(results).to.be.an('object');
|
|
19
19
|
});
|
|
20
20
|
it('return errorCount', function() {
|
|
@@ -53,9 +53,9 @@ describe('Validator should', function() {
|
|
|
53
53
|
it('return correct number of ignored', function() {
|
|
54
54
|
expect(results.ignoredCount).to.be.equal(1);
|
|
55
55
|
});
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
it('return similar results without warnings', async function() {
|
|
58
|
-
results = await validator.validateAsync(xml, schematron
|
|
58
|
+
results = await validator.validateAsync(xml, schematron);
|
|
59
59
|
expect(results).to.be.an('object');
|
|
60
60
|
expect(results.errorCount).to.be.a('number');
|
|
61
61
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -70,9 +70,9 @@ describe('Validator should', function() {
|
|
|
70
70
|
expect(results.warningCount).to.be.equal(0);
|
|
71
71
|
expect(results.ignoredCount).to.be.equal(1);
|
|
72
72
|
});
|
|
73
|
-
|
|
73
|
+
|
|
74
74
|
it('return similar results given xml filepath', async function() {
|
|
75
|
-
results = await validator.validateAsync(xmlPath, schematron);
|
|
75
|
+
results = await validator.validateAsync(xmlPath, schematron, { includeWarnings: true });
|
|
76
76
|
expect(results).to.be.an('object');
|
|
77
77
|
expect(results.errorCount).to.be.a('number');
|
|
78
78
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -87,9 +87,9 @@ describe('Validator should', function() {
|
|
|
87
87
|
expect(results.warningCount).to.be.equal(15);
|
|
88
88
|
expect(results.ignoredCount).to.be.equal(1);
|
|
89
89
|
});
|
|
90
|
-
|
|
90
|
+
|
|
91
91
|
it('return similar results given schematron filepath', async function() {
|
|
92
|
-
results = await validator.validateAsync(xml, schematronPath);
|
|
92
|
+
results = await validator.validateAsync(xml, schematronPath, { includeWarnings: true });
|
|
93
93
|
expect(results).to.be.an('object');
|
|
94
94
|
expect(results.errorCount).to.be.a('number');
|
|
95
95
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -104,9 +104,9 @@ describe('Validator should', function() {
|
|
|
104
104
|
expect(results.warningCount).to.be.equal(15);
|
|
105
105
|
expect(results.ignoredCount).to.be.equal(1);
|
|
106
106
|
});
|
|
107
|
-
|
|
107
|
+
|
|
108
108
|
it('return similar results given xml filepath and schematron filepath', async function() {
|
|
109
|
-
results = await validator.validateAsync(xmlPath, schematronPath);
|
|
109
|
+
results = await validator.validateAsync(xmlPath, schematronPath, { includeWarnings: true });
|
|
110
110
|
expect(results).to.be.an('object');
|
|
111
111
|
expect(results.errorCount).to.be.a('number');
|
|
112
112
|
expect(results.warningCount).to.be.a('number');
|
package/test/tests.js
CHANGED
|
@@ -2,19 +2,18 @@ const fs = require('fs');
|
|
|
2
2
|
const expect = require('chai').expect;
|
|
3
3
|
|
|
4
4
|
const validator = require('../validator');
|
|
5
|
-
|
|
5
|
+
const path = require('path');
|
|
6
6
|
// Which xml file to test
|
|
7
|
-
const xmlPath = './test
|
|
7
|
+
const xmlPath = path.resolve(__dirname, './test.xml');
|
|
8
8
|
// Which schematron to test against
|
|
9
|
-
const schematronPath = './test
|
|
10
|
-
|
|
9
|
+
const schematronPath = path.resolve(__dirname, './test.sch');
|
|
11
10
|
const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
|
|
12
11
|
const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
13
12
|
|
|
14
13
|
describe('Validator should', function() {
|
|
15
14
|
let results;
|
|
16
15
|
it('return results', function(done) {
|
|
17
|
-
results = validator.validate(xml, schematron);
|
|
16
|
+
results = validator.validate(xml, schematron, { includeWarnings: true });
|
|
18
17
|
expect(results).to.be.an('object');
|
|
19
18
|
done();
|
|
20
19
|
});
|
|
@@ -66,9 +65,9 @@ describe('Validator should', function() {
|
|
|
66
65
|
expect(results.ignoredCount).to.be.equal(1);
|
|
67
66
|
done();
|
|
68
67
|
});
|
|
69
|
-
|
|
68
|
+
|
|
70
69
|
it('return similar results without warnings', function(done) {
|
|
71
|
-
results = validator.validate(xml, schematron
|
|
70
|
+
results = validator.validate(xml, schematron);
|
|
72
71
|
expect(results).to.be.an('object');
|
|
73
72
|
expect(results.errorCount).to.be.a('number');
|
|
74
73
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -84,9 +83,9 @@ describe('Validator should', function() {
|
|
|
84
83
|
expect(results.ignoredCount).to.be.equal(1);
|
|
85
84
|
done();
|
|
86
85
|
});
|
|
87
|
-
|
|
86
|
+
|
|
88
87
|
it('return similar results given xml filepath', function(done) {
|
|
89
|
-
results = validator.validate(xmlPath, schematron);
|
|
88
|
+
results = validator.validate(xmlPath, schematron, { includeWarnings: true });
|
|
90
89
|
expect(results).to.be.an('object');
|
|
91
90
|
expect(results.errorCount).to.be.a('number');
|
|
92
91
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -102,9 +101,9 @@ describe('Validator should', function() {
|
|
|
102
101
|
expect(results.ignoredCount).to.be.equal(1);
|
|
103
102
|
done();
|
|
104
103
|
});
|
|
105
|
-
|
|
104
|
+
|
|
106
105
|
it('return similar results given schematron filepath', function(done) {
|
|
107
|
-
results = validator.validate(xml, schematronPath);
|
|
106
|
+
results = validator.validate(xml, schematronPath, { includeWarnings: true });
|
|
108
107
|
expect(results).to.be.an('object');
|
|
109
108
|
expect(results.errorCount).to.be.a('number');
|
|
110
109
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -120,9 +119,9 @@ describe('Validator should', function() {
|
|
|
120
119
|
expect(results.ignoredCount).to.be.equal(1);
|
|
121
120
|
done();
|
|
122
121
|
});
|
|
123
|
-
|
|
122
|
+
|
|
124
123
|
it('return similar results given xml filepath and schematron filepath', function(done) {
|
|
125
|
-
results = validator.validate(xmlPath, schematronPath);
|
|
124
|
+
results = validator.validate(xmlPath, schematronPath, { includeWarnings: true });
|
|
126
125
|
expect(results).to.be.an('object');
|
|
127
126
|
expect(results.errorCount).to.be.a('number');
|
|
128
127
|
expect(results.warningCount).to.be.a('number');
|
package/test/vitalSigns.test.js
CHANGED
|
@@ -12,8 +12,9 @@ const schematronPath = path.resolve(__dirname, './CDAR2_IG_CCDA_CLINNOTES_R1_DST
|
|
|
12
12
|
const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
|
|
13
13
|
const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
14
14
|
const options = {
|
|
15
|
-
resourceDir: path.resolve(__dirname, './')
|
|
16
|
-
|
|
15
|
+
resourceDir: path.resolve(__dirname, './'),
|
|
16
|
+
includeWarnings: true
|
|
17
|
+
};
|
|
17
18
|
|
|
18
19
|
describe('Validator should', function() {
|
|
19
20
|
let results;
|
|
@@ -57,9 +58,9 @@ describe('Validator should', function() {
|
|
|
57
58
|
it('return correct number of ignored', function() {
|
|
58
59
|
expect(results.ignoredCount).to.be.equal(0);
|
|
59
60
|
});
|
|
60
|
-
|
|
61
|
+
|
|
61
62
|
it('return similar results without warnings', async function() {
|
|
62
|
-
results = await validator.validateAsync(xml, schematron, {
|
|
63
|
+
results = await validator.validateAsync(xml, schematron, { resourceDir: options.resourceDir });
|
|
63
64
|
expect(results).to.be.an('object');
|
|
64
65
|
expect(results.errorCount).to.be.a('number');
|
|
65
66
|
expect(results.warningCount).to.be.a('number');
|
|
@@ -74,7 +75,7 @@ describe('Validator should', function() {
|
|
|
74
75
|
expect(results.warningCount).to.be.equal(0);
|
|
75
76
|
expect(results.ignoredCount).to.be.equal(0);
|
|
76
77
|
});
|
|
77
|
-
|
|
78
|
+
|
|
78
79
|
it('return similar results given xml filepath', async function() {
|
|
79
80
|
results = await validator.validateAsync(xmlPath, schematron, options);
|
|
80
81
|
expect(results).to.be.an('object');
|
|
@@ -91,7 +92,7 @@ describe('Validator should', function() {
|
|
|
91
92
|
expect(results.warningCount).to.be.equal(6);
|
|
92
93
|
expect(results.ignoredCount).to.be.equal(0);
|
|
93
94
|
});
|
|
94
|
-
|
|
95
|
+
|
|
95
96
|
it('return similar results given schematron filepath', async function() {
|
|
96
97
|
results = await validator.validateAsync(xml, schematronPath, options);
|
|
97
98
|
expect(results).to.be.an('object');
|
|
@@ -108,7 +109,7 @@ describe('Validator should', function() {
|
|
|
108
109
|
expect(results.warningCount).to.be.equal(6);
|
|
109
110
|
expect(results.ignoredCount).to.be.equal(0);
|
|
110
111
|
});
|
|
111
|
-
|
|
112
|
+
|
|
112
113
|
it('return similar results given xml filepath and schematron filepath', async function() {
|
|
113
114
|
results = await validator.validateAsync(xmlPath, schematronPath, options);
|
|
114
115
|
expect(results).to.be.an('object');
|
package/testAssertion.js
CHANGED
|
@@ -3,18 +3,18 @@
|
|
|
3
3
|
module.exports = testAssertion;
|
|
4
4
|
|
|
5
5
|
function testAssertion(test, selected, select, xmlDoc, resourceDir, xmlSnippetMaxLength) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const results = [];
|
|
7
|
+
|
|
8
8
|
for (let i = 0; i < selected.length; i++) {
|
|
9
9
|
try {
|
|
10
|
-
|
|
10
|
+
const result = select('boolean(' + test + ')', selected[i]);
|
|
11
11
|
let lineNumber = null;
|
|
12
12
|
let xmlSnippet = null;
|
|
13
13
|
if (selected[i].lineNumber) {
|
|
14
14
|
lineNumber = selected[i].lineNumber;
|
|
15
15
|
xmlSnippet = selected[i].toString();
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
const maxLength = (xmlSnippetMaxLength || 1e308);
|
|
18
18
|
if (xmlSnippet && xmlSnippet.length > maxLength) {
|
|
19
19
|
xmlSnippet = xmlSnippet.slice(0, maxLength) + '...';
|
|
20
20
|
}
|
|
@@ -24,7 +24,7 @@ function testAssertion(test, selected, select, xmlDoc, resourceDir, xmlSnippetMa
|
|
|
24
24
|
return { ignored: true, errorMessage: err.message };
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
for (let i = 0; i < results.length; i++) {
|
|
29
29
|
if (results[i].result !== true && results[i].result !== false) {
|
|
30
30
|
return { ignored: true, errorMessage: 'Test returned non-boolean result' };
|
|
@@ -33,8 +33,8 @@ function testAssertion(test, selected, select, xmlDoc, resourceDir, xmlSnippetMa
|
|
|
33
33
|
return results;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
function getXPath(node, path) {
|
|
37
|
-
|
|
36
|
+
function getXPath(node, path) {
|
|
37
|
+
const top = !path ? true : false;
|
|
38
38
|
path = path || [];
|
|
39
39
|
if (node.parentNode) {
|
|
40
40
|
path = getXPath(node.parentNode, path);
|
|
@@ -59,7 +59,8 @@ function getXPath(node, path) {
|
|
|
59
59
|
if (sibling.nodeType === 1 && sibling.nodeName === node.nodeName) {
|
|
60
60
|
count = 1;
|
|
61
61
|
sibling = null;
|
|
62
|
-
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
63
64
|
count = null;
|
|
64
65
|
sibling = sibling.previousSibling;
|
|
65
66
|
}
|
|
@@ -70,4 +71,4 @@ function getXPath(node, path) {
|
|
|
70
71
|
path.push(node.nodeName + ('[' + (count || 1) + ']'));
|
|
71
72
|
}
|
|
72
73
|
return top ? '/' + path.join('/') : path;
|
|
73
|
-
}
|
|
74
|
+
}
|
package/validator.js
CHANGED
|
@@ -10,7 +10,7 @@ module.exports = {
|
|
|
10
10
|
|
|
11
11
|
const fs = require('fs');
|
|
12
12
|
const xpath = require('xpath');
|
|
13
|
-
const
|
|
13
|
+
const Dom = require('@xmldom/xmldom').DOMParser;
|
|
14
14
|
// crypto is now built-in node
|
|
15
15
|
const crypto = require('crypto');
|
|
16
16
|
const _get = require('lodash.get');
|
|
@@ -26,7 +26,10 @@ function clearCache() {
|
|
|
26
26
|
parsedMap = Object.create({});
|
|
27
27
|
contextMap = Object.create({});
|
|
28
28
|
}
|
|
29
|
-
let namespaceMap
|
|
29
|
+
let namespaceMap;
|
|
30
|
+
let patternRuleMap;
|
|
31
|
+
let ruleAssertionMap;
|
|
32
|
+
let xpathSelect;
|
|
30
33
|
function validate(xml, schematron, options = {}) {
|
|
31
34
|
// If not valid xml, it might be a filepath
|
|
32
35
|
// Adding explicit check to make it clear
|
|
@@ -43,7 +46,7 @@ function validate(xml, schematron, options = {}) {
|
|
|
43
46
|
|
|
44
47
|
let schematronMap;
|
|
45
48
|
// Load xml doc
|
|
46
|
-
|
|
49
|
+
const xmlDoc = new Dom().parseFromString(xml);
|
|
47
50
|
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
48
51
|
if (options.parsedSchematronMap) {
|
|
49
52
|
schematronMap = options.parsedSchematronMap;
|
|
@@ -61,7 +64,7 @@ function validate(xml, schematron, options = {}) {
|
|
|
61
64
|
return;
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
|
-
|
|
67
|
+
const hash = crypto
|
|
65
68
|
.createHash('md5')
|
|
66
69
|
.update(schematron)
|
|
67
70
|
.digest('hex');
|
|
@@ -69,16 +72,15 @@ function validate(xml, schematron, options = {}) {
|
|
|
69
72
|
// If not in cache
|
|
70
73
|
if (!schematronMap) {
|
|
71
74
|
// Load schematron doc
|
|
72
|
-
|
|
75
|
+
const schematronDoc = new Dom().parseFromString(schematron);
|
|
73
76
|
|
|
74
77
|
// Parse schematron
|
|
75
|
-
schematronMap = parseSchematron(schematronDoc);
|
|
78
|
+
schematronMap = parseSchematron(schematronDoc, options);
|
|
76
79
|
|
|
77
80
|
// Cache parsed schematron
|
|
78
81
|
parsedMap[hash] = schematronMap;
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
|
-
|
|
82
84
|
// Extract data from parsed schematron object
|
|
83
85
|
namespaceMap = schematronMap.namespaceMap;
|
|
84
86
|
patternRuleMap = schematronMap.patternRuleMap;
|
|
@@ -88,10 +90,10 @@ function validate(xml, schematron, options = {}) {
|
|
|
88
90
|
// Avoid using 'select' as a variable name as it is overused
|
|
89
91
|
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
90
92
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
for (
|
|
93
|
+
const errors = [];
|
|
94
|
+
const warnings = [];
|
|
95
|
+
const ignored = [];
|
|
96
|
+
for (const patternId in patternRuleMap) {
|
|
95
97
|
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
96
98
|
continue;
|
|
97
99
|
}
|
|
@@ -101,10 +103,10 @@ function validate(xml, schematron, options = {}) {
|
|
|
101
103
|
const ruleObject = ruleAssertionMap[ruleId];
|
|
102
104
|
if (_get(ruleObject, 'abstract')) {
|
|
103
105
|
continue;
|
|
104
|
-
}
|
|
106
|
+
}
|
|
105
107
|
const context = _get(ruleObject, 'context');
|
|
106
108
|
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
107
|
-
|
|
109
|
+
const failedAssertions = checkRule(xmlDoc, context, assertionsAndExtensions, options);
|
|
108
110
|
for (let j = 0; j < failedAssertions.length; j++) {
|
|
109
111
|
const assertionObject = failedAssertions[j];
|
|
110
112
|
const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
|
|
@@ -113,7 +115,7 @@ function validate(xml, schematron, options = {}) {
|
|
|
113
115
|
const resultObject = results[k];
|
|
114
116
|
const { result, line, path, xml } = resultObject;
|
|
115
117
|
if (!result) {
|
|
116
|
-
|
|
118
|
+
const obj = {
|
|
117
119
|
type: type,
|
|
118
120
|
test: test,
|
|
119
121
|
simplifiedTest: simplifiedTest,
|
|
@@ -136,7 +138,7 @@ function validate(xml, schematron, options = {}) {
|
|
|
136
138
|
}
|
|
137
139
|
}
|
|
138
140
|
else if (results.ignored) {
|
|
139
|
-
|
|
141
|
+
const obj = {
|
|
140
142
|
errorMessage: errorMessage,
|
|
141
143
|
type: type,
|
|
142
144
|
test: test,
|
|
@@ -166,10 +168,9 @@ function validate(xml, schematron, options = {}) {
|
|
|
166
168
|
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
167
169
|
function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
168
170
|
// Context cache
|
|
169
|
-
const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
|
|
170
171
|
const resourceDir = options.resourceDir || './';
|
|
171
172
|
const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
|
|
172
|
-
|
|
173
|
+
const failedAssertions = [];
|
|
173
174
|
const context = options.contextOverride || originalContext;
|
|
174
175
|
// Determine the sections within context, load selected section from cache if possible
|
|
175
176
|
let selected = contextMap[context];
|
|
@@ -192,7 +193,7 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
|
192
193
|
if (assertionAndExtensionObject.type === 'assertion') {
|
|
193
194
|
let { level, test, id, description } = assertionAndExtensionObject;
|
|
194
195
|
// Extract values from external document and modify test if a document call is made
|
|
195
|
-
|
|
196
|
+
const originalTest = test;
|
|
196
197
|
let simplifiedTest = null;
|
|
197
198
|
try {
|
|
198
199
|
test = includeExternalDocument(test, resourceDir);
|
|
@@ -207,11 +208,11 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
|
207
208
|
results: { ignored: true, errorMessage: err.message }
|
|
208
209
|
});
|
|
209
210
|
continue;
|
|
210
|
-
}
|
|
211
|
+
}
|
|
211
212
|
if (originalTest !== test) {
|
|
212
213
|
simplifiedTest = test;
|
|
213
214
|
}
|
|
214
|
-
if (level === 'error' || includeWarnings) {
|
|
215
|
+
if (level === 'error' || _get(options, 'includeWarnings')) {
|
|
215
216
|
failedAssertions.push({
|
|
216
217
|
type: level,
|
|
217
218
|
assertionId: id,
|
|
@@ -227,13 +228,13 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
|
227
228
|
if (!extensionRule) {
|
|
228
229
|
continue;
|
|
229
230
|
}
|
|
230
|
-
const
|
|
231
|
+
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
232
|
+
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
231
233
|
if (!subAssertionsAndExtensions) {
|
|
232
234
|
continue;
|
|
233
235
|
}
|
|
234
|
-
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
235
236
|
const failedSubAssertions = checkRule(xmlDoc, context, subAssertionsAndExtensions, options);
|
|
236
|
-
failedAssertions.push(...failedSubAssertions);
|
|
237
|
+
failedAssertions.push(...failedSubAssertions);
|
|
237
238
|
}
|
|
238
239
|
}
|
|
239
240
|
return failedAssertions;
|
|
@@ -255,7 +256,7 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
255
256
|
|
|
256
257
|
let schematronMap;
|
|
257
258
|
// Load xml doc
|
|
258
|
-
|
|
259
|
+
const xmlDoc = new Dom().parseFromString(xml);
|
|
259
260
|
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
260
261
|
if (options.parsedSchematronMap) {
|
|
261
262
|
schematronMap = options.parsedSchematronMap;
|
|
@@ -273,7 +274,7 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
273
274
|
return;
|
|
274
275
|
}
|
|
275
276
|
}
|
|
276
|
-
|
|
277
|
+
const hash = crypto
|
|
277
278
|
.createHash('md5')
|
|
278
279
|
.update(schematron)
|
|
279
280
|
.digest('hex');
|
|
@@ -281,10 +282,10 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
281
282
|
// If not in cache
|
|
282
283
|
if (!schematronMap) {
|
|
283
284
|
// Load schematron doc
|
|
284
|
-
|
|
285
|
+
const schematronDoc = new Dom().parseFromString(schematron);
|
|
285
286
|
|
|
286
287
|
// Parse schematron
|
|
287
|
-
schematronMap = parseSchematron(schematronDoc);
|
|
288
|
+
schematronMap = parseSchematron(schematronDoc, options);
|
|
288
289
|
|
|
289
290
|
// Cache parsed schematron
|
|
290
291
|
parsedMap[hash] = schematronMap;
|
|
@@ -300,10 +301,10 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
300
301
|
// Avoid using 'select' as a variable name as it is overused
|
|
301
302
|
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
302
303
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
for (
|
|
304
|
+
const errors = [];
|
|
305
|
+
const warnings = [];
|
|
306
|
+
const ignored = [];
|
|
307
|
+
for (const patternId in patternRuleMap) {
|
|
307
308
|
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
308
309
|
continue;
|
|
309
310
|
}
|
|
@@ -313,10 +314,10 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
313
314
|
const ruleObject = ruleAssertionMap[ruleId];
|
|
314
315
|
if (_get(ruleObject, 'abstract')) {
|
|
315
316
|
continue;
|
|
316
|
-
}
|
|
317
|
+
}
|
|
317
318
|
const context = _get(ruleObject, 'context');
|
|
318
319
|
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
319
|
-
|
|
320
|
+
const failedAssertions = await checkRulePromise(xmlDoc, context, assertionsAndExtensions, options);
|
|
320
321
|
for (let j = 0; j < failedAssertions.length; j++) {
|
|
321
322
|
const assertionObject = failedAssertions[j];
|
|
322
323
|
const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
|
|
@@ -325,7 +326,7 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
325
326
|
const resultObject = results[k];
|
|
326
327
|
const { result, line, path, xml } = resultObject;
|
|
327
328
|
if (!result) {
|
|
328
|
-
|
|
329
|
+
const obj = {
|
|
329
330
|
type: type,
|
|
330
331
|
test: test,
|
|
331
332
|
simplifiedTest: simplifiedTest,
|
|
@@ -348,7 +349,7 @@ async function validateAsync(xml, schematron, options = {}) {
|
|
|
348
349
|
}
|
|
349
350
|
}
|
|
350
351
|
else if (results.ignored) {
|
|
351
|
-
|
|
352
|
+
const obj = {
|
|
352
353
|
errorMessage: errorMessage,
|
|
353
354
|
type: type,
|
|
354
355
|
test: test,
|
|
@@ -381,10 +382,9 @@ function checkRulePromise(xmlDoc, originalContext, assertionsAndExtensions, opti
|
|
|
381
382
|
const resultCapture = new Promise((resolve, reject) => {
|
|
382
383
|
try {
|
|
383
384
|
// Context cache
|
|
384
|
-
const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
|
|
385
385
|
const resourceDir = options.resourceDir || './';
|
|
386
386
|
const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
|
|
387
|
-
|
|
387
|
+
const failedAssertions = [];
|
|
388
388
|
const context = options.contextOverride || originalContext;
|
|
389
389
|
// Determine the sections within context, load selected section from cache if possible
|
|
390
390
|
let selected = contextMap[context];
|
|
@@ -407,7 +407,7 @@ function checkRulePromise(xmlDoc, originalContext, assertionsAndExtensions, opti
|
|
|
407
407
|
if (assertionAndExtensionObject.type === 'assertion') {
|
|
408
408
|
let { level, test, id, description } = assertionAndExtensionObject;
|
|
409
409
|
// Extract values from external document and modify test if a document call is made
|
|
410
|
-
|
|
410
|
+
const originalTest = test;
|
|
411
411
|
let simplifiedTest = null;
|
|
412
412
|
try {
|
|
413
413
|
test = includeExternalDocument(test, resourceDir);
|
|
@@ -422,11 +422,11 @@ function checkRulePromise(xmlDoc, originalContext, assertionsAndExtensions, opti
|
|
|
422
422
|
results: { ignored: true, errorMessage: err.message }
|
|
423
423
|
});
|
|
424
424
|
continue;
|
|
425
|
-
}
|
|
425
|
+
}
|
|
426
426
|
if (originalTest !== test) {
|
|
427
427
|
simplifiedTest = test;
|
|
428
428
|
}
|
|
429
|
-
if (level === 'error' || includeWarnings) {
|
|
429
|
+
if (level === 'error' || _get(options, 'includeWarnings')) {
|
|
430
430
|
failedAssertions.push({
|
|
431
431
|
type: level,
|
|
432
432
|
assertionId: id,
|
|
@@ -442,20 +442,20 @@ function checkRulePromise(xmlDoc, originalContext, assertionsAndExtensions, opti
|
|
|
442
442
|
if (!extensionRule) {
|
|
443
443
|
continue;
|
|
444
444
|
}
|
|
445
|
-
const
|
|
445
|
+
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
446
|
+
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
446
447
|
if (!subAssertionsAndExtensions) {
|
|
447
448
|
continue;
|
|
448
449
|
}
|
|
449
|
-
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
450
450
|
const failedSubAssertions = checkRule(xmlDoc, context, subAssertionsAndExtensions, options);
|
|
451
|
-
failedAssertions.push(...failedSubAssertions);
|
|
451
|
+
failedAssertions.push(...failedSubAssertions);
|
|
452
452
|
}
|
|
453
453
|
}
|
|
454
454
|
resolve (failedAssertions);
|
|
455
455
|
}
|
|
456
456
|
catch (promiseError) {
|
|
457
457
|
reject (new Error (`Promise Error: ${promiseError}`));
|
|
458
|
-
}
|
|
458
|
+
}
|
|
459
459
|
});
|
|
460
460
|
return resultCapture;
|
|
461
461
|
}
|