@rethinkhealth/hl7v2-lint-segment-header-length 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,174 @@
1
+ # @rethinkhealth/hl7v2-lint-segment-header-length
2
+
3
+ A [`unified`][github-unified] lint rule for HL7v2 that warns when a segment **header** (segment ID) is not exactly three uppercase ASCII letters (e.g., `MSH`, `PID`, `OBX`).
4
+
5
+ ## What is this?
6
+
7
+ This package validates the **segment header** in HL7v2 syntax trees produced by parsers like [`@rethinkhealth/hl7v2-parser`][github-hl7v2-parser].
8
+
9
+ It reports a message when the header before the first field delimiter is not a 3-letter uppercase code.
10
+
11
+ ## When should I use this?
12
+
13
+ Use this rule to enforce canonical HL7v2 segment identifiers across messages, catch typos (e.g., `PID1`, `Obx`, `MS`), and improve downstream processing reliability.
14
+
15
+ ## Presets
16
+
17
+ This plugin is included in the following presets:
18
+
19
+ | Preset | Options |
20
+ | - | - |
21
+ | `@rethinkhealth/hl7v2-lint-recommended` | |
22
+
23
+ ## Install
24
+
25
+ This package is **ESM only**. In Node.js (v16+), install with npm:
26
+
27
+ ```sh
28
+ npm install @rethinkhealth/hl7v2-lint-segment-header-length
29
+ ````
30
+
31
+
32
+ ## Use
33
+
34
+ On the API:
35
+
36
+ ```js
37
+ import { unified } from 'unified'
38
+ import { hl7v2Parse } from '@rethinkhealth/hl7v2-parser'
39
+ import hl7v2LintSegmentHeaderLength from '@rethinkhealth/hl7v2-lint-segment-header-length'
40
+ import { reporter } from 'vfile-reporter'
41
+
42
+ const msg = `MSH|^~\\&|...`
43
+ const file = await unified()
44
+ .use(hl7v2Parse)
45
+ .use(hl7v2LintSegmentHeaderLength)
46
+ .process(msg)
47
+
48
+ console.error(reporter([file]))
49
+ ```
50
+
51
+
52
+ ## API
53
+
54
+ ### `unified().use(hl7v2LintSegmentHeaderLength[, options])`
55
+
56
+ Warn when a segment header is not a three-letter uppercase code.
57
+
58
+ ###### Parameters
59
+
60
+ * `options.allow` (optional, `string[]`) — an allowlist of additional 3-letter codes (e.g., private segments such as `ZAD`). Values must still be 3 uppercase letters.
61
+
62
+ ###### Returns
63
+
64
+ A `unified` Transformer that adds messages to the file.
65
+
66
+ ## Recommendation
67
+
68
+ HL7v2 segments are identified by **exactly three uppercase letters**. Deviations often indicate malformed input, private extensions used incorrectly, or typos that can break routing and transformations.
69
+
70
+ It’s recommended to enable this rule in most pipelines.
71
+
72
+ ## Examples
73
+
74
+ ##### `ok.hl7`
75
+
76
+ ###### In
77
+
78
+ ```hl7
79
+ MSH|^~\&|...
80
+ PID|1||12345^^^HOSP^MR||DOE^JANE||...
81
+ OBX|1|TX|...
82
+ ```
83
+
84
+ ###### Out
85
+
86
+ No messages.
87
+
88
+ ##### `not-ok-too-long.hl7`
89
+
90
+ ###### In
91
+
92
+ ```hl7
93
+ PID1|1||12345||DOE^JANE||...
94
+ ```
95
+
96
+ ###### Out
97
+
98
+ ```text
99
+ 1:1-1:4: Invalid segment header `PID1`: expected exactly 3 uppercase letters (e.g., `PID`)
100
+ ```
101
+
102
+ ##### `not-ok-too-short.hl7`
103
+
104
+ ###### In
105
+
106
+ ```hl7
107
+ MS|^~\&|...
108
+ ```
109
+
110
+ ###### Out
111
+
112
+ ```text
113
+ 1:1-1:3: Invalid segment header `MS`: expected 3 uppercase letters
114
+ ```
115
+
116
+ ##### `not-ok-lowercase.hl7`
117
+
118
+ ###### In
119
+
120
+ ```hl7
121
+ obx|1|TX|...
122
+ ```
123
+
124
+ ###### Out
125
+
126
+ ```text
127
+ 1:1-1:4: Invalid segment header `obx`: expected uppercase `OBX`
128
+ ```
129
+
130
+ ##### `ok-with-allowlist.hl7`
131
+
132
+ ###### In
133
+
134
+ ```hl7
135
+ ZAD|1|...
136
+ ```
137
+
138
+ With:
139
+
140
+ ```js
141
+ .unified().use(hl7v2LintSegmentHeaderLength, { allow: ['ZAD'] })
142
+ ```
143
+
144
+ ###### Out
145
+
146
+ No messages.
147
+
148
+ ## Security
149
+
150
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
151
+
152
+
153
+ ## Contributing
154
+
155
+ We welcome contributions! Please see our [Contributing Guide](../../CONTRIBUTING.md) for more details.
156
+
157
+ 1. Fork the repository
158
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
159
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
160
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
161
+ 5. Open a Pull Request
162
+
163
+ ## Code of Conduct
164
+
165
+ 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.
166
+
167
+ ## License
168
+
169
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
170
+
171
+ 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.
172
+
173
+ [github-unified]: https://github.com/unifiedjs/unified
174
+ [github-hl7v2-parser]: https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-parser
@@ -0,0 +1,9 @@
1
+ import type { Node } from '@rethinkhealth/hl7v2-ast';
2
+ /**
3
+ * hl7v2-lint rule to warn when segment header length is invalid.
4
+ *
5
+ *
6
+ */
7
+ declare const hl7v2LintSegmentHeaderLength: import("unified-lint-rule").Plugin<Node, undefined>;
8
+ export default hl7v2LintSegmentHeaderLength;
9
+ //# 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,EAAW,MAAM,0BAA0B,CAAC;AAK9D;;;;GAIG;AACH,QAAA,MAAM,4BAA4B,qDAiCjC,CAAC;AAEF,eAAe,4BAA4B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,33 @@
1
+ // src/index.ts
2
+ import pluralize from "pluralize";
3
+ import { lintRule } from "unified-lint-rule";
4
+ import { CONTINUE, SKIP, visitParents } from "unist-util-visit-parents";
5
+ var hl7v2LintSegmentHeaderLength = lintRule(
6
+ {
7
+ origin: "hl7v2-lint:segment-header-length",
8
+ url: "https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-segment-header-length#readme"
9
+ },
10
+ (tree, file) => {
11
+ visitParents(tree, (node, parents) => {
12
+ if (node.type === "root") {
13
+ return CONTINUE;
14
+ }
15
+ if (node.type !== "segment") {
16
+ return SKIP;
17
+ }
18
+ const size = node.children?.[0]?.children?.[0]?.children?.[0]?.children?.[0]?.value.length ?? 0;
19
+ if (size === 3) {
20
+ return SKIP;
21
+ }
22
+ file.message(
23
+ `Unexpected ${size} header length, expected 3 characters, ${size > 3 ? `remove ${size - 3} ${pluralize("character", size - 3)}` : `add ${3 - (size ?? 0)} ${pluralize("character", 3 - (size ?? 0))}`}`,
24
+ { ancestors: [...parents, node], place: node.position }
25
+ );
26
+ });
27
+ }
28
+ );
29
+ var src_default = hl7v2LintSegmentHeaderLength;
30
+ export {
31
+ src_default as default
32
+ };
33
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Node, Segment } from '@rethinkhealth/hl7v2-ast';\nimport pluralize from 'pluralize';\nimport { lintRule } from 'unified-lint-rule';\nimport { CONTINUE, SKIP, visitParents } from 'unist-util-visit-parents';\n\n/**\n * hl7v2-lint rule to warn when segment header length is invalid.\n *\n *\n */\nconst hl7v2LintSegmentHeaderLength = lintRule<Node, undefined>(\n {\n origin: 'hl7v2-lint:segment-header-length',\n url: 'https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-segment-header-length#readme',\n },\n (tree, file) => {\n visitParents(tree, (node, parents) => {\n if (node.type === 'root') {\n return CONTINUE;\n }\n\n if (node.type !== 'segment') {\n return SKIP;\n }\n\n const size =\n (node as Segment).children?.[0]?.children?.[0]?.children?.[0]\n ?.children?.[0]?.value.length ?? 0;\n\n if (size === 3) {\n return SKIP;\n }\n\n file.message(\n `Unexpected ${size} header length, expected 3 characters, ${\n size > 3\n ? `remove ${size - 3} ${pluralize('character', size - 3)}`\n : `add ${3 - (size ?? 0)} ${pluralize('character', 3 - (size ?? 0))}`\n }`,\n { ancestors: [...parents, node], place: node.position }\n );\n });\n }\n);\n\nexport default hl7v2LintSegmentHeaderLength;\n"],"mappings":";AACA,OAAO,eAAe;AACtB,SAAS,gBAAgB;AACzB,SAAS,UAAU,MAAM,oBAAoB;AAO7C,IAAM,+BAA+B;AAAA,EACnC;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,eAAO;AAAA,MACT;AAEA,UAAI,KAAK,SAAS,WAAW;AAC3B,eAAO;AAAA,MACT;AAEA,YAAM,OACH,KAAiB,WAAW,CAAC,GAAG,WAAW,CAAC,GAAG,WAAW,CAAC,GACxD,WAAW,CAAC,GAAG,MAAM,UAAU;AAErC,UAAI,SAAS,GAAG;AACd,eAAO;AAAA,MACT;AAEA,WAAK;AAAA,QACH,cAAc,IAAI,0CAChB,OAAO,IACH,UAAU,OAAO,CAAC,IAAI,UAAU,aAAa,OAAO,CAAC,CAAC,KACtD,OAAO,KAAK,QAAQ,EAAE,IAAI,UAAU,aAAa,KAAK,QAAQ,EAAE,CAAC,EACvE;AAAA,QACA,EAAE,WAAW,CAAC,GAAG,SAAS,IAAI,GAAG,OAAO,KAAK,SAAS;AAAA,MACxD;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEA,IAAO,cAAQ;","names":[]}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@rethinkhealth/hl7v2-lint-segment-header-length",
3
+ "description": "hl7v2-lint rule to warn when segment header length is invalid",
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
+ "pluralize": "^8.0.0",
20
+ "unified": "11.0.1",
21
+ "unified-lint-rule": "^3.0.0",
22
+ "unist-util-visit-parents": "^6.0.1"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "22.15.31",
26
+ "@types/pluralize": "^0.0.33",
27
+ "@types/unist": "^3.0.3",
28
+ "@vitest/coverage-c8": "^0.33.0",
29
+ "@vitest/coverage-v8": "^3.2.4",
30
+ "tsup": "8.5.0",
31
+ "typescript": "^5.8.3",
32
+ "vfile-reporter": "^8.1.1",
33
+ "vitest": "^3.2.4",
34
+ "@rethinkhealth/hl7v2-ast": "0.2.8",
35
+ "@rethinkhealth/tsconfig": "0.0.0",
36
+ "@rethinkhealth/testing": "0.0.0",
37
+ "@rethinkhealth/hl7v2": "0.2.8"
38
+ },
39
+ "repository": "rethinkhealth/hl7v2.git",
40
+ "homepage": "https://www.rethinkhealth.io/hl7v2/docs",
41
+ "keywords": [
42
+ "hl7",
43
+ "hl7v2",
44
+ "nodejs",
45
+ "typescript",
46
+ "definition",
47
+ "lint",
48
+ "hl7v2-lint",
49
+ "hl7v2-lint-rule",
50
+ "rule"
51
+ ],
52
+ "packageManager": "pnpm@10.12.1",
53
+ "publishConfig": {
54
+ "access": "public"
55
+ },
56
+ "scripts": {
57
+ "build": "tsup && tsc --emitDeclarationOnly",
58
+ "check-types": "tsc --noEmit",
59
+ "test": "vitest run",
60
+ "test:coverage": "vitest run --coverage",
61
+ "test:watch": "vitest"
62
+ }
63
+ }