cda-schematron-validator 1.1.4 → 1.1.5
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/README.md +4 -0
- package/package.json +1 -1
- package/validator.js +254 -380
package/README.md
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
Starting release history with 1.0.7
|
|
6
6
|
|
|
7
|
+
### 1.1.5
|
|
8
|
+
|
|
9
|
+
- Major methods have been divided into multiple small methods to avoid redundancy across sync and async methods, and reduce maintenance
|
|
10
|
+
|
|
7
11
|
### 1.1.4 (Stable Release)
|
|
8
12
|
|
|
9
13
|
- Major deviations from the source repository
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cda-schematron-validator",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
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/validator.js
CHANGED
|
@@ -26,436 +26,310 @@ function clearCache() {
|
|
|
26
26
|
parsedMap = Object.create({});
|
|
27
27
|
contextMap = Object.create({});
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
let patternRuleMap;
|
|
31
|
-
let ruleAssertionMap;
|
|
32
|
-
let xpathSelect;
|
|
33
|
-
function validate(xml, schematron, options = {}) {
|
|
29
|
+
function checkIfFileOrPath(givenString) {
|
|
34
30
|
// If not valid xml, it might be a filepath
|
|
35
31
|
// Adding explicit check to make it clear
|
|
36
|
-
if (
|
|
32
|
+
if (!givenString) {
|
|
33
|
+
return new Error('No data found in the xml or schematron');
|
|
34
|
+
}
|
|
35
|
+
if (givenString.trim().indexOf('<') === -1) {
|
|
37
36
|
try {
|
|
38
|
-
|
|
37
|
+
givenString = fs.readFileSync(givenString, 'utf-8').toString();
|
|
39
38
|
}
|
|
40
39
|
catch (err) {
|
|
41
40
|
// If no valid xml found, inform user, and return immediately
|
|
42
|
-
console.log('No valid xml could be found');
|
|
43
|
-
return;
|
|
41
|
+
console.log('No valid xml or schematron could be found');
|
|
42
|
+
return err;
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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];
|
|
53
57
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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);
|
|
60
89
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
console.log('No valid schematron could be found');
|
|
64
|
-
return;
|
|
90
|
+
else {
|
|
91
|
+
warnings.push(obj);
|
|
65
92
|
}
|
|
66
93
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
}
|
|
82
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;
|
|
83
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);
|
|
84
202
|
// Extract data from parsed schematron object
|
|
85
203
|
namespaceMap = schematronMap.namespaceMap;
|
|
86
204
|
patternRuleMap = schematronMap.patternRuleMap;
|
|
87
205
|
ruleAssertionMap = schematronMap.ruleAssertionMap;
|
|
88
|
-
|
|
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
|
+
}
|
|
89
214
|
// Create selector object, initialized with namespaces
|
|
90
215
|
// Avoid using 'select' as a variable name as it is overused
|
|
91
216
|
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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);
|
|
96
228
|
for (const patternId in patternRuleMap) {
|
|
97
229
|
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
98
230
|
continue;
|
|
99
231
|
}
|
|
232
|
+
assertionInfo.patternId = patternId;
|
|
100
233
|
const rules = patternRuleMap[patternId];
|
|
101
|
-
|
|
102
|
-
const ruleId = rules[i];
|
|
103
|
-
const ruleObject = ruleAssertionMap[ruleId];
|
|
104
|
-
if (_get(ruleObject, 'abstract')) {
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
const context = _get(ruleObject, 'context');
|
|
108
|
-
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
109
|
-
const failedAssertions = checkRule(xmlDoc, context, assertionsAndExtensions, options);
|
|
110
|
-
for (let j = 0; j < failedAssertions.length; j++) {
|
|
111
|
-
const assertionObject = failedAssertions[j];
|
|
112
|
-
const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
|
|
113
|
-
if (Array.isArray(results)) {
|
|
114
|
-
for (let k = 0; k < results.length; k++) {
|
|
115
|
-
const resultObject = results[k];
|
|
116
|
-
const { result, line, path, xml } = resultObject;
|
|
117
|
-
if (!result) {
|
|
118
|
-
const obj = {
|
|
119
|
-
type: type,
|
|
120
|
-
test: test,
|
|
121
|
-
simplifiedTest: simplifiedTest,
|
|
122
|
-
description: description,
|
|
123
|
-
line: line,
|
|
124
|
-
path: path,
|
|
125
|
-
patternId: patternId,
|
|
126
|
-
ruleId: ruleId,
|
|
127
|
-
assertionId: assertionId,
|
|
128
|
-
context: context,
|
|
129
|
-
xml: xml
|
|
130
|
-
};
|
|
131
|
-
if (type === 'error') {
|
|
132
|
-
errors.push(obj);
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
warnings.push(obj);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else if (results.ignored) {
|
|
141
|
-
const obj = {
|
|
142
|
-
errorMessage: errorMessage,
|
|
143
|
-
type: type,
|
|
144
|
-
test: test,
|
|
145
|
-
simplifiedTest: simplifiedTest,
|
|
146
|
-
description: description,
|
|
147
|
-
patternId: patternId,
|
|
148
|
-
ruleId: ruleId,
|
|
149
|
-
assertionId: assertionId,
|
|
150
|
-
context: context
|
|
151
|
-
};
|
|
152
|
-
ignored.push(obj);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
234
|
+
processRules(rules, assertionInfo, options);
|
|
156
235
|
}
|
|
157
|
-
|
|
158
|
-
return {
|
|
159
|
-
errorCount: errors.length,
|
|
160
|
-
warningCount: warnings.length,
|
|
161
|
-
ignoredCount: ignored.length,
|
|
162
|
-
errors: errors,
|
|
163
|
-
warnings: warnings,
|
|
164
|
-
ignored: ignored
|
|
165
|
-
};
|
|
236
|
+
return getReturnObject(assertionInfo);
|
|
166
237
|
}
|
|
167
238
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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) {
|
|
175
252
|
// Determine the sections within context, load selected section from cache if possible
|
|
176
|
-
let
|
|
253
|
+
let selectedXml = contextMap[context];
|
|
177
254
|
let contextModified = context;
|
|
178
|
-
if (!
|
|
255
|
+
if (!selectedXml) {
|
|
179
256
|
if (context) {
|
|
180
257
|
if (context.indexOf('/')) {
|
|
181
258
|
contextModified = '//' + context;
|
|
182
259
|
}
|
|
183
|
-
|
|
260
|
+
selectedXml = xpathSelect(contextModified, xmlDoc);
|
|
184
261
|
}
|
|
185
262
|
else {
|
|
186
|
-
|
|
263
|
+
selectedXml = [xmlDoc];
|
|
187
264
|
}
|
|
188
|
-
contextMap[context] =
|
|
265
|
+
contextMap[context] = selectedXml;
|
|
189
266
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
if (originalTest !== test) {
|
|
213
|
-
simplifiedTest = test;
|
|
214
|
-
}
|
|
215
|
-
if (level === 'error' || _get(options, 'includeWarnings')) {
|
|
216
|
-
failedAssertions.push({
|
|
217
|
-
type: level,
|
|
218
|
-
assertionId: id,
|
|
219
|
-
test: originalTest,
|
|
220
|
-
simplifiedTest: simplifiedTest,
|
|
221
|
-
description: description,
|
|
222
|
-
results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
const extensionRule = assertionAndExtensionObject.rule;
|
|
228
|
-
if (!extensionRule) {
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
232
|
-
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
233
|
-
if (!subAssertionsAndExtensions) {
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
const failedSubAssertions = checkRule(xmlDoc, context, subAssertionsAndExtensions, options);
|
|
237
|
-
failedAssertions.push(...failedSubAssertions);
|
|
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);
|
|
238
287
|
}
|
|
239
288
|
}
|
|
240
|
-
return
|
|
289
|
+
return failedSubAssertions;
|
|
241
290
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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;
|
|
249
301
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
let schematronMap;
|
|
258
|
-
// Load xml doc
|
|
259
|
-
const xmlDoc = new Dom().parseFromString(xml);
|
|
260
|
-
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
261
|
-
if (options.parsedSchematronMap) {
|
|
262
|
-
schematronMap = options.parsedSchematronMap;
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
// If not valid schematron (xml), it might be a filepath
|
|
266
|
-
// Adding explicit check to make it clear
|
|
267
|
-
if (schematron.trim().indexOf('<') === -1) {
|
|
268
|
-
try {
|
|
269
|
-
schematron = fs.readFileSync(schematron, 'utf-8').toString();
|
|
270
|
-
}
|
|
271
|
-
catch (err) {
|
|
272
|
-
// If no valid schematron found, inform user, and return immediately
|
|
273
|
-
console.log('No valid schematron could be found');
|
|
274
|
-
return;
|
|
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);
|
|
275
308
|
}
|
|
276
309
|
}
|
|
277
|
-
const hash = crypto
|
|
278
|
-
.createHash('md5')
|
|
279
|
-
.update(schematron)
|
|
280
|
-
.digest('hex');
|
|
281
|
-
schematronMap = parsedMap[hash];
|
|
282
|
-
// If not in cache
|
|
283
|
-
if (!schematronMap) {
|
|
284
|
-
// Load schematron doc
|
|
285
|
-
const schematronDoc = new Dom().parseFromString(schematron);
|
|
286
|
-
|
|
287
|
-
// Parse schematron
|
|
288
|
-
schematronMap = parseSchematron(schematronDoc, options);
|
|
289
|
-
|
|
290
|
-
// Cache parsed schematron
|
|
291
|
-
parsedMap[hash] = schematronMap;
|
|
292
|
-
}
|
|
293
310
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
ruleAssertionMap = schematronMap.ruleAssertionMap;
|
|
299
|
-
|
|
300
|
-
// Create selector object, initialized with namespaces
|
|
301
|
-
// Avoid using 'select' as a variable name as it is overused
|
|
302
|
-
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
303
|
-
|
|
304
|
-
const errors = [];
|
|
305
|
-
const warnings = [];
|
|
306
|
-
const ignored = [];
|
|
307
|
-
for (const patternId in patternRuleMap) {
|
|
308
|
-
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
309
|
-
continue;
|
|
310
|
-
}
|
|
311
|
-
const rules = patternRuleMap[patternId];
|
|
312
|
-
for (let i = 0; i < rules.length; i++) {
|
|
313
|
-
const ruleId = rules[i];
|
|
314
|
-
const ruleObject = ruleAssertionMap[ruleId];
|
|
315
|
-
if (_get(ruleObject, 'abstract')) {
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
const context = _get(ruleObject, 'context');
|
|
319
|
-
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
320
|
-
const failedAssertions = await checkRulePromise(xmlDoc, context, assertionsAndExtensions, options);
|
|
321
|
-
for (let j = 0; j < failedAssertions.length; j++) {
|
|
322
|
-
const assertionObject = failedAssertions[j];
|
|
323
|
-
const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
|
|
324
|
-
if (Array.isArray(results)) {
|
|
325
|
-
for (let k = 0; k < results.length; k++) {
|
|
326
|
-
const resultObject = results[k];
|
|
327
|
-
const { result, line, path, xml } = resultObject;
|
|
328
|
-
if (!result) {
|
|
329
|
-
const obj = {
|
|
330
|
-
type: type,
|
|
331
|
-
test: test,
|
|
332
|
-
simplifiedTest: simplifiedTest,
|
|
333
|
-
description: description,
|
|
334
|
-
line: line,
|
|
335
|
-
path: path,
|
|
336
|
-
patternId: patternId,
|
|
337
|
-
ruleId: ruleId,
|
|
338
|
-
assertionId: assertionId,
|
|
339
|
-
context: context,
|
|
340
|
-
xml: xml
|
|
341
|
-
};
|
|
342
|
-
if (type === 'error') {
|
|
343
|
-
errors.push(obj);
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
warnings.push(obj);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
else if (results.ignored) {
|
|
352
|
-
const obj = {
|
|
353
|
-
errorMessage: errorMessage,
|
|
354
|
-
type: type,
|
|
355
|
-
test: test,
|
|
356
|
-
simplifiedTest: simplifiedTest,
|
|
357
|
-
description: description,
|
|
358
|
-
patternId: patternId,
|
|
359
|
-
ruleId: ruleId,
|
|
360
|
-
assertionId: assertionId,
|
|
361
|
-
context: context
|
|
362
|
-
};
|
|
363
|
-
ignored.push(obj);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
const assertionObject = getAssertionObject(level, id, originalTest, simplifiedTest, description);
|
|
313
|
+
assertionObject.results = { ignored: true, errorMessage: err.message };
|
|
314
|
+
failedAssertions.push(assertionObject);
|
|
367
315
|
}
|
|
368
|
-
|
|
369
|
-
return {
|
|
370
|
-
errorCount: errors.length,
|
|
371
|
-
warningCount: warnings.length,
|
|
372
|
-
ignoredCount: ignored.length,
|
|
373
|
-
errors: errors,
|
|
374
|
-
warnings: warnings,
|
|
375
|
-
ignored: ignored
|
|
376
|
-
};
|
|
316
|
+
return failedAssertions;
|
|
377
317
|
}
|
|
378
|
-
|
|
379
318
|
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
380
|
-
function
|
|
319
|
+
function checkRule(context, assertionsAndExtensions, options) {
|
|
381
320
|
// Context cache
|
|
382
|
-
const
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
// Determine the sections within context, load selected section from cache if possible
|
|
390
|
-
let selected = contextMap[context];
|
|
391
|
-
let contextModified = context;
|
|
392
|
-
if (!selected) {
|
|
393
|
-
if (context) {
|
|
394
|
-
if (context.indexOf('/')) {
|
|
395
|
-
contextModified = '//' + context;
|
|
396
|
-
}
|
|
397
|
-
selected = xpathSelect(contextModified, xmlDoc);
|
|
398
|
-
}
|
|
399
|
-
else {
|
|
400
|
-
selected = [xmlDoc];
|
|
401
|
-
}
|
|
402
|
-
contextMap[context] = selected;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
for (let i = 0; i < assertionsAndExtensions.length; i++) {
|
|
406
|
-
const assertionAndExtensionObject = assertionsAndExtensions[i];
|
|
407
|
-
if (assertionAndExtensionObject.type === 'assertion') {
|
|
408
|
-
let { level, test, id, description } = assertionAndExtensionObject;
|
|
409
|
-
// Extract values from external document and modify test if a document call is made
|
|
410
|
-
const originalTest = test;
|
|
411
|
-
let simplifiedTest = null;
|
|
412
|
-
try {
|
|
413
|
-
test = includeExternalDocument(test, resourceDir);
|
|
414
|
-
}
|
|
415
|
-
catch (err) {
|
|
416
|
-
failedAssertions.push({
|
|
417
|
-
type: level,
|
|
418
|
-
assertionId: id,
|
|
419
|
-
test: originalTest,
|
|
420
|
-
simplifiedTest: simplifiedTest,
|
|
421
|
-
description: description,
|
|
422
|
-
results: { ignored: true, errorMessage: err.message }
|
|
423
|
-
});
|
|
424
|
-
continue;
|
|
425
|
-
}
|
|
426
|
-
if (originalTest !== test) {
|
|
427
|
-
simplifiedTest = test;
|
|
428
|
-
}
|
|
429
|
-
if (level === 'error' || _get(options, 'includeWarnings')) {
|
|
430
|
-
failedAssertions.push({
|
|
431
|
-
type: level,
|
|
432
|
-
assertionId: id,
|
|
433
|
-
test: originalTest,
|
|
434
|
-
simplifiedTest: simplifiedTest,
|
|
435
|
-
description: description,
|
|
436
|
-
results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
else {
|
|
441
|
-
const extensionRule = assertionAndExtensionObject.rule;
|
|
442
|
-
if (!extensionRule) {
|
|
443
|
-
continue;
|
|
444
|
-
}
|
|
445
|
-
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
446
|
-
const subAssertionsAndExtensions = newRuleObject ? newRuleObject.assertionsAndExtensions : null;
|
|
447
|
-
if (!subAssertionsAndExtensions) {
|
|
448
|
-
continue;
|
|
449
|
-
}
|
|
450
|
-
const failedSubAssertions = checkRule(xmlDoc, context, subAssertionsAndExtensions, options);
|
|
451
|
-
failedAssertions.push(...failedSubAssertions);
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
resolve (failedAssertions);
|
|
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);
|
|
455
328
|
}
|
|
456
|
-
|
|
457
|
-
|
|
329
|
+
else {
|
|
330
|
+
const failedSubAssertions = processExtension(assertionAndExtensionObject, context, options);
|
|
331
|
+
failedAssertions.push(...failedSubAssertions);
|
|
458
332
|
}
|
|
459
|
-
}
|
|
460
|
-
return
|
|
333
|
+
}
|
|
334
|
+
return failedAssertions;
|
|
461
335
|
}
|