cda-schematron-validator 1.0.1 → 1.0.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/package.json +6 -1
- package/validator.js +49 -44
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cda-schematron-validator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.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": {
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
},
|
|
13
13
|
"author": "Eric Wadkins",
|
|
14
14
|
"contributors": [
|
|
15
|
+
{
|
|
16
|
+
"name" : "Eric Wadkins",
|
|
17
|
+
"url" : "http://www.ericwadkins.com/"
|
|
18
|
+
},
|
|
15
19
|
{
|
|
16
20
|
"name" : "Priyaranjan (Raj) Tokachichu",
|
|
17
21
|
"email" : "priyaranjan@priyaranjan.com",
|
|
@@ -31,6 +35,7 @@
|
|
|
31
35
|
"mocha": "~9.2.2"
|
|
32
36
|
},
|
|
33
37
|
"dependencies": {
|
|
38
|
+
"lodash.get": "~4.4.2",
|
|
34
39
|
"@xmldom/xmldom": "~0.7.2",
|
|
35
40
|
"xpath": "~0.0.32"
|
|
36
41
|
},
|
package/validator.js
CHANGED
|
@@ -12,7 +12,7 @@ const xpath = require('xpath');
|
|
|
12
12
|
const dom = require('@xmldom/xmldom').DOMParser;
|
|
13
13
|
// crypto is now built-in node
|
|
14
14
|
const crypto = require('crypto');
|
|
15
|
-
|
|
15
|
+
const _get = require('lodash.get');
|
|
16
16
|
const parseSchematron = require('./parseSchematron');
|
|
17
17
|
const testAssertion = require('./testAssertion');
|
|
18
18
|
const includeExternalDocument = require('./includeExternalDocument');
|
|
@@ -25,7 +25,7 @@ function clearCache() {
|
|
|
25
25
|
parsedMap = Object.create({});
|
|
26
26
|
contextMap = Object.create({});
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
let namespaceMap, patternRuleMap, ruleAssertionMap, xpathSelect;
|
|
29
29
|
function validate(xml, schematron, options = {}) {
|
|
30
30
|
// If not valid xml, it might be a filepath
|
|
31
31
|
// Adding explicit check to make it clear
|
|
@@ -79,45 +79,38 @@ function validate(xml, schematron, options = {}) {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// Extract data from parsed schematron object
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
namespaceMap = schematronMap.namespaceMap;
|
|
83
|
+
patternRuleMap = schematronMap.patternRuleMap;
|
|
84
|
+
ruleAssertionMap = schematronMap.ruleAssertionMap;
|
|
85
85
|
|
|
86
86
|
// Create selector object, initialized with namespaces
|
|
87
87
|
// Avoid using 'select' as a variable name as it is overused
|
|
88
|
-
|
|
88
|
+
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
89
89
|
|
|
90
90
|
let errors = [];
|
|
91
91
|
let warnings = [];
|
|
92
92
|
let ignored = [];
|
|
93
|
-
for (let
|
|
94
|
-
if (!patternRuleMap.hasOwnProperty(
|
|
93
|
+
for (let patternId in patternRuleMap) {
|
|
94
|
+
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
95
95
|
continue;
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
let rules = patternRuleMap[pattern];
|
|
97
|
+
const rules = patternRuleMap[patternId];
|
|
99
98
|
for (let i = 0; i < rules.length; i++) {
|
|
100
|
-
|
|
99
|
+
const ruleId = rules[i];
|
|
100
|
+
const ruleObject = ruleAssertionMap[ruleId];
|
|
101
|
+
if (_get(ruleObject, 'abstract')) {
|
|
101
102
|
continue;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
let
|
|
106
|
-
let
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
let assertionId = assertionResults[j].assertionId;
|
|
110
|
-
let test = assertionResults[j].test;
|
|
111
|
-
let simplifiedTest = assertionResults[j].simplifiedTest;
|
|
112
|
-
let description = assertionResults[j].description;
|
|
113
|
-
let errorMessage = assertionResults[j].errorMessage;
|
|
114
|
-
let results = assertionResults[j].results;
|
|
103
|
+
}
|
|
104
|
+
const context = _get(ruleObject, 'context');
|
|
105
|
+
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
106
|
+
let failedAssertions = checkRule(xmlDoc, context, assertionsAndExtensions, options);
|
|
107
|
+
for (let j = 0; j < failedAssertions.length; j++) {
|
|
108
|
+
const assertionObject = failedAssertions[j];
|
|
109
|
+
const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
|
|
115
110
|
if (!results.ignored) {
|
|
116
111
|
for (let k = 0; k < results.length; k++) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
let path = results[k].path;
|
|
120
|
-
let xmlSnippet = results[k].xml;
|
|
112
|
+
const resultObject = results[k];
|
|
113
|
+
const { result, line, path, xml } = resultObject;
|
|
121
114
|
if (!result) {
|
|
122
115
|
let obj = {
|
|
123
116
|
type: type,
|
|
@@ -130,7 +123,7 @@ function validate(xml, schematron, options = {}) {
|
|
|
130
123
|
ruleId: ruleId,
|
|
131
124
|
assertionId: assertionId,
|
|
132
125
|
context: context,
|
|
133
|
-
xml:
|
|
126
|
+
xml: xml
|
|
134
127
|
};
|
|
135
128
|
if (type === 'error') {
|
|
136
129
|
errors.push(obj);
|
|
@@ -162,7 +155,7 @@ function validate(xml, schematron, options = {}) {
|
|
|
162
155
|
return {
|
|
163
156
|
errorCount: errors.length,
|
|
164
157
|
warningCount: warnings.length,
|
|
165
|
-
ignoredCount: ignored
|
|
158
|
+
ignoredCount: ignored.length,
|
|
166
159
|
errors: errors,
|
|
167
160
|
warnings: warnings,
|
|
168
161
|
ignored: ignored
|
|
@@ -170,13 +163,13 @@ function validate(xml, schematron, options = {}) {
|
|
|
170
163
|
}
|
|
171
164
|
|
|
172
165
|
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
173
|
-
function checkRule(xmlDoc,
|
|
166
|
+
function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
174
167
|
// Context cache
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
168
|
+
const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
|
|
169
|
+
const resourceDir = options.resourceDir || './';
|
|
170
|
+
const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
|
|
178
171
|
let results = [];
|
|
179
|
-
|
|
172
|
+
const context = options.contextOverride || originalContext;
|
|
180
173
|
// Determine the sections within context, load selected section from cache if possible
|
|
181
174
|
let selected = contextMap[context];
|
|
182
175
|
let contextModified = context;
|
|
@@ -194,9 +187,9 @@ function checkRule(xmlDoc, xpathSelect, originalContext, assertionsAndExtensions
|
|
|
194
187
|
}
|
|
195
188
|
|
|
196
189
|
for (let i = 0; i < assertionsAndExtensions.length; i++) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
let test =
|
|
190
|
+
const assertionAndExtensionObject = assertionsAndExtensions[i];
|
|
191
|
+
if (assertionAndExtensionObject.type === 'assertion') {
|
|
192
|
+
let { level, test, id, description } = assertionAndExtensionObject;
|
|
200
193
|
|
|
201
194
|
// Extract values from external document and modify test if a document call is made
|
|
202
195
|
let originalTest = test;
|
|
@@ -211,18 +204,30 @@ function checkRule(xmlDoc, xpathSelect, originalContext, assertionsAndExtensions
|
|
|
211
204
|
if (originalTest !== test) {
|
|
212
205
|
simplifiedTest = test;
|
|
213
206
|
}
|
|
214
|
-
if (
|
|
207
|
+
if (level === 'error' || includeWarnings) {
|
|
215
208
|
results.push({
|
|
216
|
-
type:
|
|
217
|
-
assertionId:
|
|
209
|
+
type: level,
|
|
210
|
+
assertionId: id,
|
|
218
211
|
test: originalTest,
|
|
219
212
|
simplifiedTest: simplifiedTest,
|
|
220
|
-
description:
|
|
213
|
+
description: description,
|
|
221
214
|
results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
|
|
222
215
|
});
|
|
223
216
|
}
|
|
224
217
|
}
|
|
225
|
-
|
|
218
|
+
else {
|
|
219
|
+
const extensionRule = assertionAndExtensionObject.rule;
|
|
220
|
+
if (!extensionRule) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const subAssertionsAndExtensions = ruleAssertionMap[extensionRule] ? ruleAssertionMap[extensionRule].assertionsAndExtensions : null;
|
|
224
|
+
if (!subAssertionsAndExtensions) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
228
|
+
const newContext = newRuleObject.context;
|
|
229
|
+
results = results.concat(checkRule(xmlDoc, newContext, subAssertionsAndExtensions, options));
|
|
230
|
+
}
|
|
226
231
|
}
|
|
227
232
|
return results;
|
|
228
|
-
}
|
|
233
|
+
}
|