@glion/lint-segment-header-length 0.14.1

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
+ # @glion/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 [`@glion/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
+ | `@glion/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 @glion/lint-segment-header-length
29
+ ```
30
+
31
+ ## Use
32
+
33
+ On the API:
34
+
35
+ ```js
36
+ import { unified } from "unified";
37
+ import { hl7v2Parse } from "@glion/parser";
38
+ import hl7v2LintSegmentHeaderLength from "@glion/lint-segment-header-length";
39
+ import { reporter } from "vfile-reporter";
40
+
41
+ const msg = `MSH|^~\\&|...`;
42
+ const file = await unified()
43
+ .use(hl7v2Parse)
44
+ .use(hl7v2LintSegmentHeaderLength)
45
+ .process(msg);
46
+
47
+ console.error(reporter([file]));
48
+ ```
49
+
50
+ ## API
51
+
52
+ ### `unified().use(hl7v2LintSegmentHeaderLength[, options])`
53
+
54
+ Warn when a segment header is not a three-letter uppercase code.
55
+
56
+ ###### Parameters
57
+
58
+ - `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.
59
+
60
+ ###### Returns
61
+
62
+ A `unified` Transformer that adds messages to the file.
63
+
64
+ ## Recommendation
65
+
66
+ 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.
67
+
68
+ It’s recommended to enable this rule in most pipelines.
69
+
70
+ ## Examples
71
+
72
+ ##### `ok.hl7`
73
+
74
+ ###### In
75
+
76
+ ```hl7
77
+ MSH|^~\&|...
78
+ PID|1||12345^^^HOSP^MR||DOE^JANE||...
79
+ OBX|1|TX|...
80
+ ```
81
+
82
+ ###### Out
83
+
84
+ No messages.
85
+
86
+ ##### `not-ok-too-long.hl7`
87
+
88
+ ###### In
89
+
90
+ ```hl7
91
+ PID1|1||12345||DOE^JANE||...
92
+ ```
93
+
94
+ ###### Out
95
+
96
+ ```text
97
+ 1:1-1:4: Invalid segment header `PID1`: expected exactly 3 uppercase letters (e.g., `PID`)
98
+ ```
99
+
100
+ ##### `not-ok-too-short.hl7`
101
+
102
+ ###### In
103
+
104
+ ```hl7
105
+ MS|^~\&|...
106
+ ```
107
+
108
+ ###### Out
109
+
110
+ ```text
111
+ 1:1-1:3: Invalid segment header `MS`: expected 3 uppercase letters
112
+ ```
113
+
114
+ ##### `not-ok-lowercase.hl7`
115
+
116
+ ###### In
117
+
118
+ ```hl7
119
+ obx|1|TX|...
120
+ ```
121
+
122
+ ###### Out
123
+
124
+ ```text
125
+ 1:1-1:4: Invalid segment header `obx`: expected uppercase `OBX`
126
+ ```
127
+
128
+ ##### `ok-with-allowlist.hl7`
129
+
130
+ ###### In
131
+
132
+ ```hl7
133
+ ZAD|1|...
134
+ ```
135
+
136
+ With:
137
+
138
+ ```js
139
+ .unified().use(hl7v2LintSegmentHeaderLength, { allow: ['ZAD'] })
140
+ ```
141
+
142
+ ###### Out
143
+
144
+ No messages.
145
+
146
+ ## Security
147
+
148
+ This plugin only transforms AST nodes and does not execute code. Ensure you trust the source of HL7v2 messages before processing.
149
+
150
+ ## Contributing
151
+
152
+ We welcome contributions! Please see our [Contributing Guide][github-contributing] for more details.
153
+
154
+ 1. Fork the repository
155
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
156
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
157
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
158
+ 5. Open a Pull Request
159
+
160
+ ## Code of Conduct
161
+
162
+ To ensure a welcoming and positive environment, we have a [Code of Conduct][github-code-of-conduct] that all contributors and participants are expected to adhere to.
163
+
164
+ ## License
165
+
166
+ Copyright 2025 Rethink Health, SUARL. All rights reserved.
167
+
168
+ 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][github-license] file for details.
169
+
170
+ [github-code-of-conduct]: https://github.com/rethinkhealth/glion/blob/main/CODE_OF_CONDUCT.md
171
+ [github-license]: https://github.com/rethinkhealth/glion/blob/main/LICENSE
172
+ [github-contributing]: https://github.com/rethinkhealth/glion/blob/main/CONTRIBUTING.md
173
+ [github-unified]: https://github.com/unifiedjs/unified
174
+ [github-hl7v2-parser]: https://github.com/rethinkhealth/glion/tree/main/packages/hl7v2-parser
@@ -0,0 +1,7 @@
1
+ import type { Root } from "@glion/ast";
2
+ /**
3
+ * Hl7v2-lint rule to warn when segment header length is invalid.
4
+ */
5
+ declare const hl7v2LintSegmentHeaderLength: import("unified-lint-rule").Plugin<Root, undefined>;
6
+ export default hl7v2LintSegmentHeaderLength;
7
+ //# 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,EAAE,MAAM,YAAY,CAAC;AAKvC;;GAEG;AACH,QAAA,MAAM,4BAA4B,qDAuBjC,CAAC;AAEF,eAAe,4BAA4B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ import pluralize from "pluralize";
2
+ import { lintRule } from "unified-lint-rule";
3
+ import { SKIP, visitParents } from "unist-util-visit-parents";
4
+ //#region src/index.ts
5
+ /**
6
+ * Hl7v2-lint rule to warn when segment header length is invalid.
7
+ */
8
+ const hl7v2LintSegmentHeaderLength = lintRule({
9
+ origin: "hl7v2-lint:segment-header-length",
10
+ url: "https://github.com/rethinkhealth/hl7v2/tree/main/packages/hl7v2-lint-segment-header-length#readme"
11
+ }, (tree, file) => {
12
+ visitParents(tree, "segment", (node, parents) => {
13
+ const size = node.name.length;
14
+ if (size === 3) return SKIP;
15
+ file.message(`Unexpected ${size} header length, expected 3 characters, ${size > 3 ? `remove ${size - 3} ${pluralize("character", size - 3)}` : `add ${3 - size} ${pluralize("character", 3 - size)}`}`, {
16
+ ancestors: [...parents, node],
17
+ place: node.position
18
+ });
19
+ });
20
+ });
21
+ //#endregion
22
+ export { hl7v2LintSegmentHeaderLength as default };
23
+
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Root } from \"@glion/ast\";\nimport pluralize from \"pluralize\";\nimport { lintRule } from \"unified-lint-rule\";\nimport { SKIP, visitParents } from \"unist-util-visit-parents\";\n\n/**\n * Hl7v2-lint rule to warn when segment header length is invalid.\n */\nconst hl7v2LintSegmentHeaderLength = lintRule<Root, 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<Root, \"segment\">(tree, \"segment\", (node, parents) => {\n const size = node.name.length;\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} ${pluralize(\"character\", 3 - size)}`\n }`,\n { ancestors: [...parents, node], place: node.position }\n );\n });\n }\n);\n\nexport default hl7v2LintSegmentHeaderLength;\n"],"mappings":";;;;;;;AAQA,MAAM,+BAA+B,SACnC;CACE,QAAQ;CACR,KAAK;CACN,GACA,MAAM,SAAS;AACd,cAA8B,MAAM,YAAY,MAAM,YAAY;EAChE,MAAM,OAAO,KAAK,KAAK;AAEvB,MAAI,SAAS,EACX,QAAO;AAGT,OAAK,QACH,cAAc,KAAK,yCACjB,OAAO,IACH,UAAU,OAAO,EAAE,GAAG,UAAU,aAAa,OAAO,EAAE,KACtD,OAAO,IAAI,KAAK,GAAG,UAAU,aAAa,IAAI,KAAK,MAEzD;GAAE,WAAW,CAAC,GAAG,SAAS,KAAK;GAAE,OAAO,KAAK;GAAU,CACxD;GACD;EAEL"}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@glion/lint-segment-header-length",
3
+ "version": "0.14.1",
4
+ "description": "hl7v2-lint rule to warn when segment header length is invalid",
5
+ "keywords": [
6
+ "definition",
7
+ "hl7",
8
+ "hl7v2",
9
+ "hl7v2-lint",
10
+ "hl7v2-lint-rule",
11
+ "lint",
12
+ "nodejs",
13
+ "rule",
14
+ "typescript"
15
+ ],
16
+ "homepage": "https://glion.dev",
17
+ "license": "MIT",
18
+ "author": {
19
+ "name": "Melek Somai",
20
+ "email": "melek@rethinkhealth.io"
21
+ },
22
+ "repository": "rethinkhealth/glion.git",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "dependencies": {
35
+ "pluralize": "8.0.0",
36
+ "unified": "11.0.5",
37
+ "unified-lint-rule": "3.0.1",
38
+ "unist-util-visit-parents": "6.0.2"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^25.5.0",
42
+ "@types/pluralize": "^0.0.33",
43
+ "@types/unist": "^3.0.3",
44
+ "@vitest/coverage-v8": "4.1.2",
45
+ "tsdown": "0.21.7",
46
+ "typescript": "^5.9.3",
47
+ "vfile": "^6.0.3",
48
+ "vitest": "4.1.2",
49
+ "@glion/testing": "0.0.2",
50
+ "@glion/ast": "0.14.1",
51
+ "@glion/builder": "0.14.1",
52
+ "@glion/tsconfig": "0.0.1"
53
+ },
54
+ "engines": {
55
+ "node": ">=18"
56
+ },
57
+ "scripts": {
58
+ "build": "tsdown && tsc --emitDeclarationOnly",
59
+ "check-types": "tsc --noEmit",
60
+ "test": "vitest run",
61
+ "test:coverage": "vitest run --coverage",
62
+ "test:watch": "vitest"
63
+ }
64
+ }