cda-schematron-validator 1.0.6 → 1.0.9

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.
@@ -0,0 +1,70 @@
1
+ # For most projects, this workflow file will not need changing; you simply need
2
+ # to commit it to your repository.
3
+ #
4
+ # You may wish to alter this file to override the set of languages analyzed,
5
+ # or to provide custom queries or build logic.
6
+ #
7
+ # ******** NOTE ********
8
+ # We have attempted to detect the languages in your repository. Please check
9
+ # the `language` matrix defined below to confirm you have the correct set of
10
+ # supported CodeQL languages.
11
+ #
12
+ name: "CodeQL"
13
+
14
+ on:
15
+ push:
16
+ branches: [ main ]
17
+ pull_request:
18
+ # The branches below must be a subset of the branches above
19
+ branches: [ main ]
20
+ schedule:
21
+ - cron: '34 18 * * 4'
22
+
23
+ jobs:
24
+ analyze:
25
+ name: Analyze
26
+ runs-on: ubuntu-latest
27
+ permissions:
28
+ actions: read
29
+ contents: read
30
+ security-events: write
31
+
32
+ strategy:
33
+ fail-fast: false
34
+ matrix:
35
+ language: [ 'javascript' ]
36
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
+ # Learn more about CodeQL language support at https://git.io/codeql-language-support
38
+
39
+ steps:
40
+ - name: Checkout repository
41
+ uses: actions/checkout@v3
42
+
43
+ # Initializes the CodeQL tools for scanning.
44
+ - name: Initialize CodeQL
45
+ uses: github/codeql-action/init@v2
46
+ with:
47
+ languages: ${{ matrix.language }}
48
+ # If you wish to specify custom queries, you can do so here or in a config file.
49
+ # By default, queries listed here will override any specified in a config file.
50
+ # Prefix the list here with "+" to use these queries and those in the config file.
51
+ # queries: ./path/to/local/query, your-org/your-repo/queries@main
52
+
53
+ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54
+ # If this step fails, then you should remove it and run the build manually (see below)
55
+ - name: Autobuild
56
+ uses: github/codeql-action/autobuild@v2
57
+
58
+ # ℹ️ Command-line programs to run using the OS shell.
59
+ # 📚 https://git.io/JvXDl
60
+
61
+ # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62
+ # and modify them (or add more) to build your code if your project
63
+ # uses a compiled language
64
+
65
+ #- run: |
66
+ # make bootstrap
67
+ # make release
68
+
69
+ - name: Perform CodeQL Analysis
70
+ uses: github/codeql-action/analyze@v2
package/README.md CHANGED
@@ -1,4 +1,17 @@
1
- # cda-schematron-validator
1
+ # [cda-schematron-validator](https://github.com/priyaranjan-tokachichu/cda-schematron-validator)
2
+ ## Release History
3
+ Starting release history with 1.0.7
4
+ ### 1.0.9
5
+ - Fixed the bug introduced in 1.0.8 trying to adjust to different data types of the return value
6
+ - Changed variable names to improve understanding of the process
7
+ ### 1.0.8
8
+ - Bug fix identifying the ignored errors
9
+ ### 1.0.7
10
+ - Added async validate function `validateAsync`
11
+ - Added `testsAsync` to test `validateAsync`
12
+ - Plan to use common functions between sync and async methods in the future release
13
+
14
+ ### 1.0.0 to 1.0.6
2
15
  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.
3
16
 
4
17
  This is just a cosmetic change with updated node modules, a couple of minor bugs observed, and esversion 6 changes.
