@glion/lint-profile-segment-order 0.0.0-stage → 0.21.0
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/LICENSE +21 -0
- package/README.md +197 -2
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +91 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Rethink Health
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,198 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @glion/lint-profile-segment-order
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lint rule that validates HL7v2 segment order against the event schema defined by the profile.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Walks the parsed tree segment-by-segment, feeding each segment name to a runner over the event schema from `@glion/profiles`. Reports one message for the first segment that is not valid at its position, or for a message that ends before the schema is complete. When no `definition` is given, the rule resolves the schema from MSH-9 and MSH-12.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @glion/lint-profile-segment-order
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { hl7v2Parser } from "@glion/parser";
|
|
19
|
+
import hl7v2LintSegmentOrder from "@glion/lint-profile-segment-order";
|
|
20
|
+
import { unified } from "unified";
|
|
21
|
+
import { reporter } from "vfile-reporter";
|
|
22
|
+
|
|
23
|
+
const message = [
|
|
24
|
+
"MSH|^~\\&|SENDER|FAC|RECV|RFAC|20250601120000||ADT^A01^ADT_A01|MSG00001|P|2.5",
|
|
25
|
+
"EVN|A01|20250601120000",
|
|
26
|
+
"PID|1||PATID1234^^^HOSP^MR||DOE^JANE||19800101|F",
|
|
27
|
+
].join("\r");
|
|
28
|
+
|
|
29
|
+
const file = await unified()
|
|
30
|
+
.use(hl7v2Parser)
|
|
31
|
+
.use(hl7v2LintSegmentOrder)
|
|
32
|
+
.process(message);
|
|
33
|
+
|
|
34
|
+
console.error(reporter([file]));
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
With an event schema of your own:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import hl7v2LintSegmentOrder from "@glion/lint-profile-segment-order";
|
|
41
|
+
import type { EventSchema } from "@glion/profiles";
|
|
42
|
+
import { unified } from "unified";
|
|
43
|
+
|
|
44
|
+
const ADT_A01_SITE: EventSchema = {
|
|
45
|
+
id: "ADT_A01_SITE",
|
|
46
|
+
elements: [
|
|
47
|
+
{ type: "segment", name: "MSH", optional: false, repeating: false },
|
|
48
|
+
{ type: "segment", name: "EVN", optional: false, repeating: false },
|
|
49
|
+
{ type: "segment", name: "PID", optional: false, repeating: false },
|
|
50
|
+
{ type: "segment", name: "ZPD", optional: true, repeating: false },
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const processor = unified().use(hl7v2LintSegmentOrder, {
|
|
55
|
+
definition: ADT_A01_SITE,
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
With a schema chosen per message, and the one MSH-9 names otherwise:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import hl7v2LintSegmentOrder from "@glion/lint-profile-segment-order";
|
|
63
|
+
import { value } from "@glion/util-query";
|
|
64
|
+
import { unified } from "unified";
|
|
65
|
+
|
|
66
|
+
const processor = unified().use(hl7v2LintSegmentOrder, {
|
|
67
|
+
definition: ({ tree }) =>
|
|
68
|
+
value(tree, "MSH-4")?.value === "SITE_A" ? ADT_A01_SITE : undefined,
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Reporting every Z-segment the schema does not name:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import hl7v2LintSegmentOrder from "@glion/lint-profile-segment-order";
|
|
76
|
+
import { unified } from "unified";
|
|
77
|
+
|
|
78
|
+
const processor = unified().use(hl7v2LintSegmentOrder, {
|
|
79
|
+
allowZSegments: false,
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
### `unified().use(hl7v2LintSegmentOrder[, options])`
|
|
86
|
+
|
|
87
|
+
A `unified` lint rule plugin.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import type { Plugin } from "unified";
|
|
91
|
+
import type { Root } from "@glion/ast";
|
|
92
|
+
import type { EventSchema } from "@glion/profiles";
|
|
93
|
+
import type { VFile } from "vfile";
|
|
94
|
+
|
|
95
|
+
export interface SegmentOrderContext {
|
|
96
|
+
tree: Root;
|
|
97
|
+
file: VFile;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface SegmentOrderOptions {
|
|
101
|
+
/**
|
|
102
|
+
* The event schema to validate against, or a function that returns the
|
|
103
|
+
* one to use for a message. Default: the schema MSH-9 names.
|
|
104
|
+
*
|
|
105
|
+
* When the function returns `undefined`, the rule uses the schema MSH-9
|
|
106
|
+
* names. Return the same object for every message that uses a schema: a
|
|
107
|
+
* new object is compiled on its first run.
|
|
108
|
+
*/
|
|
109
|
+
definition?:
|
|
110
|
+
| EventSchema
|
|
111
|
+
| ((
|
|
112
|
+
context: SegmentOrderContext
|
|
113
|
+
) => EventSchema | undefined | Promise<EventSchema | undefined>);
|
|
114
|
+
/**
|
|
115
|
+
* Whether a Z-segment (a segment ID that starts with `Z`) that the event
|
|
116
|
+
* schema does not name fits at any position. HL7v2 allows local Z-segments
|
|
117
|
+
* in any message and segment group (v2.5.1 §2.11). When `false`, the rule
|
|
118
|
+
* reports such a segment as unexpected. Default: `true`.
|
|
119
|
+
*/
|
|
120
|
+
allowZSegments?: boolean;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
declare const hl7v2LintSegmentOrder: Plugin<[SegmentOrderOptions?], Root>;
|
|
124
|
+
export default hl7v2LintSegmentOrder;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
All messages use `ruleId: "segment-order"` and `source: "hl7v2-lint"`. The rule reports at most one order error per message: the first segment the schema does not allow.
|
|
128
|
+
|
|
129
|
+
## What it checks
|
|
130
|
+
|
|
131
|
+
Segments must appear in an order the event schema allows, and the message must include every segment the schema requires. A Z-segment the schema does not name may appear anywhere, unless `allowZSegments` is `false`; a Z-segment the schema names must appear where the schema allows it.
|
|
132
|
+
|
|
133
|
+
### Valid
|
|
134
|
+
|
|
135
|
+
An `ADT_A01` message whose segments follow the schema defined for v2.5:
|
|
136
|
+
|
|
137
|
+
```hl7
|
|
138
|
+
MSH|^~\&|SENDER|FAC|RECV|RFAC|20250601120000||ADT^A01^ADT_A01|MSG00001|P|2.5
|
|
139
|
+
EVN|A01|20250601120000
|
|
140
|
+
PID|1||PATID1234^^^HOSP^MR||DOE^JANE||19800101|F
|
|
141
|
+
PV1|1|I|WARD^101^1
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The same message with a site's Z-segment, which the schema does not name:
|
|
145
|
+
|
|
146
|
+
```hl7
|
|
147
|
+
MSH|^~\&|SENDER|FAC|RECV|RFAC|20250601120000||ADT^A01^ADT_A01|MSG00001|P|2.5
|
|
148
|
+
EVN|A01|20250601120000
|
|
149
|
+
PID|1||PATID1234^^^HOSP^MR||DOE^JANE||19800101|F
|
|
150
|
+
ZPD|1|site patient data
|
|
151
|
+
PV1|1|I|WARD^101^1
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Invalid — unexpected segment
|
|
155
|
+
|
|
156
|
+
`PID` appears before `EVN` in an `ADT_A01`:
|
|
157
|
+
|
|
158
|
+
```hl7
|
|
159
|
+
MSH|^~\&|SENDER|FAC|RECV|RFAC|20250601120000||ADT^A01^ADT_A01|MSG00001|P|2.5
|
|
160
|
+
PID|1||PATID1234^^^HOSP^MR||DOE^JANE||19800101|F
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Reported message:
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
Unexpected segment 'PID'. Expected: EVN, SFT
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The offending segment name and the segments valid at that position, sorted, are interpolated.
|
|
170
|
+
|
|
171
|
+
### Invalid — message ended prematurely
|
|
172
|
+
|
|
173
|
+
All segments were consumed but the schema still requires more; an `ADT_A01` needs a `PV1`:
|
|
174
|
+
|
|
175
|
+
```hl7
|
|
176
|
+
MSH|^~\&|SENDER|FAC|RECV|RFAC|20250601120000||ADT^A01^ADT_A01|MSG00001|P|2.5
|
|
177
|
+
EVN|A01|20250601120000
|
|
178
|
+
PID|1||PATID1234^^^HOSP^MR||DOE^JANE||19800101|F
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Reported message:
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
Message ended prematurely. Expected: NK1, PD1, PV1, ROL
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
The list is the segments valid after the last one, sorted. Only reported when no other validation error was emitted.
|
|
188
|
+
|
|
189
|
+
### No schema
|
|
190
|
+
|
|
191
|
+
When no `definition` is given, or a `definition` function returns `undefined`, the rule uses the schema MSH-9 and MSH-12 name; when they name no bundled schema, it reports nothing.
|
|
192
|
+
|
|
193
|
+
## Part of Glion
|
|
194
|
+
|
|
195
|
+
`@glion/lint-profile-segment-order` is part of **[Glion]**, the application framework for HL7v2. See the [Glion README] for the full package catalog and architecture.
|
|
196
|
+
|
|
197
|
+
[Glion]: https://github.com/rethinkhealth/glion#readme
|
|
198
|
+
[Glion README]: https://github.com/rethinkhealth/glion#readme
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Root } from "@glion/ast";
|
|
2
|
+
import type { EventSchema } from "@glion/profiles";
|
|
3
|
+
import type { VFile } from "vfile";
|
|
4
|
+
/** The message a `definition` function chooses an event schema for. */
|
|
5
|
+
export interface SegmentOrderContext {
|
|
6
|
+
tree: Root;
|
|
7
|
+
file: VFile;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Options for the segment order lint rule.
|
|
11
|
+
*/
|
|
12
|
+
export interface SegmentOrderOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The event schema to validate against, or a function that returns the
|
|
15
|
+
* one to use for a message. Default: the schema MSH-9 names.
|
|
16
|
+
*
|
|
17
|
+
* When the function returns `undefined`, the rule uses the schema MSH-9
|
|
18
|
+
* names. Return the same object for every message that uses a schema: a
|
|
19
|
+
* new object is compiled on its first run.
|
|
20
|
+
*/
|
|
21
|
+
definition?: EventSchema | ((context: SegmentOrderContext) => EventSchema | undefined | Promise<EventSchema | undefined>);
|
|
22
|
+
/**
|
|
23
|
+
* Whether a Z-segment (a segment ID that starts with `Z`) that the event
|
|
24
|
+
* schema does not name fits at any position. HL7v2 allows local Z-segments
|
|
25
|
+
* in any message and segment group (v2.5.1 §2.11). When `false`, the rule
|
|
26
|
+
* reports such a segment as unexpected. Default: `true`.
|
|
27
|
+
*/
|
|
28
|
+
allowZSegments?: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare const hl7v2LintSegmentOrder: import("unified-lint-rule").Plugin<Root, SegmentOrderOptions>;
|
|
31
|
+
export default hl7v2LintSegmentOrder;
|
|
32
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAS,IAAI,EAAW,MAAM,YAAY,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAKnD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAEnC,uEAAuE;AACvE,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,KAAK,CAAC;CACb;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;;;;OAOG;IACH,UAAU,CAAC,EACP,WAAW,GACX,CAAC,CACC,OAAO,EAAE,mBAAmB,KACzB,WAAW,GAAG,SAAS,GAAG,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAC,CAAC;IACrE;;;;;OAKG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAuDD,QAAA,MAAM,qBAAqB,+DAmD1B,CAAC;AAEF,eAAe,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { profiles, runner } from "@glion/profiles";
|
|
2
|
+
import { value } from "@glion/util-query";
|
|
3
|
+
import { SKIP, visit } from "@glion/util-visit";
|
|
4
|
+
import { lintRule } from "unified-lint-rule";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* Lint rule that validates HL7v2 segment order against event schema
|
|
8
|
+
* profiles.
|
|
9
|
+
*
|
|
10
|
+
* Verifies each segment appears in the order the event schema defines.
|
|
11
|
+
*
|
|
12
|
+
* **Resolution**: If no `definition` is provided, the rule resolves the
|
|
13
|
+
* schema from MSH-12 (version) and MSH-9.3 (event schema), or MSH-9.1
|
|
14
|
+
* and MSH-9.2 when MSH-9.3 is empty. If the schema is unavailable, the rule
|
|
15
|
+
* reports nothing.
|
|
16
|
+
*
|
|
17
|
+
* **Behavior**: Reports at most one order error per message: the first segment
|
|
18
|
+
* the schema does not allow. A Z-segment the schema does not name is allowed
|
|
19
|
+
* anywhere unless `allowZSegments` is `false`.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // With the schema MSH-9 names:
|
|
24
|
+
* unified().use(hl7v2LintSegmentOrder);
|
|
25
|
+
*
|
|
26
|
+
* // With a schema of your own:
|
|
27
|
+
* unified().use(hl7v2LintSegmentOrder, {
|
|
28
|
+
* definition: {
|
|
29
|
+
* elements: [
|
|
30
|
+
* { name: "MSH", optional: false, repeating: false, type: "segment" },
|
|
31
|
+
* { name: "PID", optional: false, repeating: false, type: "segment" },
|
|
32
|
+
* ],
|
|
33
|
+
* id: "ADT_SITE",
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // With a schema chosen per message, and the one MSH-9 names otherwise:
|
|
38
|
+
* unified().use(hl7v2LintSegmentOrder, {
|
|
39
|
+
* definition: ({ tree }) =>
|
|
40
|
+
* isSiteMessage(tree) ? SITE_STRUCTURE : undefined,
|
|
41
|
+
* });
|
|
42
|
+
* ```;
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* The event schema MSH-12 and MSH-9 name: MSH-9.3, or MSH-9.1 and MSH-9.2,
|
|
46
|
+
* which the events store resolves through the version's event map.
|
|
47
|
+
*/
|
|
48
|
+
const schemaOf = async (tree) => {
|
|
49
|
+
const version = value(tree, "MSH-12.1")?.value;
|
|
50
|
+
if (!version) return;
|
|
51
|
+
const id = value(tree, "MSH-9.3")?.value || `${value(tree, "MSH-9.1")?.value ?? ""}_${value(tree, "MSH-9.2")?.value ?? ""}`;
|
|
52
|
+
return await profiles.events.load(version, id);
|
|
53
|
+
};
|
|
54
|
+
const hl7v2LintSegmentOrder = lintRule({ origin: "hl7v2-lint:segment-order" }, async (tree, file, options) => {
|
|
55
|
+
const definition = options?.definition;
|
|
56
|
+
const schema = (typeof definition === "function" ? await definition({
|
|
57
|
+
file,
|
|
58
|
+
tree
|
|
59
|
+
}) : definition) ?? await schemaOf(tree);
|
|
60
|
+
if (!schema) return;
|
|
61
|
+
const segments = [];
|
|
62
|
+
visit(tree, "segment", (node, parents) => {
|
|
63
|
+
segments.push({
|
|
64
|
+
ancestors: [...parents, node],
|
|
65
|
+
node
|
|
66
|
+
});
|
|
67
|
+
return SKIP;
|
|
68
|
+
});
|
|
69
|
+
const result = runner(schema, segments.map(({ node }) => node.name), { allowZSegments: options?.allowZSegments });
|
|
70
|
+
switch (result.type) {
|
|
71
|
+
case "matched": break;
|
|
72
|
+
case "mismatched": {
|
|
73
|
+
const { ancestors, node } = segments[result.index];
|
|
74
|
+
file.message(`Unexpected segment '${node.name}'. Expected: ${result.expected.join(", ")}`, {
|
|
75
|
+
ancestors,
|
|
76
|
+
place: node.position
|
|
77
|
+
});
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "incomplete":
|
|
81
|
+
file.message(`Message ended prematurely. Expected: ${result.expected.join(", ")}`, {
|
|
82
|
+
ancestors: [tree],
|
|
83
|
+
place: tree.position
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
//#endregion
|
|
89
|
+
export { hl7v2LintSegmentOrder as default };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Nodes, Root, Segment } from \"@glion/ast\";\nimport type { EventSchema } from \"@glion/profiles\";\nimport { profiles, runner } from \"@glion/profiles\";\nimport { value } from \"@glion/util-query\";\nimport { SKIP, visit } from \"@glion/util-visit\";\nimport { lintRule } from \"unified-lint-rule\";\nimport type { VFile } from \"vfile\";\n\n/** The message a `definition` function chooses an event schema for. */\nexport interface SegmentOrderContext {\n tree: Root;\n file: VFile;\n}\n\n/**\n * Options for the segment order lint rule.\n */\nexport interface SegmentOrderOptions {\n /**\n * The event schema to validate against, or a function that returns the\n * one to use for a message. Default: the schema MSH-9 names.\n *\n * When the function returns `undefined`, the rule uses the schema MSH-9\n * names. Return the same object for every message that uses a schema: a\n * new object is compiled on its first run.\n */\n definition?:\n | EventSchema\n | ((\n context: SegmentOrderContext\n ) => EventSchema | undefined | Promise<EventSchema | undefined>);\n /**\n * Whether a Z-segment (a segment ID that starts with `Z`) that the event\n * schema does not name fits at any position. HL7v2 allows local Z-segments\n * in any message and segment group (v2.5.1 §2.11). When `false`, the rule\n * reports such a segment as unexpected. Default: `true`.\n */\n allowZSegments?: boolean;\n}\n\n/**\n * Lint rule that validates HL7v2 segment order against event schema\n * profiles.\n *\n * Verifies each segment appears in the order the event schema defines.\n *\n * **Resolution**: If no `definition` is provided, the rule resolves the\n * schema from MSH-12 (version) and MSH-9.3 (event schema), or MSH-9.1\n * and MSH-9.2 when MSH-9.3 is empty. If the schema is unavailable, the rule\n * reports nothing.\n *\n * **Behavior**: Reports at most one order error per message: the first segment\n * the schema does not allow. A Z-segment the schema does not name is allowed\n * anywhere unless `allowZSegments` is `false`.\n *\n * @example\n * ```typescript\n * // With the schema MSH-9 names:\n * unified().use(hl7v2LintSegmentOrder);\n *\n * // With a schema of your own:\n * unified().use(hl7v2LintSegmentOrder, {\n * definition: {\n * elements: [\n * { name: \"MSH\", optional: false, repeating: false, type: \"segment\" },\n * { name: \"PID\", optional: false, repeating: false, type: \"segment\" },\n * ],\n * id: \"ADT_SITE\",\n * },\n * });\n *\n * // With a schema chosen per message, and the one MSH-9 names otherwise:\n * unified().use(hl7v2LintSegmentOrder, {\n * definition: ({ tree }) =>\n * isSiteMessage(tree) ? SITE_STRUCTURE : undefined,\n * });\n * ```;\n */\n/**\n * The event schema MSH-12 and MSH-9 name: MSH-9.3, or MSH-9.1 and MSH-9.2,\n * which the events store resolves through the version's event map.\n */\nconst schemaOf = async (tree: Root): Promise<EventSchema | undefined> => {\n const version = value(tree, \"MSH-12.1\")?.value;\n if (!version) {\n return;\n }\n const id =\n value(tree, \"MSH-9.3\")?.value ||\n `${value(tree, \"MSH-9.1\")?.value ?? \"\"}_${value(tree, \"MSH-9.2\")?.value ?? \"\"}`;\n return await profiles.events.load(version, id);\n};\n\nconst hl7v2LintSegmentOrder = lintRule<Root, SegmentOrderOptions>(\n {\n origin: \"hl7v2-lint:segment-order\",\n },\n async (tree, file, options) => {\n const definition = options?.definition;\n const chosen =\n typeof definition === \"function\"\n ? await definition({ file, tree })\n : definition;\n const schema = chosen ?? (await schemaOf(tree));\n\n if (!schema) {\n return;\n }\n\n const segments: { node: Segment; ancestors: Nodes[] }[] = [];\n visit(tree, \"segment\", (node, parents) => {\n segments.push({ ancestors: [...parents, node], node });\n return SKIP;\n });\n\n const result = runner(\n schema,\n segments.map(({ node }) => node.name),\n { allowZSegments: options?.allowZSegments }\n );\n\n switch (result.type) {\n case \"matched\": {\n break;\n }\n case \"mismatched\": {\n const { ancestors, node } = segments[\n result.index\n ] as (typeof segments)[number];\n file.message(\n `Unexpected segment '${node.name}'. Expected: ${result.expected.join(\", \")}`,\n { ancestors, place: node.position }\n );\n break;\n }\n case \"incomplete\": {\n file.message(\n `Message ended prematurely. Expected: ${result.expected.join(\", \")}`,\n { ancestors: [tree], place: tree.position }\n );\n break;\n }\n }\n }\n);\n\nexport default hl7v2LintSegmentOrder;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkFA,MAAM,WAAW,OAAO,SAAiD;CACvE,MAAM,UAAU,MAAM,MAAM,WAAW,EAAE;AACzC,KAAI,CAAC,QACH;CAEF,MAAM,KACJ,MAAM,MAAM,UAAU,EAAE,SACxB,GAAG,MAAM,MAAM,UAAU,EAAE,SAAS,GAAG,GAAG,MAAM,MAAM,UAAU,EAAE,SAAS;AAC7E,QAAO,MAAM,SAAS,OAAO,KAAK,SAAS,GAAG;;AAGhD,MAAM,wBAAwB,SAC5B,EACE,QAAQ,4BACT,EACD,OAAO,MAAM,MAAM,YAAY;CAC7B,MAAM,aAAa,SAAS;CAK5B,MAAM,UAHJ,OAAO,eAAe,aAClB,MAAM,WAAW;EAAE;EAAM;EAAM,CAAC,GAChC,eACoB,MAAM,SAAS,KAAK;AAE9C,KAAI,CAAC,OACH;CAGF,MAAM,WAAoD,EAAE;AAC5D,OAAM,MAAM,YAAY,MAAM,YAAY;AACxC,WAAS,KAAK;GAAE,WAAW,CAAC,GAAG,SAAS,KAAK;GAAE;GAAM,CAAC;AACtD,SAAO;GACP;CAEF,MAAM,SAAS,OACb,QACA,SAAS,KAAK,EAAE,WAAW,KAAK,KAAK,EACrC,EAAE,gBAAgB,SAAS,gBAAgB,CAC5C;AAED,SAAQ,OAAO,MAAf;EACE,KAAK,UACH;EAEF,KAAK,cAAc;GACjB,MAAM,EAAE,WAAW,SAAS,SAC1B,OAAO;AAET,QAAK,QACH,uBAAuB,KAAK,KAAK,eAAe,OAAO,SAAS,KAAK,KAAK,IAC1E;IAAE;IAAW,OAAO,KAAK;IAAU,CACpC;AACD;;EAEF,KAAK;AACH,QAAK,QACH,wCAAwC,OAAO,SAAS,KAAK,KAAK,IAClE;IAAE,WAAW,CAAC,KAAK;IAAE,OAAO,KAAK;IAAU,CAC5C;AACD;;EAIP"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glion/lint-profile-segment-order",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.21.0",
|
|
4
|
+
"description": "Lints the segment order of an HL7v2 message against its message structure",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"health",
|
|
7
|
+
"healthcare",
|
|
8
|
+
"hl7",
|
|
9
|
+
"hl7v2",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://glion.dev",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Melek Somai",
|
|
17
|
+
"email": "melek@rethinkhealth.io"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/rethinkhealth/glion.git",
|
|
22
|
+
"directory": "packages/lint-profile-segment-order"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"unified-lint-rule": "3.0.1",
|
|
37
|
+
"vfile": "6.0.3",
|
|
38
|
+
"@glion/ast": "0.21.0",
|
|
39
|
+
"@glion/util-query": "0.21.0",
|
|
40
|
+
"@glion/util-visit": "0.21.0",
|
|
41
|
+
"@glion/profiles": "0.21.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "25.6.0",
|
|
45
|
+
"@vitest/coverage-v8": "4.1.11",
|
|
46
|
+
"publint": "0.3.18",
|
|
47
|
+
"tsdown": "0.21.10",
|
|
48
|
+
"typescript": "6.0.3",
|
|
49
|
+
"unified": "^11.0.5",
|
|
50
|
+
"vitest": "4.1.11",
|
|
51
|
+
"@glion/builder": "0.21.0",
|
|
52
|
+
"@glion/check-readme": "0.18.0",
|
|
53
|
+
"@glion/tsconfig": "0.18.0",
|
|
54
|
+
"@glion/testing": "0.18.0",
|
|
55
|
+
"@glion/parser": "0.21.0"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=22"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsdown && tsc --emitDeclarationOnly",
|
|
62
|
+
"check-types": "tsc --noEmit && tsc --noEmit -p tests/tsconfig.json",
|
|
63
|
+
"check:readme": "glion-check-readme",
|
|
64
|
+
"test": "vitest run",
|
|
65
|
+
"test:coverage": "vitest run --coverage",
|
|
66
|
+
"test:watch": "vitest",
|
|
67
|
+
"publint": "publint --strict"
|
|
68
|
+
}
|
|
6
69
|
}
|