cda-schematron-validator 1.0.2 → 1.0.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/.jshintrc +96 -96
- package/.vscode/launch.json +17 -17
- package/README.md +132 -132
- package/includeExternalDocument.js +108 -108
- package/package.json +4 -10
- package/parseSchematron.js +121 -121
- package/test/test.sch +46 -46
- package/test/test.xml +79 -79
- package/test/tests.js +141 -141
- package/testAssertion.js +72 -72
- package/validator.js +233 -228
package/.jshintrc
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
/*jshint :esversion 6 */
|
|
2
|
-
{
|
|
3
|
-
// JSHint Default Configuration File (as on JSHint website)
|
|
4
|
-
// See http://jshint.com/docs/ for more details
|
|
5
|
-
|
|
6
|
-
"maxerr": 500, // {int} Maximum error before stopping
|
|
7
|
-
|
|
8
|
-
// Enforcing
|
|
9
|
-
"bitwise": true, // true: Prohibit bitwise operators (&, |, ^, etc.)
|
|
10
|
-
"camelcase": false, // true: Identifiers must be in camelCase
|
|
11
|
-
"curly": false, // true: Require {} for every new block or scope
|
|
12
|
-
"eqeqeq": true, // true: Require triple equals (===) for comparison
|
|
13
|
-
"forin": true, // true: Require filtering for..in loops with obj.hasOwnProperty()
|
|
14
|
-
"immed": true, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
|
|
15
|
-
"indent": false, // {int} Number of spaces to use for indentation
|
|
16
|
-
"latedef": false, // true: Require variables/functions to be defined before being used
|
|
17
|
-
"newcap": false, // true: Require capitalization of all constructor functions e.g. `new F()`
|
|
18
|
-
"noarg": true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
|
|
19
|
-
"noempty": true, // true: Prohibit use of empty blocks
|
|
20
|
-
"nonew": false, // true: Prohibit use of constructors for side-effects (without assignment)
|
|
21
|
-
"plusplus": false, // true: Prohibit use of `++` & `--`
|
|
22
|
-
"quotmark": "single", // Quotation mark consistency:
|
|
23
|
-
// false : do nothing (default)
|
|
24
|
-
// true : ensure whatever is used is consistent
|
|
25
|
-
// "single" : require single quotes
|
|
26
|
-
// "double" : require double quotes
|
|
27
|
-
"undef": true, // true: Require all non-global variables to be declared (prevents global leaks)
|
|
28
|
-
"unused": true, // true: Require all defined variables be used
|
|
29
|
-
"strict": false, // true: Requires all functions run in ES5 Strict Mode
|
|
30
|
-
"trailing": false, // true: Prohibit trailing whitespaces
|
|
31
|
-
"maxparams": false, // {int} Max number of formal params allowed per function
|
|
32
|
-
"maxdepth": false, // {int} Max depth of nested blocks (within functions)
|
|
33
|
-
"maxstatements": false, // {int} Max number statements per function
|
|
34
|
-
"maxcomplexity": false, // {int} Max cyclomatic complexity per function
|
|
35
|
-
"maxlen": false, // {int} Max number of characters per line
|
|
36
|
-
|
|
37
|
-
// Relaxing
|
|
38
|
-
"asi": false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
|
|
39
|
-
"boss": false, // true: Tolerate assignments where comparisons would be expected
|
|
40
|
-
"debug": true, // true: Allow debugger statements e.g. browser breakpoints.
|
|
41
|
-
"eqnull": false, // true: Tolerate use of `== null`
|
|
42
|
-
"es5": false, // true: Allow ES5 syntax (ex: getters and setters)
|
|
43
|
-
"esversion": 6,
|
|
44
|
-
"esnext": false, // true: Allow ES.next (ES6) syntax (ex: `const`)
|
|
45
|
-
"moz": false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
|
|
46
|
-
// (ex: `for each`, multiple try/catch, function expression...)
|
|
47
|
-
"evil": false, // true: Tolerate use of `eval` and `new Function()`
|
|
48
|
-
"expr": true, // true: Tolerate `ExpressionStatement` as Programs
|
|
49
|
-
"funcscope": false, // true: Tolerate defining variables inside control statements"
|
|
50
|
-
"globalstrict": false, // true: Allow global "use strict" (also enables 'strict')
|
|
51
|
-
"iterator": false, // true: Tolerate using the `__iterator__` property
|
|
52
|
-
"lastsemic": false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
|
|
53
|
-
"laxbreak": true, // true: Tolerate possibly unsafe line breakings
|
|
54
|
-
"laxcomma": true, // true: Tolerate comma-first style coding
|
|
55
|
-
"loopfunc": false, // true: Tolerate functions being defined in loops
|
|
56
|
-
"multistr": false, // true: Tolerate multi-line strings
|
|
57
|
-
"proto": false, // true: Tolerate using the `__proto__` property
|
|
58
|
-
"scripturl": false, // true: Tolerate script-targeted URLs
|
|
59
|
-
"smarttabs": true, // true: Tolerate mixed tabs/spaces when used for alignment
|
|
60
|
-
"shadow": false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
|
|
61
|
-
"sub": false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
|
|
62
|
-
"supernew": false, // true: Tolerate `new function () { ... };` and `new Object;`
|
|
63
|
-
"validthis": false, // true: Tolerate using this in a non-constructor function
|
|
64
|
-
|
|
65
|
-
// Environments
|
|
66
|
-
"browser": true, // Web Browser (window, document, etc)
|
|
67
|
-
"couch": false, // CouchDB
|
|
68
|
-
"devel": true, // Development/debugging (alert, confirm, etc)
|
|
69
|
-
"dojo": false, // Dojo Toolkit
|
|
70
|
-
"jquery": true, // jQuery
|
|
71
|
-
"mootools": false, // MooTools
|
|
72
|
-
"node": false, // Node.js
|
|
73
|
-
"nonstandard": false, // Widely adopted globals (escape, unescape, etc)
|
|
74
|
-
"prototypejs": false, // Prototype and Scriptaculous
|
|
75
|
-
"rhino": false, // Rhino
|
|
76
|
-
"worker": false, // Web Workers
|
|
77
|
-
"wsh": false, // Windows Scripting Host
|
|
78
|
-
"yui": false, // Yahoo User Interface
|
|
79
|
-
|
|
80
|
-
// Legacy
|
|
81
|
-
"nomen": false, // true: Prohibit dangling `_` in variables
|
|
82
|
-
"onevar": false, // true: Allow only one `var` statement per function
|
|
83
|
-
"passfail": false, // true: Stop on first error
|
|
84
|
-
"white": false, // true: Check against strict whitespace and indentation rules
|
|
85
|
-
|
|
86
|
-
// Custom Globals
|
|
87
|
-
"globals": {
|
|
88
|
-
"describe": false,
|
|
89
|
-
"it": false,
|
|
90
|
-
"before": false,
|
|
91
|
-
"beforeEach": false,
|
|
92
|
-
"after": false,
|
|
93
|
-
"afterEach": false,
|
|
94
|
-
"module" : false
|
|
95
|
-
}
|
|
96
|
-
}
|
|
1
|
+
/*jshint :esversion 6 */
|
|
2
|
+
{
|
|
3
|
+
// JSHint Default Configuration File (as on JSHint website)
|
|
4
|
+
// See http://jshint.com/docs/ for more details
|
|
5
|
+
|
|
6
|
+
"maxerr": 500, // {int} Maximum error before stopping
|
|
7
|
+
|
|
8
|
+
// Enforcing
|
|
9
|
+
"bitwise": true, // true: Prohibit bitwise operators (&, |, ^, etc.)
|
|
10
|
+
"camelcase": false, // true: Identifiers must be in camelCase
|
|
11
|
+
"curly": false, // true: Require {} for every new block or scope
|
|
12
|
+
"eqeqeq": true, // true: Require triple equals (===) for comparison
|
|
13
|
+
"forin": true, // true: Require filtering for..in loops with obj.hasOwnProperty()
|
|
14
|
+
"immed": true, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
|
|
15
|
+
"indent": false, // {int} Number of spaces to use for indentation
|
|
16
|
+
"latedef": false, // true: Require variables/functions to be defined before being used
|
|
17
|
+
"newcap": false, // true: Require capitalization of all constructor functions e.g. `new F()`
|
|
18
|
+
"noarg": true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
|
|
19
|
+
"noempty": true, // true: Prohibit use of empty blocks
|
|
20
|
+
"nonew": false, // true: Prohibit use of constructors for side-effects (without assignment)
|
|
21
|
+
"plusplus": false, // true: Prohibit use of `++` & `--`
|
|
22
|
+
"quotmark": "single", // Quotation mark consistency:
|
|
23
|
+
// false : do nothing (default)
|
|
24
|
+
// true : ensure whatever is used is consistent
|
|
25
|
+
// "single" : require single quotes
|
|
26
|
+
// "double" : require double quotes
|
|
27
|
+
"undef": true, // true: Require all non-global variables to be declared (prevents global leaks)
|
|
28
|
+
"unused": true, // true: Require all defined variables be used
|
|
29
|
+
"strict": false, // true: Requires all functions run in ES5 Strict Mode
|
|
30
|
+
"trailing": false, // true: Prohibit trailing whitespaces
|
|
31
|
+
"maxparams": false, // {int} Max number of formal params allowed per function
|
|
32
|
+
"maxdepth": false, // {int} Max depth of nested blocks (within functions)
|
|
33
|
+
"maxstatements": false, // {int} Max number statements per function
|
|
34
|
+
"maxcomplexity": false, // {int} Max cyclomatic complexity per function
|
|
35
|
+
"maxlen": false, // {int} Max number of characters per line
|
|
36
|
+
|
|
37
|
+
// Relaxing
|
|
38
|
+
"asi": false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
|
|
39
|
+
"boss": false, // true: Tolerate assignments where comparisons would be expected
|
|
40
|
+
"debug": true, // true: Allow debugger statements e.g. browser breakpoints.
|
|
41
|
+
"eqnull": false, // true: Tolerate use of `== null`
|
|
42
|
+
"es5": false, // true: Allow ES5 syntax (ex: getters and setters)
|
|
43
|
+
"esversion": 6,
|
|
44
|
+
"esnext": false, // true: Allow ES.next (ES6) syntax (ex: `const`)
|
|
45
|
+
"moz": false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
|
|
46
|
+
// (ex: `for each`, multiple try/catch, function expression...)
|
|
47
|
+
"evil": false, // true: Tolerate use of `eval` and `new Function()`
|
|
48
|
+
"expr": true, // true: Tolerate `ExpressionStatement` as Programs
|
|
49
|
+
"funcscope": false, // true: Tolerate defining variables inside control statements"
|
|
50
|
+
"globalstrict": false, // true: Allow global "use strict" (also enables 'strict')
|
|
51
|
+
"iterator": false, // true: Tolerate using the `__iterator__` property
|
|
52
|
+
"lastsemic": false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
|
|
53
|
+
"laxbreak": true, // true: Tolerate possibly unsafe line breakings
|
|
54
|
+
"laxcomma": true, // true: Tolerate comma-first style coding
|
|
55
|
+
"loopfunc": false, // true: Tolerate functions being defined in loops
|
|
56
|
+
"multistr": false, // true: Tolerate multi-line strings
|
|
57
|
+
"proto": false, // true: Tolerate using the `__proto__` property
|
|
58
|
+
"scripturl": false, // true: Tolerate script-targeted URLs
|
|
59
|
+
"smarttabs": true, // true: Tolerate mixed tabs/spaces when used for alignment
|
|
60
|
+
"shadow": false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
|
|
61
|
+
"sub": false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
|
|
62
|
+
"supernew": false, // true: Tolerate `new function () { ... };` and `new Object;`
|
|
63
|
+
"validthis": false, // true: Tolerate using this in a non-constructor function
|
|
64
|
+
|
|
65
|
+
// Environments
|
|
66
|
+
"browser": true, // Web Browser (window, document, etc)
|
|
67
|
+
"couch": false, // CouchDB
|
|
68
|
+
"devel": true, // Development/debugging (alert, confirm, etc)
|
|
69
|
+
"dojo": false, // Dojo Toolkit
|
|
70
|
+
"jquery": true, // jQuery
|
|
71
|
+
"mootools": false, // MooTools
|
|
72
|
+
"node": false, // Node.js
|
|
73
|
+
"nonstandard": false, // Widely adopted globals (escape, unescape, etc)
|
|
74
|
+
"prototypejs": false, // Prototype and Scriptaculous
|
|
75
|
+
"rhino": false, // Rhino
|
|
76
|
+
"worker": false, // Web Workers
|
|
77
|
+
"wsh": false, // Windows Scripting Host
|
|
78
|
+
"yui": false, // Yahoo User Interface
|
|
79
|
+
|
|
80
|
+
// Legacy
|
|
81
|
+
"nomen": false, // true: Prohibit dangling `_` in variables
|
|
82
|
+
"onevar": false, // true: Allow only one `var` statement per function
|
|
83
|
+
"passfail": false, // true: Stop on first error
|
|
84
|
+
"white": false, // true: Check against strict whitespace and indentation rules
|
|
85
|
+
|
|
86
|
+
// Custom Globals
|
|
87
|
+
"globals": {
|
|
88
|
+
"describe": false,
|
|
89
|
+
"it": false,
|
|
90
|
+
"before": false,
|
|
91
|
+
"beforeEach": false,
|
|
92
|
+
"after": false,
|
|
93
|
+
"afterEach": false,
|
|
94
|
+
"module" : false
|
|
95
|
+
}
|
|
96
|
+
}
|
package/.vscode/launch.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "0.2.0",
|
|
3
|
-
"configurations": [
|
|
4
|
-
{
|
|
5
|
-
"name": "Mocha (Test single file)",
|
|
6
|
-
"type": "node",
|
|
7
|
-
"request": "launch",
|
|
8
|
-
"runtimeArgs": [
|
|
9
|
-
"${workspaceRoot}/node_modules
|
|
10
|
-
"--inspect-brk",
|
|
11
|
-
"${relativeFile}",
|
|
12
|
-
],
|
|
13
|
-
"console": "integratedTerminal",
|
|
14
|
-
"internalConsoleOptions": "neverOpen",
|
|
15
|
-
"port": 9229
|
|
16
|
-
}
|
|
17
|
-
]
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"name": "Mocha (Test single file)",
|
|
6
|
+
"type": "node",
|
|
7
|
+
"request": "launch",
|
|
8
|
+
"runtimeArgs": [
|
|
9
|
+
"${workspaceRoot}/node_modules/mocha/bin/mocha",
|
|
10
|
+
"--inspect-brk",
|
|
11
|
+
"${relativeFile}",
|
|
12
|
+
],
|
|
13
|
+
"console": "integratedTerminal",
|
|
14
|
+
"internalConsoleOptions": "neverOpen",
|
|
15
|
+
"port": 9229
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
18
|
}
|
package/README.md
CHANGED
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
# cda-schematron-validator
|
|
2
|
-
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
|
-
|
|
4
|
-
This is just a cosmetic change with updated node modules, a couple of minor bugs observed, and esversion 6 changes.
|
|
5
|
-
|
|
6
|
-
The changes are fully backward compatible. So, leaving the original documentation as is below.
|
|
7
|
-
|
|
8
|
-
### New option parameter parsedSchematronMap
|
|
9
|
-
In addition to everything that was possible,
|
|
10
|
-
you can now supply a new option parameter `parsedSchematronMap`. You can create a schematron map as follows.
|
|
11
|
-
|
|
12
|
-
```javascript
|
|
13
|
-
const fs = require('fs');
|
|
14
|
-
const dom = require('@xmldom/xmldom').DOMParser;
|
|
15
|
-
const { parseSchematron, validate } = require('cda-schematron-validator');
|
|
16
|
-
const schematronString = fs.readFileSync('SchematronFilePath', 'utf-8').toString();
|
|
17
|
-
const schematronDoc = new dom().parseFromString(schematronString);
|
|
18
|
-
const schematronMap = parseSchematron(schematronDoc);
|
|
19
|
-
const options = {
|
|
20
|
-
parsedSchematronMap: schematronMap
|
|
21
|
-
};
|
|
22
|
-
const results = validate('xml string or file path', 'schematron sting, file path or null', options);
|
|
23
|
-
```
|
|
24
|
-
If a schematron map is provided, the schematron string or path will not be read.
|
|
25
|
-
|
|
26
|
-
To install this npm module, it will be
|
|
27
|
-
|
|
28
|
-
```
|
|
29
|
-
npm install cda-schematron-validator
|
|
30
|
-
```
|
|
31
|
-
Mocha VS Code configuratin added to directly execute the test file in `test/tests.js` using VS Code.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
# Original Documentation of the cda-schematron
|
|
35
|
-
# cda-schematron
|
|
36
|
-
|
|
37
|
-
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
|
-
|
|
39
|
-
Check out [cda-schematron-server](https://github.com/ewadkins/cda-schematron-server), a server wrapper of **cda-schematron**, for easy schematron validation.
|
|
40
|
-
|
|
41
|
-
### Validating xml
|
|
42
|
-
```javascript
|
|
43
|
-
var validator = require('cda-schematron');
|
|
44
|
-
|
|
45
|
-
var xmlPath = 'someFile.xml';
|
|
46
|
-
var schematronPath = 'someFile.sch';
|
|
47
|
-
|
|
48
|
-
var fs = require('fs');
|
|
49
|
-
var xml = fs.readFileSync(xmlPath).toString();
|
|
50
|
-
var schematron = fs.readFileSync(schematronPath).toString();
|
|
51
|
-
|
|
52
|
-
var results = validator.validate(xml, schematron);
|
|
53
|
-
```
|
|
54
|
-
File paths can also be passed to the validator directly. The following lines all return the same results:
|
|
55
|
-
```javascript
|
|
56
|
-
var results = validator.validate(xml, schematronPath);
|
|
57
|
-
```
|
|
58
|
-
```javascript
|
|
59
|
-
var results = validator.validate(xmlPath, schematron);
|
|
60
|
-
```
|
|
61
|
-
```javascript
|
|
62
|
-
var results = validator.validate(xmlPath, schematronPath);
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Results
|
|
66
|
-
```results``` is an object containing arrays ```errors```, ```warnings```, and ```ignoreds```.
|
|
67
|
-
|
|
68
|
-
**Errors** and **warnings** are reported as determined by the schematron and test descriptions. They are of the following form:
|
|
69
|
-
```javascript
|
|
70
|
-
{
|
|
71
|
-
type: type, // "error" or "warning"
|
|
72
|
-
test: test, // xpath test
|
|
73
|
-
simplifiedTest: simplifiedTest, // xpath test with resource values included, if applicable, null otherwise
|
|
74
|
-
description: description, // schematron description of the test case
|
|
75
|
-
line: line, // line number of the violating context
|
|
76
|
-
path: path, // xpath path of the violating context
|
|
77
|
-
patternId: patternId, // schematron-assigned pattern id
|
|
78
|
-
ruleId: ruleId, // schematron-assigned rule id
|
|
79
|
-
assertionId: assertionId, // schematron-assigned assertion id
|
|
80
|
-
context: context, // xpath context of the rule
|
|
81
|
-
xml: xml // xml snippet of the violating context
|
|
82
|
-
}
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
**Ignored** tests are those that resulted in an exception while running (eg. the test is invalid xpath and could not be parsed properly) and require manual inspection. They are of the following form:
|
|
86
|
-
```javascript
|
|
87
|
-
{
|
|
88
|
-
errorMessage: errorMessage, // reason for the exception/ignoring the test
|
|
89
|
-
type: type, // "error" or "warning"
|
|
90
|
-
test: test, // xpath test
|
|
91
|
-
simplifiedTest: simplifiedTest, // xpath test with resource values included, if applicable, null otherwise
|
|
92
|
-
description: description, // schematron description of the test case
|
|
93
|
-
patternId: patternId, // schematron-assigned pattern id
|
|
94
|
-
ruleId: ruleId, // schematron-assigned rule id
|
|
95
|
-
assertionId: assertionId, // schematron-assigned assertion id
|
|
96
|
-
context: context, // xpath context of the rule
|
|
97
|
-
}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
### Options
|
|
101
|
-
The ```validate``` function takes in an ```options``` object as an optional third argument. The three fields that can be included in ```options``` are as follows:
|
|
102
|
-
|
|
103
|
-
* **```includeWarnings```**: ```true``` or ```false```, this determines whether or not warnings should be tested and returned. Defaults to ```true```.
|
|
104
|
-
|
|
105
|
-
* **```resourceDir```**: the path to a directory containing resource files (eg. voc.xml) which may be necessary for some schematron tests. Defaults to ```'./'```, the current directory.
|
|
106
|
-
|
|
107
|
-
* **```xmlSnippetMaxLength```**: an integer, which is the maximum length of the ```xml``` field in validation results. Defaults to ```200```. Set to ```0``` for unlimited length.
|
|
108
|
-
|
|
109
|
-
Here is an example with warnings disabled:
|
|
110
|
-
|
|
111
|
-
```javascript
|
|
112
|
-
var results = validator.validate(xml, schematron, { includeWarnings: false });
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### Cache
|
|
116
|
-
The validator uses a cache to store parsed schematrons, an intermediate data structure used to store revelant schematron information. This reduces the runtime of the validator when validating against the same schematron multiple times. You can clear the cache at any time with:
|
|
117
|
-
```javascript
|
|
118
|
-
validator.clearCache();
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
---
|
|
122
|
-
## License (MIT)
|
|
123
|
-
|
|
124
|
-
Copyright © 2017 [Eric Wadkins](http://www.ericwadkins.com/)
|
|
125
|
-
|
|
126
|
-
Copyright © 2022 [Priyaranjan (Raj) Tokachichu](https://github.com/priyaranjan-tokachichu)
|
|
127
|
-
|
|
128
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
129
|
-
|
|
130
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
131
|
-
|
|
132
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
# cda-schematron-validator
|
|
2
|
+
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
|
+
|
|
4
|
+
This is just a cosmetic change with updated node modules, a couple of minor bugs observed, and esversion 6 changes.
|
|
5
|
+
|
|
6
|
+
The changes are fully backward compatible. So, leaving the original documentation as is below.
|
|
7
|
+
|
|
8
|
+
### New option parameter parsedSchematronMap
|
|
9
|
+
In addition to everything that was possible,
|
|
10
|
+
you can now supply a new option parameter `parsedSchematronMap`. You can create a schematron map as follows.
|
|
11
|
+
|
|
12
|
+
```javascript
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const dom = require('@xmldom/xmldom').DOMParser;
|
|
15
|
+
const { parseSchematron, validate } = require('cda-schematron-validator');
|
|
16
|
+
const schematronString = fs.readFileSync('SchematronFilePath', 'utf-8').toString();
|
|
17
|
+
const schematronDoc = new dom().parseFromString(schematronString);
|
|
18
|
+
const schematronMap = parseSchematron(schematronDoc);
|
|
19
|
+
const options = {
|
|
20
|
+
parsedSchematronMap: schematronMap
|
|
21
|
+
};
|
|
22
|
+
const results = validate('xml string or file path', 'schematron sting, file path or null', options);
|
|
23
|
+
```
|
|
24
|
+
If a schematron map is provided, the schematron string or path will not be read.
|
|
25
|
+
|
|
26
|
+
To install this npm module, it will be
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
npm install cda-schematron-validator
|
|
30
|
+
```
|
|
31
|
+
Mocha VS Code configuratin added to directly execute the test file in `test/tests.js` using VS Code.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# Original Documentation of the cda-schematron
|
|
35
|
+
# cda-schematron
|
|
36
|
+
|
|
37
|
+
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
|
+
|
|
39
|
+
Check out [cda-schematron-server](https://github.com/ewadkins/cda-schematron-server), a server wrapper of **cda-schematron**, for easy schematron validation.
|
|
40
|
+
|
|
41
|
+
### Validating xml
|
|
42
|
+
```javascript
|
|
43
|
+
var validator = require('cda-schematron');
|
|
44
|
+
|
|
45
|
+
var xmlPath = 'someFile.xml';
|
|
46
|
+
var schematronPath = 'someFile.sch';
|
|
47
|
+
|
|
48
|
+
var fs = require('fs');
|
|
49
|
+
var xml = fs.readFileSync(xmlPath).toString();
|
|
50
|
+
var schematron = fs.readFileSync(schematronPath).toString();
|
|
51
|
+
|
|
52
|
+
var results = validator.validate(xml, schematron);
|
|
53
|
+
```
|
|
54
|
+
File paths can also be passed to the validator directly. The following lines all return the same results:
|
|
55
|
+
```javascript
|
|
56
|
+
var results = validator.validate(xml, schematronPath);
|
|
57
|
+
```
|
|
58
|
+
```javascript
|
|
59
|
+
var results = validator.validate(xmlPath, schematron);
|
|
60
|
+
```
|
|
61
|
+
```javascript
|
|
62
|
+
var results = validator.validate(xmlPath, schematronPath);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Results
|
|
66
|
+
```results``` is an object containing arrays ```errors```, ```warnings```, and ```ignoreds```.
|
|
67
|
+
|
|
68
|
+
**Errors** and **warnings** are reported as determined by the schematron and test descriptions. They are of the following form:
|
|
69
|
+
```javascript
|
|
70
|
+
{
|
|
71
|
+
type: type, // "error" or "warning"
|
|
72
|
+
test: test, // xpath test
|
|
73
|
+
simplifiedTest: simplifiedTest, // xpath test with resource values included, if applicable, null otherwise
|
|
74
|
+
description: description, // schematron description of the test case
|
|
75
|
+
line: line, // line number of the violating context
|
|
76
|
+
path: path, // xpath path of the violating context
|
|
77
|
+
patternId: patternId, // schematron-assigned pattern id
|
|
78
|
+
ruleId: ruleId, // schematron-assigned rule id
|
|
79
|
+
assertionId: assertionId, // schematron-assigned assertion id
|
|
80
|
+
context: context, // xpath context of the rule
|
|
81
|
+
xml: xml // xml snippet of the violating context
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Ignored** tests are those that resulted in an exception while running (eg. the test is invalid xpath and could not be parsed properly) and require manual inspection. They are of the following form:
|
|
86
|
+
```javascript
|
|
87
|
+
{
|
|
88
|
+
errorMessage: errorMessage, // reason for the exception/ignoring the test
|
|
89
|
+
type: type, // "error" or "warning"
|
|
90
|
+
test: test, // xpath test
|
|
91
|
+
simplifiedTest: simplifiedTest, // xpath test with resource values included, if applicable, null otherwise
|
|
92
|
+
description: description, // schematron description of the test case
|
|
93
|
+
patternId: patternId, // schematron-assigned pattern id
|
|
94
|
+
ruleId: ruleId, // schematron-assigned rule id
|
|
95
|
+
assertionId: assertionId, // schematron-assigned assertion id
|
|
96
|
+
context: context, // xpath context of the rule
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Options
|
|
101
|
+
The ```validate``` function takes in an ```options``` object as an optional third argument. The three fields that can be included in ```options``` are as follows:
|
|
102
|
+
|
|
103
|
+
* **```includeWarnings```**: ```true``` or ```false```, this determines whether or not warnings should be tested and returned. Defaults to ```true```.
|
|
104
|
+
|
|
105
|
+
* **```resourceDir```**: the path to a directory containing resource files (eg. voc.xml) which may be necessary for some schematron tests. Defaults to ```'./'```, the current directory.
|
|
106
|
+
|
|
107
|
+
* **```xmlSnippetMaxLength```**: an integer, which is the maximum length of the ```xml``` field in validation results. Defaults to ```200```. Set to ```0``` for unlimited length.
|
|
108
|
+
|
|
109
|
+
Here is an example with warnings disabled:
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
var results = validator.validate(xml, schematron, { includeWarnings: false });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Cache
|
|
116
|
+
The validator uses a cache to store parsed schematrons, an intermediate data structure used to store revelant schematron information. This reduces the runtime of the validator when validating against the same schematron multiple times. You can clear the cache at any time with:
|
|
117
|
+
```javascript
|
|
118
|
+
validator.clearCache();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
## License (MIT)
|
|
123
|
+
|
|
124
|
+
Copyright © 2017 [Eric Wadkins](http://www.ericwadkins.com/)
|
|
125
|
+
|
|
126
|
+
Copyright © 2022 [Priyaranjan (Raj) Tokachichu](https://github.com/priyaranjan-tokachichu)
|
|
127
|
+
|
|
128
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
129
|
+
|
|
130
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
131
|
+
|
|
132
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|