cda-schematron-validator 1.0.4 → 1.0.7
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/.github/workflows/codeql-analysis.yml +70 -0
- package/.jshintrc +96 -96
- package/.vscode/launch.json +37 -18
- package/README.md +140 -132
- package/includeExternalDocument.js +108 -108
- package/package.json +52 -52
- package/parseSchematron.js +121 -121
- package/test/test.sch +46 -46
- package/test/test.xml +79 -79
- package/test/tests.js +141 -141
- package/test/testsAsync.js +124 -0
- package/testAssertion.js +72 -72
- package/validator.js +455 -233
package/validator.js
CHANGED
|
@@ -1,233 +1,455 @@
|
|
|
1
|
-
// jshint node:true
|
|
2
|
-
// jshint shadow:true
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
validate: validate,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
let
|
|
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
|
-
if
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
let
|
|
92
|
-
let
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
let
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
1
|
+
// jshint node:true
|
|
2
|
+
// jshint shadow:true
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
validate: validate,
|
|
6
|
+
validateAsync: validateAsync,
|
|
7
|
+
clearCache: clearCache,
|
|
8
|
+
parseSchematron: require('./parseSchematron')
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const xpath = require('xpath');
|
|
13
|
+
const dom = require('@xmldom/xmldom').DOMParser;
|
|
14
|
+
// crypto is now built-in node
|
|
15
|
+
const crypto = require('crypto');
|
|
16
|
+
const _get = require('lodash.get');
|
|
17
|
+
const parseSchematron = require('./parseSchematron');
|
|
18
|
+
const testAssertion = require('./testAssertion');
|
|
19
|
+
const includeExternalDocument = require('./includeExternalDocument');
|
|
20
|
+
|
|
21
|
+
// Parsed object cache
|
|
22
|
+
let parsedMap = Object.create({});
|
|
23
|
+
let contextMap = Object.create({});
|
|
24
|
+
|
|
25
|
+
function clearCache() {
|
|
26
|
+
parsedMap = Object.create({});
|
|
27
|
+
contextMap = Object.create({});
|
|
28
|
+
}
|
|
29
|
+
let namespaceMap, patternRuleMap, ruleAssertionMap, xpathSelect;
|
|
30
|
+
function validate(xml, schematron, options = {}) {
|
|
31
|
+
// If not valid xml, it might be a filepath
|
|
32
|
+
// Adding explicit check to make it clear
|
|
33
|
+
if (xml.trim().indexOf('<') === -1) {
|
|
34
|
+
try {
|
|
35
|
+
xml = fs.readFileSync(xml, 'utf-8').toString();
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
// If no valid xml found, inform user, and return immediately
|
|
39
|
+
console.log('No valid xml could be found');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let schematronMap;
|
|
45
|
+
// Load xml doc
|
|
46
|
+
let xmlDoc = new dom().parseFromString(xml);
|
|
47
|
+
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
48
|
+
if (options.parsedSchematronMap) {
|
|
49
|
+
schematronMap = options.parsedSchematronMap;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// If not valid schematron (xml), it might be a filepath
|
|
53
|
+
// Adding explicit check to make it clear
|
|
54
|
+
if (schematron.trim().indexOf('<') === -1) {
|
|
55
|
+
try {
|
|
56
|
+
schematron = fs.readFileSync(schematron, 'utf-8').toString();
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
// If no valid schematron found, inform user, and return immediately
|
|
60
|
+
console.log('No valid schematron could be found');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
let hash = crypto
|
|
65
|
+
.createHash('md5')
|
|
66
|
+
.update(schematron)
|
|
67
|
+
.digest('hex');
|
|
68
|
+
schematronMap = parsedMap[hash];
|
|
69
|
+
// If not in cache
|
|
70
|
+
if (!schematronMap) {
|
|
71
|
+
// Load schematron doc
|
|
72
|
+
let schematronDoc = new dom().parseFromString(schematron);
|
|
73
|
+
|
|
74
|
+
// Parse schematron
|
|
75
|
+
schematronMap = parseSchematron(schematronDoc);
|
|
76
|
+
|
|
77
|
+
// Cache parsed schematron
|
|
78
|
+
parsedMap[hash] = schematronMap;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Extract data from parsed schematron object
|
|
83
|
+
namespaceMap = schematronMap.namespaceMap;
|
|
84
|
+
patternRuleMap = schematronMap.patternRuleMap;
|
|
85
|
+
ruleAssertionMap = schematronMap.ruleAssertionMap;
|
|
86
|
+
|
|
87
|
+
// Create selector object, initialized with namespaces
|
|
88
|
+
// Avoid using 'select' as a variable name as it is overused
|
|
89
|
+
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
90
|
+
|
|
91
|
+
let errors = [];
|
|
92
|
+
let warnings = [];
|
|
93
|
+
let ignored = [];
|
|
94
|
+
for (let patternId in patternRuleMap) {
|
|
95
|
+
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const rules = patternRuleMap[patternId];
|
|
99
|
+
for (let i = 0; i < rules.length; i++) {
|
|
100
|
+
const ruleId = rules[i];
|
|
101
|
+
const ruleObject = ruleAssertionMap[ruleId];
|
|
102
|
+
if (_get(ruleObject, 'abstract')) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const context = _get(ruleObject, 'context');
|
|
106
|
+
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
107
|
+
let failedAssertions = checkRule(xmlDoc, context, assertionsAndExtensions, options);
|
|
108
|
+
for (let j = 0; j < failedAssertions.length; j++) {
|
|
109
|
+
const assertionObject = failedAssertions[j];
|
|
110
|
+
const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
|
|
111
|
+
if (!results.ignored) {
|
|
112
|
+
for (let k = 0; k < results.length; k++) {
|
|
113
|
+
const resultObject = results[k];
|
|
114
|
+
const { result, line, path, xml } = resultObject;
|
|
115
|
+
if (!result) {
|
|
116
|
+
let obj = {
|
|
117
|
+
type: type,
|
|
118
|
+
test: test,
|
|
119
|
+
simplifiedTest: simplifiedTest,
|
|
120
|
+
description: description,
|
|
121
|
+
line: line,
|
|
122
|
+
path: path,
|
|
123
|
+
patternId: patternId,
|
|
124
|
+
ruleId: ruleId,
|
|
125
|
+
assertionId: assertionId,
|
|
126
|
+
context: context,
|
|
127
|
+
xml: xml
|
|
128
|
+
};
|
|
129
|
+
if (type === 'error') {
|
|
130
|
+
errors.push(obj);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
warnings.push(obj);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
let obj = {
|
|
140
|
+
errorMessage: errorMessage,
|
|
141
|
+
type: type,
|
|
142
|
+
test: test,
|
|
143
|
+
simplifiedTest: simplifiedTest,
|
|
144
|
+
description: description,
|
|
145
|
+
patternId: patternId,
|
|
146
|
+
ruleId: ruleId,
|
|
147
|
+
assertionId: assertionId,
|
|
148
|
+
context: context
|
|
149
|
+
};
|
|
150
|
+
ignored.push(obj);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
errorCount: errors.length,
|
|
158
|
+
warningCount: warnings.length,
|
|
159
|
+
ignoredCount: ignored.length,
|
|
160
|
+
errors: errors,
|
|
161
|
+
warnings: warnings,
|
|
162
|
+
ignored: ignored
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
167
|
+
function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
168
|
+
// Context cache
|
|
169
|
+
const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
|
|
170
|
+
const resourceDir = options.resourceDir || './';
|
|
171
|
+
const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
|
|
172
|
+
let results = [];
|
|
173
|
+
const context = options.contextOverride || originalContext;
|
|
174
|
+
// Determine the sections within context, load selected section from cache if possible
|
|
175
|
+
let selected = contextMap[context];
|
|
176
|
+
let contextModified = context;
|
|
177
|
+
if (!selected) {
|
|
178
|
+
if (context) {
|
|
179
|
+
if (context.indexOf('/')) {
|
|
180
|
+
contextModified = '//' + context;
|
|
181
|
+
}
|
|
182
|
+
selected = xpathSelect(contextModified, xmlDoc);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
selected = [xmlDoc];
|
|
186
|
+
}
|
|
187
|
+
contextMap[context] = selected;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (let i = 0; i < assertionsAndExtensions.length; i++) {
|
|
191
|
+
const assertionAndExtensionObject = assertionsAndExtensions[i];
|
|
192
|
+
if (assertionAndExtensionObject.type === 'assertion') {
|
|
193
|
+
let { level, test, id, description } = assertionAndExtensionObject;
|
|
194
|
+
|
|
195
|
+
// Extract values from external document and modify test if a document call is made
|
|
196
|
+
let originalTest = test;
|
|
197
|
+
try {
|
|
198
|
+
test = includeExternalDocument(test, resourceDir);
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
return { ignored: true, errorMessage: err.message };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let simplifiedTest = null;
|
|
205
|
+
if (originalTest !== test) {
|
|
206
|
+
simplifiedTest = test;
|
|
207
|
+
}
|
|
208
|
+
if (level === 'error' || includeWarnings) {
|
|
209
|
+
results.push({
|
|
210
|
+
type: level,
|
|
211
|
+
assertionId: id,
|
|
212
|
+
test: originalTest,
|
|
213
|
+
simplifiedTest: simplifiedTest,
|
|
214
|
+
description: description,
|
|
215
|
+
results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
const extensionRule = assertionAndExtensionObject.rule;
|
|
221
|
+
if (!extensionRule) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const subAssertionsAndExtensions = ruleAssertionMap[extensionRule] ? ruleAssertionMap[extensionRule].assertionsAndExtensions : null;
|
|
225
|
+
if (!subAssertionsAndExtensions) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
229
|
+
const newContext = newRuleObject.context;
|
|
230
|
+
results = results.concat(checkRule(xmlDoc, newContext, subAssertionsAndExtensions, options));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return results;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function validateAsync(xml, schematron, options = {}) {
|
|
237
|
+
// If not valid xml, it might be a filepath
|
|
238
|
+
// Adding explicit check to make it clear
|
|
239
|
+
if (xml.trim().indexOf('<') === -1) {
|
|
240
|
+
try {
|
|
241
|
+
xml = fs.readFileSync(xml, 'utf-8').toString();
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
// If no valid xml found, inform user, and return immediately
|
|
245
|
+
console.log('No valid xml could be found');
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
let schematronMap;
|
|
251
|
+
// Load xml doc
|
|
252
|
+
let xmlDoc = new dom().parseFromString(xml);
|
|
253
|
+
// Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
|
|
254
|
+
if (options.parsedSchematronMap) {
|
|
255
|
+
schematronMap = options.parsedSchematronMap;
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
// If not valid schematron (xml), it might be a filepath
|
|
259
|
+
// Adding explicit check to make it clear
|
|
260
|
+
if (schematron.trim().indexOf('<') === -1) {
|
|
261
|
+
try {
|
|
262
|
+
schematron = fs.readFileSync(schematron, 'utf-8').toString();
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
// If no valid schematron found, inform user, and return immediately
|
|
266
|
+
console.log('No valid schematron could be found');
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
let hash = crypto
|
|
271
|
+
.createHash('md5')
|
|
272
|
+
.update(schematron)
|
|
273
|
+
.digest('hex');
|
|
274
|
+
schematronMap = parsedMap[hash];
|
|
275
|
+
// If not in cache
|
|
276
|
+
if (!schematronMap) {
|
|
277
|
+
// Load schematron doc
|
|
278
|
+
let schematronDoc = new dom().parseFromString(schematron);
|
|
279
|
+
|
|
280
|
+
// Parse schematron
|
|
281
|
+
schematronMap = parseSchematron(schematronDoc);
|
|
282
|
+
|
|
283
|
+
// Cache parsed schematron
|
|
284
|
+
parsedMap[hash] = schematronMap;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Extract data from parsed schematron object
|
|
289
|
+
namespaceMap = schematronMap.namespaceMap;
|
|
290
|
+
patternRuleMap = schematronMap.patternRuleMap;
|
|
291
|
+
ruleAssertionMap = schematronMap.ruleAssertionMap;
|
|
292
|
+
|
|
293
|
+
// Create selector object, initialized with namespaces
|
|
294
|
+
// Avoid using 'select' as a variable name as it is overused
|
|
295
|
+
xpathSelect = xpath.useNamespaces(namespaceMap);
|
|
296
|
+
|
|
297
|
+
let errors = [];
|
|
298
|
+
let warnings = [];
|
|
299
|
+
let ignored = [];
|
|
300
|
+
for (let patternId in patternRuleMap) {
|
|
301
|
+
if (!patternRuleMap.hasOwnProperty(patternId)) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
const rules = patternRuleMap[patternId];
|
|
305
|
+
for (let i = 0; i < rules.length; i++) {
|
|
306
|
+
const ruleId = rules[i];
|
|
307
|
+
const ruleObject = ruleAssertionMap[ruleId];
|
|
308
|
+
if (_get(ruleObject, 'abstract')) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const context = _get(ruleObject, 'context');
|
|
312
|
+
const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
|
|
313
|
+
let failedAssertions;
|
|
314
|
+
try {
|
|
315
|
+
failedAssertions = await checkRulePromise(xmlDoc, context, assertionsAndExtensions, options);
|
|
316
|
+
}
|
|
317
|
+
catch (errorInRuleCheck) {
|
|
318
|
+
console.log(errorInRuleCheck);
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
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 (!results.ignored) {
|
|
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
|
+
let 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 {
|
|
352
|
+
let 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
|
+
}
|
|
367
|
+
}
|
|
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
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
|
|
380
|
+
function checkRulePromise(xmlDoc, originalContext, assertionsAndExtensions, options) {
|
|
381
|
+
// Context cache
|
|
382
|
+
const resultCapture = new Promise((resolve, reject) => {
|
|
383
|
+
try {
|
|
384
|
+
const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
|
|
385
|
+
const resourceDir = options.resourceDir || './';
|
|
386
|
+
const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
|
|
387
|
+
let results = [];
|
|
388
|
+
const context = options.contextOverride || originalContext;
|
|
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
|
+
|
|
410
|
+
// Extract values from external document and modify test if a document call is made
|
|
411
|
+
let originalTest = test;
|
|
412
|
+
try {
|
|
413
|
+
test = includeExternalDocument(test, resourceDir);
|
|
414
|
+
}
|
|
415
|
+
catch (err) {
|
|
416
|
+
return { ignored: true, errorMessage: err.message };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
let simplifiedTest = null;
|
|
420
|
+
if (originalTest !== test) {
|
|
421
|
+
simplifiedTest = test;
|
|
422
|
+
}
|
|
423
|
+
if (level === 'error' || includeWarnings) {
|
|
424
|
+
results.push({
|
|
425
|
+
type: level,
|
|
426
|
+
assertionId: id,
|
|
427
|
+
test: originalTest,
|
|
428
|
+
simplifiedTest: simplifiedTest,
|
|
429
|
+
description: description,
|
|
430
|
+
results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
const extensionRule = assertionAndExtensionObject.rule;
|
|
436
|
+
if (!extensionRule) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
const subAssertionsAndExtensions = ruleAssertionMap[extensionRule] ? ruleAssertionMap[extensionRule].assertionsAndExtensions : null;
|
|
440
|
+
if (!subAssertionsAndExtensions) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const newRuleObject = ruleAssertionMap[extensionRule];
|
|
444
|
+
const newContext = newRuleObject.context;
|
|
445
|
+
results = results.concat(checkRulePromise(xmlDoc, newContext, subAssertionsAndExtensions, options));
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
resolve (results);
|
|
449
|
+
}
|
|
450
|
+
catch (promiseError) {
|
|
451
|
+
reject (new Error (`Promise Error: ${promiseError}`));
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
return resultCapture;
|
|
455
|
+
}
|