@glion/lint-profile-segment-order 0.22.0 → 0.23.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/README.md +21 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -168,6 +168,27 @@ Unexpected segment 'PID'. Expected: EVN, SFT
|
|
|
168
168
|
|
|
169
169
|
The offending segment name and the segments valid at that position, sorted, are interpolated.
|
|
170
170
|
|
|
171
|
+
### Invalid — segment after the end of the message
|
|
172
|
+
|
|
173
|
+
`DSC` ends an `ORU_R01`, so no segment may follow it:
|
|
174
|
+
|
|
175
|
+
```hl7
|
|
176
|
+
MSH|^~\&|LAB|FAC|EMR|RFAC|20250601120000||ORU^R01^ORU_R01|MSG00001|P|2.5
|
|
177
|
+
PID|1||PATID1234^^^HOSP^MR||DOE^JANE||19800101|F
|
|
178
|
+
OBR|1|ORD1||CBC
|
|
179
|
+
OBX|1|NM|WBC||5.0
|
|
180
|
+
DSC|1
|
|
181
|
+
PID|2||PATID5678^^^HOSP^MR||DOE^JOHN||19750101|M
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Reported message:
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
Unexpected segment 'PID'
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
When no segment is valid at that position, the message lists none.
|
|
191
|
+
|
|
171
192
|
### Invalid — message ended prematurely
|
|
172
193
|
|
|
173
194
|
All segments were consumed but the schema still requires more; an `ADT_A01` needs a `PV1`:
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,+
|
|
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,+DAoD1B,CAAC;AAEF,eAAe,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -71,7 +71,8 @@ const hl7v2LintSegmentOrder = lintRule({ origin: "hl7v2-lint:segment-order" }, a
|
|
|
71
71
|
case "matched": break;
|
|
72
72
|
case "mismatched": {
|
|
73
73
|
const { ancestors, node } = segments[result.index];
|
|
74
|
-
|
|
74
|
+
const reason = result.expected.length > 0 ? `Unexpected segment '${node.name}'. Expected: ${result.expected.join(", ")}` : `Unexpected segment '${node.name}'`;
|
|
75
|
+
file.message(reason, {
|
|
75
76
|
ancestors,
|
|
76
77
|
place: node.position
|
|
77
78
|
});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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
|
|
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 const reason =\n result.expected.length > 0\n ? `Unexpected segment '${node.name}'. Expected: ${result.expected.join(\", \")}`\n : `Unexpected segment '${node.name}'`;\n file.message(reason, { ancestors, place: node.position });\n break;\n }\n case \"incomplete\": {\n file.message(\n `Message ended prematurely. Expected: ${result.expected.join(\", \")}`,\n { ancestors: [tree], place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AK,IAClE;IAAE,WAAW,CAAC,KAAK;IAAE,OAAO,KAAK;IAAU,CAC5C;AACD;;EAIP"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glion/lint-profile-segment-order",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "Lints the segment order of an HL7v2 message against its message structure",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"health",
|
|
@@ -35,10 +35,10 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"unified-lint-rule": "3.0.1",
|
|
37
37
|
"vfile": "6.0.3",
|
|
38
|
-
"@glion/ast": "0.
|
|
39
|
-
"@glion/
|
|
40
|
-
"@glion/
|
|
41
|
-
"@glion/util-visit": "0.
|
|
38
|
+
"@glion/ast": "0.23.0",
|
|
39
|
+
"@glion/profiles": "0.23.0",
|
|
40
|
+
"@glion/util-query": "0.23.0",
|
|
41
|
+
"@glion/util-visit": "0.23.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@types/node": "25.6.0",
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
"typescript": "6.0.3",
|
|
49
49
|
"unified": "^11.0.5",
|
|
50
50
|
"vitest": "4.1.11",
|
|
51
|
-
"@glion/builder": "0.
|
|
51
|
+
"@glion/builder": "0.23.0",
|
|
52
52
|
"@glion/check-readme": "0.18.0",
|
|
53
|
-
"@glion/parser": "0.
|
|
53
|
+
"@glion/parser": "0.23.0",
|
|
54
54
|
"@glion/testing": "0.18.0",
|
|
55
55
|
"@glion/tsconfig": "0.18.0"
|
|
56
56
|
},
|