@@ -32,7 +45,7 @@ Mocha VS Code configuratin added to directly execute the test file in `test/test
32
45
 
33
46
 
34
47
  # Original Documentation of the cda-schematron
35
- # cda-schematron
48
+ ## [cda-schematron](https://github.com/ewadkins/cda-schematron)
36
49
 
37
50
  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.
38
51
 
package/SECURITY.md ADDED
@@ -0,0 +1,21 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ All releases within a major version will be supported with security updates. The first release from 1.x.x version is 1.0.7.
6
+
7
+ | Version | Supported |
8
+ | ------- | ------------------ |
9
+ | 1.0.7 | :white_check_mark: |
10
+ | 1.0.6 | :x: |
11
+ | 1.0.5 | :x: |
12
+ | 1.0.4 | :x: |
13
+ | 1.0.3 | :x: |
14
+ | 1.0.2 | :x: |
15
+ | 1.0.1 | :x: |
16
+ | 1.0.0 | :x: |
17
+
18
+ ## Reporting a Vulnerability
19
+
20
+ Please, open an issue with the vulnerability discovered. If possible open a branch from main,
21
+ add a fix, and open a PR. I will review the security vulnerability and merge the PR if it is accepted.
package/debug.log ADDED
@@ -0,0 +1 @@
1
+ [0404/083636.694:ERROR:registration_protocol_win.cc(102)] CreateFile: The system cannot find the file specified. (0x2)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cda-schematron-validator",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
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": {
@@ -0,0 +1,124 @@
1
+ const fs = require('fs');
2
+ const expect = require('chai').expect;
3
+
4
+ const validator = require('../validator');
5
+
6
+ // Which xml file to test
7
+ const xmlPath = './test/test.xml';
8
+ // Which schematron to test against
9
+ const schematronPath = './test/test.sch';
10
+
11
+ const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
12
+ const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
13
+
14
+ describe('Validator should', function() {
15
+ let results;
16
+ it('return results', async function() {
17
+ results = await validator.validateAsync(xml, schematron);
18
+ expect(results).to.be.an('object');
19
+ });
20
+ it('return errorCount', function() {
21
+ expect(results.errorCount).to.be.a('number');
22
+ });
23
+ it('return warningCount', function() {
24
+ expect(results.warningCount).to.be.a('number');
25
+ });
26
+ it('return ignoredCount', function() {
27
+ expect(results.ignoredCount).to.be.a('number');
28
+ });
29
+ it('return errors array', function() {
30
+ expect(results.errors).to.be.a('array');
31
+ });
32
+ it('return warnings array', function() {
33
+ expect(results.warnings).to.be.a('array');
34
+ });
35
+ it('return ignored array', function() {
36
+ expect(results.ignored).to.be.a('array');
37
+ });
38
+ it('return matching errorCount', function() {
39
+ expect(results.errorCount).to.be.equal(results.errors.length);
40
+ });
41
+ it('return matching warningCount', function() {
42
+ expect(results.warningCount).to.be.equal(results.warnings.length);
43
+ });
44
+ it('return matching ignoredCount', function() {
45
+ expect(results.ignoredCount).to.be.equal(results.ignored.length);
46
+ });
47
+ it('return correct number of errors', function() {
48
+ expect(results.errorCount).to.be.equal(16);
49
+ });
50
+ it('return correct number of warnings', function() {
51
+ expect(results.warningCount).to.be.equal(15);
52
+ });
53
+ it('return correct number of ignored', function() {
54
+ expect(results.ignoredCount).to.be.equal(1);
55
+ });
56
+
57
+ it('return similar results without warnings', async function() {
58
+ results = await validator.validateAsync(xml, schematron, { includeWarnings: false });
59
+ expect(results).to.be.an('object');
60
+ expect(results.errorCount).to.be.a('number');
61
+ expect(results.warningCount).to.be.a('number');
62
+ expect(results.ignoredCount).to.be.a('number');
63
+ expect(results.errors).to.be.a('array');
64
+ expect(results.warnings).to.be.a('array');
65
+ expect(results.ignored).to.be.a('array');
66
+ expect(results.errorCount).to.be.equal(results.errors.length);
67
+ expect(results.warningCount).to.be.equal(results.warnings.length);
68
+ expect(results.ignoredCount).to.be.equal(results.ignored.length);
69
+ expect(results.errorCount).to.be.equal(16);
70
+ expect(results.warningCount).to.be.equal(0);
71
+ expect(results.ignoredCount).to.be.equal(1);
72
+ });
73
+
74
+ it('return similar results given xml filepath', async function() {
75
+ results = await validator.validateAsync(xmlPath, schematron);
76
+ expect(results).to.be.an('object');
77
+ expect(results.errorCount).to.be.a('number');
78
+ expect(results.warningCount).to.be.a('number');
79
+ expect(results.ignoredCount).to.be.a('number');
80
+ expect(results.errors).to.be.a('array');
81
+ expect(results.warnings).to.be.a('array');
82
+ expect(results.ignored).to.be.a('array');
83
+ expect(results.errorCount).to.be.equal(results.errors.length);
84
+ expect(results.warningCount).to.be.equal(results.warnings.length);
85
+ expect(results.ignoredCount).to.be.equal(results.ignored.length);
86
+ expect(results.errorCount).to.be.equal(16);
87
+ expect(results.warningCount).to.be.equal(15);
88
+ expect(results.ignoredCount).to.be.equal(1);
89
+ });
90
+
91
+ it('return similar results given schematron filepath', async function() {
92
+ results = await validator.validateAsync(xml, schematronPath);
93
+ expect(results).to.be.an('object');
94
+ expect(results.errorCount).to.be.a('number');
95
+ expect(results.warningCount).to.be.a('number');
96
+ expect(results.ignoredCount).to.be.a('number');
97
+ expect(results.errors).to.be.a('array');
98
+ expect(results.warnings).to.be.a('array');
99
+ expect(results.ignored).to.be.a('array');
100
+ expect(results.errorCount).to.be.equal(results.errors.length);
101
+ expect(results.warningCount).to.be.equal(results.warnings.length);
102
+ expect(results.ignoredCount).to.be.equal(results.ignored.length);
103
+ expect(results.errorCount).to.be.equal(16);
104
+ expect(results.warningCount).to.be.equal(15);
105
+ expect(results.ignoredCount).to.be.equal(1);
106
+ });
107
+
108
+ it('return similar results given xml filepath and schematron filepath', async function() {
109
+ results = await validator.validateAsync(xmlPath, schematronPath);
110
+ expect(results).to.be.an('object');
111
+ expect(results.errorCount).to.be.a('number');
112
+ expect(results.warningCount).to.be.a('number');
113
+ expect(results.ignoredCount).to.be.a('number');
114
+ expect(results.errors).to.be.a('array');
115
+ expect(results.warnings).to.be.a('array');
116
+ expect(results.ignored).to.be.a('array');
117
+ expect(results.errorCount).to.be.equal(results.errors.length);
118
+ expect(results.warningCount).to.be.equal(results.warnings.length);
119
+ expect(results.ignoredCount).to.be.equal(results.ignored.length);
120
+ expect(results.errorCount).to.be.equal(16);
121
+ expect(results.warningCount).to.be.equal(15);
122
+ expect(results.ignoredCount).to.be.equal(1);
123
+ });
124
+ });
package/validator.js CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  module.exports = {
5
5
  validate: validate,
6
+ validateAsync: validateAsync,
6
7
  clearCache: clearCache,
7
8
  parseSchematron: require('./parseSchematron')
8
9
  };
@@ -107,7 +108,7 @@ function validate(xml, schematron, options = {}) {
107
108
  for (let j = 0; j < failedAssertions.length; j++) {
108
109
  const assertionObject = failedAssertions[j];
109
110
  const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
110
- if (!results.ignored) {
111
+ if (Array.isArray(results)) {
111
112
  for (let k = 0; k < results.length; k++) {
112
113
  const resultObject = results[k];
113
114
  const { result, line, path, xml } = resultObject;
@@ -134,7 +135,7 @@ function validate(xml, schematron, options = {}) {
134
135
  }
135
136
  }
136
137
  }
137
- else {
138
+ else if (results.ignored) {
138
139
  let obj = {
139
140
  errorMessage: errorMessage,
140
141
  type: type,
@@ -168,7 +169,7 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
168
169
  const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
169
170
  const resourceDir = options.resourceDir || './';
170
171
  const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
171
- let results = [];
172
+ let failedAssertions = [];
172
173
  const context = options.contextOverride || originalContext;
173
174
  // Determine the sections within context, load selected section from cache if possible
174
175
  let selected = contextMap[context];
@@ -193,19 +194,26 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
193
194
 
194
195
  // Extract values from external document and modify test if a document call is made
195
196
  let originalTest = test;
197
+ let simplifiedTest = null;
196
198
  try {
197
199
  test = includeExternalDocument(test, resourceDir);
198
200
  }
199
201
  catch (err) {
200
- return { ignored: true, errorMessage: err.message };
201
- }
202
-
203
- let simplifiedTest = null;
202
+ failedAssertions.push({
203
+ type: level,
204
+ assertionId: id,
205
+ test: originalTest,
206
+ simplifiedTest: simplifiedTest,
207
+ description: description,
208
+ results: { ignored: true, errorMessage: err.message }
209
+ });
210
+ continue;
211
+ }
204
212
  if (originalTest !== test) {
205
213
  simplifiedTest = test;
206
214
  }
207
215
  if (level === 'error' || includeWarnings) {
208
- results.push({
216
+ failedAssertions.push({
209
217
  type: level,
210
218
  assertionId: id,
211
219
  test: originalTest,
@@ -226,8 +234,238 @@ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
226
234
  }
227
235
  const newRuleObject = ruleAssertionMap[extensionRule];
228
236
  const newContext = newRuleObject.context;
229
- results = results.concat(checkRule(xmlDoc, newContext, subAssertionsAndExtensions, options));
237
+ const failedSubAssertions = checkRule(xmlDoc, newContext, subAssertionsAndExtensions, options);
238
+ failedAssertions.push(...failedSubAssertions);
239
+ }
240
+ }
241
+ return failedAssertions;
242
+ }
243
+
244
+ async function validateAsync(xml, schematron, options = {}) {
245
+ // If not valid xml, it might be a filepath
246
+ // Adding explicit check to make it clear
247
+ if (xml.trim().indexOf('<') === -1) {
248
+ try {
249
+ xml = fs.readFileSync(xml, 'utf-8').toString();
250
+ }
251
+ catch (err) {
252
+ // If no valid xml found, inform user, and return immediately
253
+ console.log('No valid xml could be found');
254
+ return;
255
+ }
256
+ }
257
+
258
+ let schematronMap;
259
+ // Load xml doc
260
+ let xmlDoc = new dom().parseFromString(xml);
261
+ // Allowing users to send a parsed schematron map if testing multiple xml files with the same schematron
262
+ if (options.parsedSchematronMap) {
263
+ schematronMap = options.parsedSchematronMap;
264
+ }
265
+ else {
266
+ // If not valid schematron (xml), it might be a filepath
267
+ // Adding explicit check to make it clear
268
+ if (schematron.trim().indexOf('<') === -1) {
269
+ try {
270
+ schematron = fs.readFileSync(schematron, 'utf-8').toString();
271
+ }
272
+ catch (err) {
273
+ // If no valid schematron found, inform user, and return immediately
274
+ console.log('No valid schematron could be found');
275
+ return;
276
+ }
277
+ }
278
+ let hash = crypto
279
+ .createHash('md5')
280
+ .update(schematron)
281
+ .digest('hex');
282
+ schematronMap = parsedMap[hash];
283
+ // If not in cache
284
+ if (!schematronMap) {
285
+ // Load schematron doc
286
+ let schematronDoc = new dom().parseFromString(schematron);
287
+
288
+ // Parse schematron
289
+ schematronMap = parseSchematron(schematronDoc);
290
+
291
+ // Cache parsed schematron
292
+ parsedMap[hash] = schematronMap;
230
293
  }
231
294
  }
232
- return results;
295
+
296
+ // Extract data from parsed schematron object
297
+ namespaceMap = schematronMap.namespaceMap;
298
+ patternRuleMap = schematronMap.patternRuleMap;
299
+ ruleAssertionMap = schematronMap.ruleAssertionMap;
300
+
301
+ // Create selector object, initialized with namespaces
302
+ // Avoid using 'select' as a variable name as it is overused
303
+ xpathSelect = xpath.useNamespaces(namespaceMap);
304
+
305
+ let errors = [];
306
+ let warnings = [];
307
+ let ignored = [];
308
+ for (let patternId in patternRuleMap) {
309
+ if (!patternRuleMap.hasOwnProperty(patternId)) {
310
+ continue;
311
+ }
312
+ const rules = patternRuleMap[patternId];
313
+ for (let i = 0; i < rules.length; i++) {
314
+ const ruleId = rules[i];
315
+ const ruleObject = ruleAssertionMap[ruleId];
316
+ if (_get(ruleObject, 'abstract')) {
317
+ continue;
318
+ }
319
+ const context = _get(ruleObject, 'context');
320
+ const assertionsAndExtensions = _get(ruleObject, 'assertionsAndExtensions') || [];
321
+ try {
322
+ failedAssertions = await checkRulePromise(xmlDoc, context, assertionsAndExtensions, options);
323
+ }
324
+ catch (errorInRuleCheck) {
325
+ console.log(errorInRuleCheck);
326
+ continue;
327
+ }
328
+ for (let j = 0; j < failedAssertions.length; j++) {
329
+ const assertionObject = failedAssertions[j];
330
+ const { type, assertionId, test, simplifiedTest, description, errorMessage, results } = assertionObject;
331
+ if (Array.isArray(results)) {
332
+ for (let k = 0; k < results.length; k++) {
333
+ const resultObject = results[k];
334
+ const { result, line, path, xml } = resultObject;
335
+ if (!result) {
336
+ let obj = {
337
+ type: type,
338
+ test: test,
339
+ simplifiedTest: simplifiedTest,
340
+ description: description,
341
+ line: line,
342
+ path: path,
343
+ patternId: patternId,
344
+ ruleId: ruleId,
345
+ assertionId: assertionId,
346
+ context: context,
347
+ xml: xml
348
+ };
349
+ if (type === 'error') {
350
+ errors.push(obj);
351
+ }
352
+ else {
353
+ warnings.push(obj);
354
+ }
355
+ }
356
+ }
357
+ }
358
+ else if (results.ignored) {
359
+ let obj = {
360
+ errorMessage: errorMessage,
361
+ type: type,
362
+ test: test,
363
+ simplifiedTest: simplifiedTest,
364
+ description: description,
365
+ patternId: patternId,
366
+ ruleId: ruleId,
367
+ assertionId: assertionId,
368
+ context: context
369
+ };
370
+ ignored.push(obj);
371
+ }
372
+ }
373
+ }
374
+ }
375
+
376
+ return {
377
+ errorCount: errors.length,
378
+ warningCount: warnings.length,
379
+ ignoredCount: ignored.length,
380
+ errors: errors,
381
+ warnings: warnings,
382
+ ignored: ignored
383
+ };
384
+ }
385
+
386
+ // Take the checkRule function out of validate function, and pass on the variable needed as parameters and options
387
+ function checkRulePromise(xmlDoc, originalContext, assertionsAndExtensions, options) {
388
+ // Context cache
389
+ const resultCapture = new Promise((resolve, reject) => {
390
+ try {
391
+ // Context cache
392
+ const includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
393
+ const resourceDir = options.resourceDir || './';
394
+ const xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
395
+ let failedAssertions = [];
396
+ const context = options.contextOverride || originalContext;
397
+ // Determine the sections within context, load selected section from cache if possible
398
+ let selected = contextMap[context];
399
+ let contextModified = context;
400
+ if (!selected) {
401
+ if (context) {
402
+ if (context.indexOf('/')) {
403
+ contextModified = '//' + context;
404
+ }
405
+ selected = xpathSelect(contextModified, xmlDoc);
406
+ }
407
+ else {
408
+ selected = [xmlDoc];
409
+ }
410
+ contextMap[context] = selected;
411
+ }
412
+
413
+ for (let i = 0; i < assertionsAndExtensions.length; i++) {
414
+ const assertionAndExtensionObject = assertionsAndExtensions[i];
415
+ if (assertionAndExtensionObject.type === 'assertion') {
416
+ let { level, test, id, description } = assertionAndExtensionObject;
417
+
418
+ // Extract values from external document and modify test if a document call is made
419
+ let originalTest = test;
420
+ let simplifiedTest = null;
421
+ try {
422
+ test = includeExternalDocument(test, resourceDir);
423
+ }
424
+ catch (err) {
425
+ failedAssertions.push({
426
+ type: level,
427
+ assertionId: id,
428
+ test: originalTest,
429
+ simplifiedTest: simplifiedTest,
430
+ description: description,
431
+ results: { ignored: true, errorMessage: err.message }
432
+ });
433
+ continue;
434
+ }
435
+ if (originalTest !== test) {
436
+ simplifiedTest = test;
437
+ }
438
+ if (level === 'error' || includeWarnings) {
439
+ failedAssertions.push({
440
+ type: level,
441
+ assertionId: id,
442
+ test: originalTest,
443
+ simplifiedTest: simplifiedTest,
444
+ description: description,
445
+ results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
446
+ });
447
+ }
448
+ }
449
+ else {
450
+ const extensionRule = assertionAndExtensionObject.rule;
451
+ if (!extensionRule) {
452
+ continue;
453
+ }
454
+ const subAssertionsAndExtensions = ruleAssertionMap[extensionRule] ? ruleAssertionMap[extensionRule].assertionsAndExtensions : null;
455
+ if (!subAssertionsAndExtensions) {
456
+ continue;
457
+ }
458
+ const newRuleObject = ruleAssertionMap[extensionRule];
459
+ const newContext = newRuleObject.context;
460
+ const failedSubAssertions = checkRule(xmlDoc, newContext, subAssertionsAndExtensions, options);
461
+ failedAssertions.push(...failedSubAssertions);
462
+ }
463
+ }
464
+ resolve (failedAssertions);
465
+ }
466
+ catch (promiseError) {
467
+ reject (new Error (`Promise Error: ${promiseError}`));
468
+ }
469
+ });
470
+ return resultCapture;
233
471
  }