@rethinkhealth/hl7v2-lint-required-message-header 0.2.8

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 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 ADDED
@@ -0,0 +1,136 @@
1
+ # @rethinkhealth/hl7v2-lint-required-message-header
2
+
3
+ A [`unified`][github-unified] lint rule for HL7v2 that warns when the message header segment (`MSH`) is missing or not the first segment in a message. This helps ensure all HL7v2 messages begin with the required header, improving interoperability and data quality.
4
+
5
+ HL7v2 messages must always begin with a message header segment, known as `MSH`. This rule checks that the first segment in a message is `MSH` and warns if it is missing or replaced by another segment.
6
+
7
+ ## What is this?
8
+
9
+ This package validates the **segment header** in HL7v2 syntax trees produced by parsers like [`@rethinkhealth/hl7v2-parser`][github-hl7v2-parser].
10
+
11
+ It reports a message when the first segment of an HL7v2 message is not a segment header (MSH).
12
+
13
+ ## When should I use this?
14
+
15
+ Use this rule to enforce canonical HL7v2 segment identifiers across messages, catch typos (e.g., `PID1`, `Obx`, `MS`), and improve downstream processing reliability.
16
+
17
+ ## Presets
18
+
19
+ This plugin is included in the following presets:
20
+
21
+ | Preset | Options |
22
+ | - | - |
23
+ | `@rethinkhealth/hl7v2-lint-recommended` | |
24
+
25
+ ## Install
26
+
27
+ This package is **ESM only**. In Node.js (v16+), install with npm:
28
+
29
+ ```sh
30
+ npm install @rethinkhealth/hl7v2-lint-required-message-header
31
+ ````
32
+
33
+
34
+ ## Use
35
+
36
+ On the API:
37
+
38
+ ```js
39
+ import { unified } from 'unified'
40
+ import { hl7v2Parse } from '@rethinkhealth/hl7v2-parser'
41
+ import hl7v2LintRequiredMessageHeader from '@rethinkhealth/hl7v2-lint-required-message-header'
42
+ import { reporter } from 'vfile-reporter'
43
+
44
+ const msg = `MSH|^~\\&|...`
45
+ const file = await unified()
46
+ .use(hl7v2Parse)
47
+ .use(hl7v2LintRequiredMessageHeader)
48
+ .process(msg)
49
+
50
+ console.error(reporter([file]))
51
+ ```
52
+
53
+
54
+ ## API
55
+
56
+ ### `unified().use(hl7v2LintSegmentHeaderLength[, options])`
57
+
58
+ Warn when a segment header is not a three-letter uppercase code.
59
+
60
+ ###### Returns
61
+
62
+ A `unified` Transformer that adds messages to the file.
63
+
64
+ ## Recommendation
65
+
66
+ This helps ensure that HL7v2 messages are well-formed and can be reliably parsed and processed by downstream systems. If the message does not start with `MSH`, a warning is reported indicating which segment was found instead.
67
+
68
+ **Why is this important?**
69
+
70
+ - The `MSH` segment contains metadata required for routing and interpreting the message.
71
+ - Missing or misplaced `MSH` segments can cause interoperability issues and data loss.
72
+
73
+ **When to disable:**
74
+
75
+ You should only disable this rule if you are intentionally working with non-standard HL7v2 messages that do not require a message header segment, which is rare in practice.
76
+
77
+
78
+ It’s recommended to enable this rule in most pipelines.
79
+
80
+ ## Examples
81
+
82
+ ##### `ok.hl7`
83
+
84
+ ###### In
85
+
86
+ ```hl7
87
+ MSH|^~\&|...
88
+ PID|1||12345^^^HOSP^MR||DOE^JANE||...
89
+ OBX|1|TX|...
90
+ ```
91
+
92
+ ###### Out
93
+
94
+ No messages.
95
+
96
+ ##### `not-ok.hl7`
97
+
98
+ ###### In
99
+
100
+ ```hl7
101
+ PID|1||12345||DOE^JANE||...
102
+ ```
103
+
104
+ ###### Out
105
+
106
+ ```text
107
+ Message header (MSH) segment is required. Received PID instead.
108
+ ```
109
+
110
+ ## Security
111
+
112
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
113
+
114
+
115
+ ## Contributing
116
+
117
+ We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for more details.
118
+
119
+ 1. Fork the repository
120
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
121
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
122
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
123
+ 5. Open a Pull Request
124
+
125
+ ## Code of Conduct
126
+
127
+ To ensure a welcoming and positive environment, we have a [Code of Conduct](../../CODE_OF_CONDUCT.md) that all contributors and participants are expected to adhere to.
128
+
129
+ ## License
130
+
131
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
132
+
133
+ This program is licensed to you under the terms of the [MIT License](https://opensource.org/licenses/MIT). This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the [LICENSE](../../LICENSE) file for details.
134
+
135
+ [github-unified]: https://github.com/unifiedjs/unified
136
+ [github-hl7v2-parser]: https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-parser
@@ -0,0 +1,10 @@
1
+ import type { Node } from '@rethinkhealth/hl7v2-ast';
2
+ /**
3
+ * hl7v2-lint rule to warn when message header segment (MSH) is missing.
4
+ *
5
+ * This rule is useful for ensuring that all messages start with a message
6
+ * header segment.
7
+ */
8
+ declare const hl7v2LintSegmentRequiredMessageHeader: import("unified-lint-rule").Plugin<Node, undefined>;
9
+ export default hl7v2LintSegmentRequiredMessageHeader;
10
+ //# 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,EAAE,IAAI,EAAiB,MAAM,0BAA0B,CAAC;AAIpE;;;;;GAKG;AACH,QAAA,MAAM,qCAAqC,qDA2B1C,CAAC;AAEF,eAAe,qCAAqC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ // src/index.ts
2
+ import { lintRule } from "unified-lint-rule";
3
+ import { SKIP, visitParents } from "unist-util-visit-parents";
4
+ var hl7v2LintSegmentRequiredMessageHeader = lintRule(
5
+ {
6
+ origin: "hl7v2-lint:segment-required-message-header",
7
+ url: "https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-segment-required-message-header#readme"
8
+ },
9
+ (tree, file) => {
10
+ visitParents(tree, (node, parents) => {
11
+ if (node.type === "root") {
12
+ const firstSegment = node.children[0];
13
+ if (firstSegment?.type === "segment") {
14
+ const header = firstSegment.children?.[0]?.children?.[0]?.children?.[0]?.children?.[0]?.value;
15
+ if (header !== "MSH") {
16
+ file.message(
17
+ `Message header (MSH) segment is required. Received ${header} instead.`,
18
+ {
19
+ ancestors: [...parents, node, firstSegment],
20
+ place: firstSegment.position
21
+ }
22
+ );
23
+ }
24
+ }
25
+ } else {
26
+ return SKIP;
27
+ }
28
+ });
29
+ }
30
+ );
31
+ var src_default = hl7v2LintSegmentRequiredMessageHeader;
32
+ export {
33
+ src_default as default
34
+ };
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Node, Root, Segment } from '@rethinkhealth/hl7v2-ast';\nimport { lintRule } from 'unified-lint-rule';\nimport { SKIP, visitParents } from 'unist-util-visit-parents';\n\n/**\n * hl7v2-lint rule to warn when message header segment (MSH) is missing.\n *\n * This rule is useful for ensuring that all messages start with a message\n * header segment.\n */\nconst hl7v2LintSegmentRequiredMessageHeader = lintRule<Node, undefined>(\n {\n origin: 'hl7v2-lint:segment-required-message-header',\n url: 'https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-segment-required-message-header#readme',\n },\n (tree, file) => {\n visitParents(tree, (node, parents) => {\n if (node.type === 'root') {\n const firstSegment = (node as Root).children[0];\n if (firstSegment?.type === 'segment') {\n const header = (firstSegment as Segment).children?.[0]?.children?.[0]\n ?.children?.[0]?.children?.[0]?.value;\n if (header !== 'MSH') {\n file.message(\n `Message header (MSH) segment is required. Received ${header} instead.`,\n {\n ancestors: [...parents, node, firstSegment],\n place: firstSegment.position,\n }\n );\n }\n }\n } else {\n return SKIP;\n }\n });\n }\n);\n\nexport default hl7v2LintSegmentRequiredMessageHeader;\n"],"mappings":";AACA,SAAS,gBAAgB;AACzB,SAAS,MAAM,oBAAoB;AAQnC,IAAM,wCAAwC;AAAA,EAC5C;AAAA,IACE,QAAQ;AAAA,IACR,KAAK;AAAA,EACP;AAAA,EACA,CAAC,MAAM,SAAS;AACd,iBAAa,MAAM,CAAC,MAAM,YAAY;AACpC,UAAI,KAAK,SAAS,QAAQ;AACxB,cAAM,eAAgB,KAAc,SAAS,CAAC;AAC9C,YAAI,cAAc,SAAS,WAAW;AACpC,gBAAM,SAAU,aAAyB,WAAW,CAAC,GAAG,WAAW,CAAC,GAChE,WAAW,CAAC,GAAG,WAAW,CAAC,GAAG;AAClC,cAAI,WAAW,OAAO;AACpB,iBAAK;AAAA,cACH,sDAAsD,MAAM;AAAA,cAC5D;AAAA,gBACE,WAAW,CAAC,GAAG,SAAS,MAAM,YAAY;AAAA,gBAC1C,OAAO,aAAa;AAAA,cACtB;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF,OAAO;AACL,eAAO;AAAA,MACT;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@rethinkhealth/hl7v2-lint-required-message-header",
3
+ "description": "hl7v2-lint rule to warn when message header segment is missing",
4
+ "version": "0.2.8",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Melek Somai",
8
+ "email": "melek@rethinkhealth.io"
9
+ },
10
+ "type": "module",
11
+ "types": "./dist/index.d.ts",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "exports": {
16
+ ".": "./dist/index.js"
17
+ },
18
+ "dependencies": {
19
+ "unified": "11.0.1",
20
+ "unified-lint-rule": "^3.0.0",
21
+ "unist-util-visit-parents": "^6.0.1"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "22.15.31",
25
+ "@types/unist": "^3.0.3",
26
+ "@vitest/coverage-c8": "^0.33.0",
27
+ "@vitest/coverage-v8": "^3.2.4",
28
+ "tsup": "8.5.0",
29
+ "typescript": "^5.8.3",
30
+ "vfile-reporter": "^8.1.1",
31
+ "vitest": "^3.2.4",
32
+ "@rethinkhealth/hl7v2": "0.2.8",
33
+ "@rethinkhealth/testing": "0.0.0",
34
+ "@rethinkhealth/hl7v2-ast": "0.2.8",
35
+ "@rethinkhealth/tsconfig": "0.0.0"
36
+ },
37
+ "repository": "rethinkhealth/hl7v2.git",
38
+ "homepage": "https://www.rethinkhealth.io/hl7v2/docs",
39
+ "keywords": [
40
+ "hl7",
41
+ "hl7v2",
42
+ "nodejs",
43
+ "typescript",
44
+ "definition",
45
+ "lint",
46
+ "hl7v2-lint",
47
+ "hl7v2-lint-rule",
48
+ "rule"
49
+ ],
50
+ "packageManager": "pnpm@10.12.1",
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "scripts": {
55
+ "build": "tsup && tsc --emitDeclarationOnly",
56
+ "check-types": "tsc --noEmit",
57
+ "test": "vitest run",
58
+ "test:coverage": "vitest run --coverage",
59
+ "test:watch": "vitest"
60
+ }
61
+ }