cda-schematron-validator 1.1.2 → 1.1.6
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/.eslintrc.js +30 -0
- package/.husky/pre-commit +5 -0
- package/.jshintrc +3 -3
- package/Gruntfile.js +8 -11
- package/README.md +71 -14
- package/lib/checkIfFileOrPath.js +28 -0
- package/lib/checkRule.js +35 -0
- package/lib/getAssertionObject.js +12 -0
- package/lib/getErrorsAndWarnings.js +34 -0
- package/lib/getFailedAssertions.js +16 -0
- package/lib/getIgnored.js +19 -0
- package/lib/getResults.js +13 -0
- package/lib/getReturnObject.js +20 -0
- package/lib/getSchematronMap.js +31 -0
- package/lib/getSelectXmlChunk.js +20 -0
- package/{includeExternalDocument.js → lib/includeExternalDocument.js} +28 -30
- package/lib/initializeProcess.js +45 -0
- package/lib/processAssertions.js +33 -0
- package/lib/processFailedAssertions.js +9 -0
- package/lib/processRules.js +20 -0
- package/lib/processRulesAsync.js +30 -0
- package/{testAssertion.js → lib/testAssertion.js} +10 -9
- package/package.json +9 -3
- package/parseSchematron.js +41 -41
- package/test/async.test.js +10 -10
- package/test/asyncFileList.test.js +37 -0
- package/test/tests.js +13 -14
- package/test/vitalSigns.test.js +4 -98
- package/validateAsync.js +70 -0
- package/validateSync.js +55 -0
- package/validator.js +5 -455
package/test/vitalSigns.test.js
CHANGED
|
@@ -12,42 +12,16 @@ const schematronPath = path.resolve(__dirname, './CDAR2_IG_CCDA_CLINNOTES_R1_DST
|
|
|
12
12
|
const xml = fs.readFileSync(xmlPath, 'utf-8').toString();
|
|
13
13
|
const schematron = fs.readFileSync(schematronPath, 'utf-8').toString();
|
|
14
14
|
const options = {
|
|
15
|
-
resourceDir: path.resolve(__dirname, './')
|
|
16
|
-
|
|
15
|
+
resourceDir: path.resolve(__dirname, './'),
|
|
16
|
+
includeWarnings: true
|
|
17
|
+
};
|
|
17
18
|
|
|
18
|
-
describe('
|
|
19
|
+
describe('Vital Sign async schematron validation should', function() {
|
|
19
20
|
let results;
|
|
20
21
|
it('return results', async function() {
|
|
21
22
|
results = await validator.validateAsync(xml, schematron, options);
|
|
22
23
|
expect(results).to.be.an('object');
|
|
23
24
|
});
|
|
24
|
-
it('return errorCount', function() {
|
|
25
|
-
expect(results.errorCount).to.be.a('number');
|
|
26
|
-
});
|
|
27
|
-
it('return warningCount', function() {
|
|
28
|
-
expect(results.warningCount).to.be.a('number');
|
|
29
|
-
});
|
|
30
|
-
it('return ignoredCount', function() {
|
|
31
|
-
expect(results.ignoredCount).to.be.a('number');
|
|
32
|
-
});
|
|
33
|
-
it('return errors array', function() {
|
|
34
|
-
expect(results.errors).to.be.a('array');
|
|
35
|
-
});
|
|
36
|
-
it('return warnings array', function() {
|
|
37
|
-
expect(results.warnings).to.be.a('array');
|
|
38
|
-
});
|
|
39
|
-
it('return ignored array', function() {
|
|
40
|
-
expect(results.ignored).to.be.a('array');
|
|
41
|
-
});
|
|
42
|
-
it('return matching errorCount', function() {
|
|
43
|
-
expect(results.errorCount).to.be.equal(results.errors.length);
|
|
44
|
-
});
|
|
45
|
-
it('return matching warningCount', function() {
|
|
46
|
-
expect(results.warningCount).to.be.equal(results.warnings.length);
|
|
47
|
-
});
|
|
48
|
-
it('return matching ignoredCount', function() {
|
|
49
|
-
expect(results.ignoredCount).to.be.equal(results.ignored.length);
|
|
50
|
-
});
|
|
51
25
|
it('return correct number of errors', function() {
|
|
52
26
|
expect(results.errorCount).to.be.equal(1);
|
|
53
27
|
});
|
|
@@ -57,72 +31,4 @@ describe('Validator should', function() {
|
|
|
57
31
|
it('return correct number of ignored', function() {
|
|
58
32
|
expect(results.ignoredCount).to.be.equal(0);
|
|
59
33
|
});
|
|
60
|
-
|
|
61
|
-
it('return similar results without warnings', async function() {
|
|
62
|
-
results = await validator.validateAsync(xml, schematron, { includeWarnings: false, resourceDir: options.resourceDir });
|
|
63
|
-
expect(results).to.be.an('object');
|
|
64
|
-
expect(results.errorCount).to.be.a('number');
|
|
65
|
-
expect(results.warningCount).to.be.a('number');
|
|
66
|
-
expect(results.ignoredCount).to.be.a('number');
|
|
67
|
-
expect(results.errors).to.be.a('array');
|
|
68
|
-
expect(results.warnings).to.be.a('array');
|
|
69
|
-
expect(results.ignored).to.be.a('array');
|
|
70
|
-
expect(results.errorCount).to.be.equal(results.errors.length);
|
|
71
|
-
expect(results.warningCount).to.be.equal(results.warnings.length);
|
|
72
|
-
expect(results.ignoredCount).to.be.equal(results.ignored.length);
|
|
73
|
-
expect(results.errorCount).to.be.equal(1);
|
|
74
|
-
expect(results.warningCount).to.be.equal(0);
|
|
75
|
-
expect(results.ignoredCount).to.be.equal(0);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('return similar results given xml filepath', async function() {
|
|
79
|
-
results = await validator.validateAsync(xmlPath, schematron, options);
|
|
80
|
-
expect(results).to.be.an('object');
|
|
81
|
-
expect(results.errorCount).to.be.a('number');
|
|
82
|
-
expect(results.warningCount).to.be.a('number');
|
|
83
|
-
expect(results.ignoredCount).to.be.a('number');
|
|
84
|
-
expect(results.errors).to.be.a('array');
|
|
85
|
-
expect(results.warnings).to.be.a('array');
|
|
86
|
-
expect(results.ignored).to.be.a('array');
|
|
87
|
-
expect(results.errorCount).to.be.equal(results.errors.length);
|
|
88
|
-
expect(results.warningCount).to.be.equal(results.warnings.length);
|
|
89
|
-
expect(results.ignoredCount).to.be.equal(results.ignored.length);
|
|
90
|
-
expect(results.errorCount).to.be.equal(1);
|
|
91
|
-
expect(results.warningCount).to.be.equal(6);
|
|
92
|
-
expect(results.ignoredCount).to.be.equal(0);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('return similar results given schematron filepath', async function() {
|
|
96
|
-
results = await validator.validateAsync(xml, schematronPath, options);
|
|
97
|
-
expect(results).to.be.an('object');
|
|
98
|
-
expect(results.errorCount).to.be.a('number');
|
|
99
|
-
expect(results.warningCount).to.be.a('number');
|
|
100
|
-
expect(results.ignoredCount).to.be.a('number');
|
|
101
|
-
expect(results.errors).to.be.a('array');
|
|
102
|
-
expect(results.warnings).to.be.a('array');
|
|
103
|
-
expect(results.ignored).to.be.a('array');
|
|
104
|
-
expect(results.errorCount).to.be.equal(results.errors.length);
|
|
105
|
-
expect(results.warningCount).to.be.equal(results.warnings.length);
|
|
106
|
-
expect(results.ignoredCount).to.be.equal(results.ignored.length);
|
|
107
|
-
expect(results.errorCount).to.be.equal(1);
|
|
108
|
-
expect(results.warningCount).to.be.equal(6);
|
|
109
|
-
expect(results.ignoredCount).to.be.equal(0);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('return similar results given xml filepath and schematron filepath', async function() {
|
|
113
|
-
results = await validator.validateAsync(xmlPath, schematronPath, options);
|
|
114
|
-
expect(results).to.be.an('object');
|
|
115
|
-
expect(results.errorCount).to.be.a('number');
|
|
116
|
-
expect(results.warningCount).to.be.a('number');
|
|
117
|
-
expect(results.ignoredCount).to.be.a('number');
|
|
118
|
-
expect(results.errors).to.be.a('array');
|
|
119
|
-
expect(results.warnings).to.be.a('array');
|
|
120
|
-
expect(results.ignored).to.be.a('array');
|
|
121
|
-
expect(results.errorCount).to.be.equal(results.errors.length);
|
|
122
|
-
expect(results.warningCount).to.be.equal(results.warnings.length);
|
|
123
|
-
expect(results.ignoredCount).to.be.equal(results.ignored.length);
|
|
124
|
-
expect(results.errorCount).to.be.equal(1);
|
|
125
|
-
expect(results.warningCount).to.be.equal(6);
|
|
126
|
-
expect(results.ignoredCount).to.be.equal(0);
|
|
127
|
-
});
|
|
128
34
|
});
|
package/validateAsync.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// crypto is now built-in node
|
|
2
|
+
const initializeProcess = require('./lib/initializeProcess');
|
|
3
|
+
const processRulesAsync = require('./lib/processRulesAsync');
|
|
4
|
+
const getReturnObject = require('./lib/getReturnObject');
|
|
5
|
+
|
|
6
|
+
// Parsed object cache
|
|
7
|
+
let parsedMap = Object.create({});
|
|
8
|
+
let contextMap = Object.create({});
|
|
9
|
+
let schematronMap = Object.create({});
|
|
10
|
+
|
|
11
|
+
function clearCache() {
|
|
12
|
+
parsedMap = Object.create({});
|
|
13
|
+
contextMap = Object.create({});
|
|
14
|
+
schematronMap = Object.create({});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @method validateAsync The entry method called to validate an xml with schematron. Calls `initializeProcess` and `processRulesAsync` methods, and gets the results.
|
|
19
|
+
* This is similar to `validate` method except that it uses asynchronous `processRulesAsync` method.
|
|
20
|
+
* @param {string} xml string content of an xml file or path to an xml file
|
|
21
|
+
* @param {string} schematron string content of a schematron file or path to a schematron file
|
|
22
|
+
* @param {object} options object that takes parameters
|
|
23
|
+
* `resourceDir`: Where additional documents with added rules such as value set restrictions are present. Default value is the same directory of the code being executed './'.,
|
|
24
|
+
* `xmlSnippetMaxLength`: Restrict the length of the xpath mapped xml snippet when returning the error/warning data to the user. Default value is 200 characters.,
|
|
25
|
+
* `includeWarnings`: User can choose to look for warnings and include them in the report. The default value is false.,
|
|
26
|
+
* `parsedSchematronMap`: User can choose to supply the schematron map instead of a schematron file avoiding the parsing of the schematron file for multiple xml files or check for cache.
|
|
27
|
+
* @returns Calls `getReturnObject` and gets the results (errors, warning, and ignored) as javascript objects
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
async function validateAsync(xml, schematron, options = {}) {
|
|
31
|
+
const resultInfo = {
|
|
32
|
+
errors: [],
|
|
33
|
+
warnings: [],
|
|
34
|
+
ignored: []
|
|
35
|
+
};
|
|
36
|
+
options.parsedMap = parsedMap;
|
|
37
|
+
options.contextMap = contextMap;
|
|
38
|
+
options.schematronMap = schematronMap;
|
|
39
|
+
initializeProcess(xml, schematron, options);
|
|
40
|
+
return Promise.all(
|
|
41
|
+
Object.keys(options.schematronMap.patternRuleMap).map(async (patternId) => {
|
|
42
|
+
if (options.schematronMap.patternRuleMap.hasOwnProperty(patternId)) {
|
|
43
|
+
const assertionInfo = Object.create({});
|
|
44
|
+
assertionInfo.patternId = patternId;
|
|
45
|
+
const rules = options.schematronMap.patternRuleMap[patternId];
|
|
46
|
+
await processRulesAsync(rules, assertionInfo, resultInfo, options);
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
).then(() => {
|
|
50
|
+
return getReturnObject(resultInfo);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function validateFileListAsync(xmlArray, schematron, options = {}) {
|
|
55
|
+
const resultInfo = [];
|
|
56
|
+
return Promise.all(
|
|
57
|
+
xmlArray.map(async (xml) => {
|
|
58
|
+
resultInfo.push(await validateAsync(xml, schematron, options));
|
|
59
|
+
console.log(`Number of files processed: ${resultInfo.length}`);
|
|
60
|
+
})
|
|
61
|
+
).then(() => {
|
|
62
|
+
return resultInfo;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = {
|
|
67
|
+
validateAsync,
|
|
68
|
+
validateFileListAsync,
|
|
69
|
+
clearCache
|
|
70
|
+
};
|
package/validateSync.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// crypto is now built-in node
|
|
2
|
+
const initializeProcess = require('./lib/initializeProcess');
|
|
3
|
+
const processRules = require('./lib/processRules');
|
|
4
|
+
const getReturnObject = require('./lib/getReturnObject');
|
|
5
|
+
|
|
6
|
+
// Parsed object cache
|
|
7
|
+
let parsedMap = Object.create({});
|
|
8
|
+
let contextMap = Object.create({});
|
|
9
|
+
let schematronMap = Object.create({});
|
|
10
|
+
|
|
11
|
+
function clearCache() {
|
|
12
|
+
parsedMap = Object.create({});
|
|
13
|
+
contextMap = Object.create({});
|
|
14
|
+
schematronMap = Object.create({});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @method validate The entry method called to validate an xml with schematron. Calls `initializeProcess` and `processRules` methods, and gets the results
|
|
20
|
+
* @param {string} xml string content of an xml file or path to an xml file
|
|
21
|
+
* @param {string} schematron string content of a schematron file or path to a schematron file
|
|
22
|
+
* @param {object} options object that takes parameters
|
|
23
|
+
* `resourceDir`: Where additional documents with added rules such as value set restrictions are present. Default value is the same directory of the code being executed './'.,
|
|
24
|
+
* `xmlSnippetMaxLength`: Restrict the length of the xpath mapped xml snippet when returning the error/warning data to the user. Default value is 200 characters.,
|
|
25
|
+
* `includeWarnings`: User can choose to look for warnings and include them in the report. The default value is false.,
|
|
26
|
+
* `parsedSchematronMap`: User can choose to supply the schematron map instead of a schematron file avoiding the parsing of the schematron file for multiple xml files or check for cache.
|
|
27
|
+
* @returns Calls `getReturnObject` and gets the results (errors, warning, and ignored) as javascript objects
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
function validateSync(xml, schematron, options = {}) {
|
|
31
|
+
const resultInfo = {
|
|
32
|
+
errors: [],
|
|
33
|
+
warnings: [],
|
|
34
|
+
ignored: []
|
|
35
|
+
};
|
|
36
|
+
options.parsedMap = parsedMap;
|
|
37
|
+
options.contextMap = contextMap;
|
|
38
|
+
options.schematronMap = schematronMap;
|
|
39
|
+
initializeProcess(xml, schematron, options);
|
|
40
|
+
for (const patternId in options.schematronMap.patternRuleMap) {
|
|
41
|
+
if (!options.schematronMap.patternRuleMap.hasOwnProperty(patternId)) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const assertionInfo = Object.create({});
|
|
45
|
+
assertionInfo.patternId = patternId;
|
|
46
|
+
const rules = options.schematronMap.patternRuleMap[patternId];
|
|
47
|
+
processRules(rules, assertionInfo, resultInfo, options);
|
|
48
|
+
}
|
|
49
|
+
return getReturnObject(resultInfo);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
validateSync,
|
|
54
|
+
clearCache
|
|
55
|
+
};
|