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.
Files changed (2) hide show
  1. package/package.json +6 -1
  2. 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.1",
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
- let namespaceMap = schematronMap.namespaceMap;
83
- let patternRuleMap = schematronMap.patternRuleMap;
84
- let ruleAssertionMap = schematronMap.ruleAssertionMap;
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
- const xpathSelect = xpath.useNamespaces(namespaceMap);
88
+ xpathSelect = xpath.useNamespaces(namespaceMap);
89
89
 
90
90
  let errors = [];
91
91
  let warnings = [];
92
92
  let ignored = [];
93
- for (let pattern in patternRuleMap) {
94
- if (!patternRuleMap.hasOwnProperty(pattern)) {
93
+ for (let patternId in patternRuleMap) {
94
+ if (!patternRuleMap.hasOwnProperty(patternId)) {
95
95
  continue;
96
96
  }
97
- let patternId = pattern;
98
- let rules = patternRuleMap[pattern];
97
+ const rules = patternRuleMap[patternId];
99
98
  for (let i = 0; i < rules.length; i++) {
100
- if (ruleAssertionMap[rules[i]].abstract) {
99
+ const ruleId = rules[i];
100
+ const ruleObject = ruleAssertionMap[ruleId];
101
+ if (_get(ruleObject, 'abstract')) {
101
102
  continue;
102
- }
103
- let ruleId = rules[i];
104
- let context = ruleAssertionMap[rules[i]].context;
105
- let assertionsAndExtensions = ruleAssertionMap[rules[i]].assertionsAndExtensions || [];
106
- let assertionResults = checkRule(xmlDoc, xpathSelect, context, assertionsAndExtensions, options);
107
- for (let j = 0; j < assertionResults.length; j++) {
108
- let type = assertionResults[j].type;
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
- let result = results[k].result;
118
- let line = results[k].line;
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: xmlSnippet
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 ? ignored.length : 0,
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, xpathSelect, originalContext, assertionsAndExtensions, options) {
166
+ function checkRule(xmlDoc, originalContext, assertionsAndExtensions, options) {
174
167
  // Context cache
175
- let includeWarnings = options.includeWarnings === undefined ? true : options.includeWarnings;
176
- let resourceDir = options.resourceDir || './';
177
- let xmlSnippetMaxLength = options.xmlSnippetMaxLength === undefined ? 200 : options.xmlSnippetMaxLength;
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
- let context = options.contextOverride || originalContext;
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
- if (assertionsAndExtensions[i].type === 'assertion') {
198
- let type = assertionsAndExtensions[i].level;
199
- let test = assertionsAndExtensions[i].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 (type === 'error' || includeWarnings) {
207
+ if (level === 'error' || includeWarnings) {
215
208
  results.push({
216
- type: type,
217
- assertionId: assertionsAndExtensions[i].id,
209
+ type: level,
210
+ assertionId: id,
218
211
  test: originalTest,
219
212
  simplifiedTest: simplifiedTest,
220
- description: assertionsAndExtensions[i].description,
213
+ description: description,
221
214
  results: testAssertion(test, selected, xpathSelect, xmlDoc, resourceDir, xmlSnippetMaxLength)
222
215
  });
223
216
  }
224
217
  }
225
- // removed recursive call to checkRule as it just returns an emptry array
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
+ }