cda-schematron-validator 1.0.3 → 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 +2 -1
- package/validator.js +32 -38
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": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"mocha": "~9.2.2"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
+
"lodash.get": "~4.4.2",
|
|
38
39
|
"@xmldom/xmldom": "~0.7.2",
|
|
39
40
|
"xpath": "~0.0.32"
|
|
40
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');
|
|
@@ -90,34 +90,27 @@ function validate(xml, schematron, options = {}) {
|
|
|
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
|
|
@@ -172,11 +165,11 @@ function validate(xml, schematron, options = {}) {
|
|
|
172
165
|
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
173
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, originalContext, assertionsAndExtensions, options) {
|
|
|
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,19 +204,19 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
|
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 {
|
|
226
|
-
const extensionRule =
|
|
219
|
+
const extensionRule = assertionAndExtensionObject.rule;
|
|
227
220
|
if (!extensionRule) {
|
|
228
221
|
continue;
|
|
229
222
|
}
|
|
@@ -231,7 +224,8 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
|
231
224
|
if (!subAssertionsAndExtensions) {
|
|
232
225
|
continue;
|
|
233
226
|
}
|
|
234
|
-
const
|
|
227
|
+
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
228
|
+
const newContext = newRuleObject.context;
|
|
235
229
|
results = results.concat(checkRule(xmlDoc, newContext, subAssertionsAndExtensions, options));
|
|
236
230
|
}
|
|
237
231
|
}
|