@peerbits/fhir-validator 1.0.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.
Files changed (3) hide show
  1. package/LICENSE +105 -0
  2. package/README.md +211 -0
  3. package/package.json +49 -0
package/LICENSE ADDED
@@ -0,0 +1,105 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity.
18
+
19
+ "You" (or "Your") shall mean an individual or Legal Entity
20
+ exercising permissions granted by this License.
21
+
22
+ "Source" form shall mean the preferred form for making modifications.
23
+
24
+ "Object" form shall mean any form resulting from mechanical
25
+ transformation or translation of a Source form.
26
+
27
+ "Work" shall mean the work of authorship, whether in Source or
28
+ Object form, made available under the License, as indicated by a
29
+ copyright notice that is included in or attached to the work.
30
+
31
+ "Derivative Works" shall mean any work, whether in Source or Object
32
+ form, that is based on (or derived from) the Work.
33
+
34
+ "Contribution" shall mean any work of authorship intentionally
35
+ submitted to Licensor for inclusion in the Work.
36
+
37
+ "Contributor" shall mean Licensor and any individual or Legal Entity
38
+ on behalf of whom a Contribution has been received by Licensor.
39
+
40
+ 2. Grant of Copyright License. Subject to the terms and conditions of
41
+ this License, each Contributor hereby grants to You a perpetual,
42
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
43
+ copyright license to reproduce, prepare Derivative Works of,
44
+ publicly display, publicly perform, sublicense, and distribute the
45
+ Work and such Derivative Works in Source or Object form.
46
+
47
+ 3. Grant of Patent License. Subject to the terms and conditions of
48
+ this License, each Contributor hereby grants to You a perpetual,
49
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
50
+ (except as stated in this section) patent license to make, have
51
+ made, use, offer to sell, sell, import, and otherwise transfer the
52
+ Work. If You institute patent litigation against any entity
53
+ alleging that the Work constitutes direct or contributory patent
54
+ infringement, then any patent licenses granted to You under this
55
+ License for that Work shall terminate as of the date such
56
+ litigation is filed.
57
+
58
+ 4. Redistribution. You may reproduce and distribute copies of the
59
+ Work or Derivative Works thereof provided that You meet the
60
+ following conditions:
61
+
62
+ (a) You must give any other recipients a copy of this License; and
63
+ (b) You must cause modified files to carry prominent notices; and
64
+ (c) You must retain all copyright, patent, trademark, and
65
+ attribution notices from the Source form; and
66
+ (d) If the Work includes a NOTICE file, any Derivative Works must
67
+ include a readable copy of its attribution notices.
68
+
69
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
70
+ any Contribution intentionally submitted for inclusion in the Work
71
+ by You shall be under the terms of this License, without additional
72
+ terms or conditions.
73
+
74
+ 6. Trademarks. This License does not grant permission to use trade
75
+ names, trademarks, service marks, or product names of the Licensor.
76
+
77
+ 7. Disclaimer of Warranty. The Work is provided on an "AS IS" BASIS,
78
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
79
+ implied.
80
+
81
+ 8. Limitation of Liability. In no event shall any Contributor be
82
+ liable for damages arising as a result of this License or out of
83
+ the use or inability to use the Work.
84
+
85
+ 9. Accepting Warranty or Additional Liability. You may choose to offer
86
+ support, warranty, indemnity, or other liability obligations, but
87
+ only on Your own behalf and sole responsibility.
88
+
89
+ END OF TERMS AND CONDITIONS
90
+
91
+ APPENDIX: How to apply the Apache License to your work.
92
+
93
+ Copyright [yyyy] [name of copyright owner]
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License");
96
+ you may not use this file except in compliance with the License.
97
+ You may obtain a copy of the License at
98
+
99
+ http://www.apache.org/licenses/LICENSE-2.0
100
+
101
+ Unless required by applicable law or agreed to in writing, software
102
+ distributed under the License is distributed on an "AS IS" BASIS,
103
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
104
+ See the License for the specific language governing permissions and
105
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # @peerbits/fhir-validator
2
+
3
+ > Fast, lightweight structural, cardinality, and reference validation for FHIR R4 resources.
4
+
5
+ [![CI](https://github.com/PeerbitsSolution/fhir-validator/actions/workflows/ci.yml/badge.svg)](https://github.com/PeerbitsSolution/fhir-validator/actions)
6
+ [![CodeQL](https://github.com/PeerbitsSolution/fhir-validator/actions/workflows/codeql.yml/badge.svg)](https://github.com/PeerbitsSolution/fhir-validator/actions)
7
+ [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE)
8
+
9
+ > [!IMPORTANT]
10
+ > **Scope & Positioning**: `@peerbits/fhir-validator` is a fast in-memory **structural and reference validator** for FHIR R4 resources. It is **not** a full FHIR profile conformance engine, does not evaluate FHIRPath invariant expressions, and is not an ONC certification test kit.
11
+ >
12
+ > **Read our [Known Limitations](docs/KNOWN_LIMITATIONS.md)** for a clear overview of supported checks vs. out-of-scope capabilities and recommended full-conformance alternatives.
13
+
14
+ ---
15
+
16
+ ## The Peerbits HealthTech Toolkit Narrative
17
+
18
+ `@peerbits/fhir-validator` works seamlessly alongside the rest of the Peerbits Open Source healthcare stack:
19
+
20
+ 1. **[`@peerbits/smart-launch`](../smart-launch)** — Performs EHR SMART-on-FHIR OAuth2 / OIDC discovery and authorization.
21
+ 2. **[`@peerbits/fhir-client`](../fhir-client)** — Connects to FHIR servers and executes type-safe CRUD, searches, and batch bundles.
22
+ 3. **`@peerbits/fhir-validator`** — Inspects and verifies payload integrity at runtime, catching missing fields, invalid references, and malformed codings before requests hit the wire or database.
23
+
24
+ ---
25
+
26
+ ## Features
27
+
28
+ - **Zero-Dependency & In-Memory**: Pure TypeScript with zero runtime dependencies. Runs in Node.js, browsers, Edge workers, and Lambda functions.
29
+ - **7 Core Resource Types**: Base structural rules and cardinality checks for:
30
+ - `Patient`
31
+ - `Observation`
32
+ - `Encounter`
33
+ - `Condition`
34
+ - `Coverage`
35
+ - `Claim`
36
+ - `ClaimResponse`
37
+ - **Reference Target-Type Enforcement**: Checks that `Reference` elements target allowed resource types per the FHIR R4 specification.
38
+ - **Coding & CodeableConcept Shape Verification**: Structural validation of `code` and URI `system` fields without heavy external network calls.
39
+ - **Pluggable Profile Constraints**: Declaratively enforce additional required fields, cardinality minimums, and fixed values on top of base specifications (includes illustrative `USCorePatientProfile` and `USCoreObservationVitalsProfile`).
40
+ - **OperationOutcome-Aligned Output**: Returns `{ valid: boolean, issues: ValidationIssue[] }` with diagnostic severities (`error`, `warning`, `information`).
41
+
42
+ ---
43
+
44
+ ## Installation
45
+
46
+ ```bash
47
+ npm install @peerbits/fhir-validator
48
+ ```
49
+
50
+ ---
51
+
52
+ ## Quick Start
53
+
54
+ ### 1. Validating a Resource
55
+
56
+ ```typescript
57
+ import { validate } from "@peerbits/fhir-validator";
58
+
59
+ const observation = {
60
+ resourceType: "Observation",
61
+ id: "heart-rate-001",
62
+ status: "final",
63
+ code: {
64
+ coding: [
65
+ {
66
+ system: "http://loinc.org",
67
+ code: "8867-4",
68
+ display: "Heart rate",
69
+ },
70
+ ],
71
+ },
72
+ subject: {
73
+ reference: "Patient/synthetic-patient-001",
74
+ },
75
+ valueQuantity: {
76
+ value: 72,
77
+ unit: "/min",
78
+ system: "http://unitsofmeasure.org",
79
+ code: "/min",
80
+ },
81
+ };
82
+
83
+ const result = validate(observation);
84
+ console.log(result.valid); // true
85
+ console.log(result.issues); // []
86
+ ```
87
+
88
+ ### 2. Catching Structural Errors (Before / After)
89
+
90
+ When passed a resource with deliberate errors:
91
+
92
+ ```typescript
93
+ import { validate } from "@peerbits/fhir-validator";
94
+
95
+ const malformedObservation = {
96
+ resourceType: "Observation",
97
+ // 1. Missing required 'status'
98
+ code: {
99
+ coding: [
100
+ {
101
+ // 2. Missing required 'system' URI
102
+ code: "8867-4",
103
+ },
104
+ ],
105
+ },
106
+ subject: {
107
+ // 3. Disallowed target resource type for Observation.subject
108
+ reference: "Claim/claim-999",
109
+ },
110
+ };
111
+
112
+ const result = validate(malformedObservation);
113
+ console.log(result.valid); // false
114
+ console.log(result.issues);
115
+ ```
116
+
117
+ **Result Output (`ValidationIssue[]`):**
118
+
119
+ ```json
120
+ {
121
+ "valid": false,
122
+ "issues": [
123
+ {
124
+ "severity": "error",
125
+ "path": "Observation.status",
126
+ "code": "required",
127
+ "message": "Missing required 'status' in 'Observation'."
128
+ },
129
+ {
130
+ "severity": "error",
131
+ "path": "Observation.code.coding[0].system",
132
+ "code": "required",
133
+ "message": "Missing required 'system' URI in 'Observation.code.coding[0]'."
134
+ },
135
+ {
136
+ "severity": "error",
137
+ "path": "Observation.subject",
138
+ "code": "invalid-reference-type",
139
+ "message": "Reference at 'Observation.subject' targets disallowed resource type 'Claim'. Allowed types: Patient, Group, Device, Location."
140
+ }
141
+ ]
142
+ }
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Applying Profile Constraints
148
+
149
+ You can pass profile constraints to enforce additional requirements beyond base FHIR:
150
+
151
+ ```typescript
152
+ import { validate, USCorePatientProfile } from "@peerbits/fhir-validator";
153
+
154
+ const minimalPatient = {
155
+ resourceType: "Patient",
156
+ id: "patient-1",
157
+ gender: "female",
158
+ };
159
+
160
+ // Base validation passes (since name & identifier are optional in base FHIR R4)
161
+ const baseResult = validate(minimalPatient);
162
+ console.log(baseResult.valid); // true
163
+
164
+ // US Core Patient requires name and identifier
165
+ const profileResult = validate(minimalPatient, { profile: USCorePatientProfile });
166
+ console.log(profileResult.valid); // false
167
+ console.log(profileResult.issues);
168
+ // => Issues flagging missing 'Patient.identifier' and 'Patient.name'
169
+ ```
170
+
171
+ ---
172
+
173
+ ## API Reference
174
+
175
+ ### `validate(resource: unknown, options?: ValidateOptions): ValidationResult`
176
+
177
+ Validates any JSON object against base FHIR R4 rules and optional profile constraints.
178
+
179
+ #### `ValidationResult`
180
+ - `valid: boolean` — `true` if zero issues with `severity: "error"` were found.
181
+ - `issues: ValidationIssue[]` — List of validation issues.
182
+
183
+ #### `ValidationIssue`
184
+ - `severity: "error" | "warning" | "information"`
185
+ - `path: string` — Dot-notated element path (e.g. `Observation.code.coding[0].system`).
186
+ - `code: string` — Machine-readable issue code (e.g. `required`, `invalid-type`, `invalid-reference-type`).
187
+ - `message: string` — Human-readable description.
188
+
189
+ ---
190
+
191
+ ## Contributing & Development
192
+
193
+ ```bash
194
+ # Install dependencies
195
+ npm install
196
+
197
+ # Run test suite
198
+ npm test
199
+
200
+ # Type check
201
+ npm run typecheck
202
+
203
+ # Build bundle
204
+ npm run build
205
+ ```
206
+
207
+ ---
208
+
209
+ ## License
210
+
211
+ [Apache 2.0](LICENSE) © Peerbits
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@peerbits/fhir-validator",
3
+ "version": "1.0.0",
4
+ "description": "Structural, cardinality, and reference validation for FHIR R4 resources — a fast base-spec validator with a pluggable illustrative-profile mechanism (not a full conformance engine)",
5
+ "license": "Apache-2.0",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "type": "module",
10
+ "main": "dist/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.json",
24
+ "lint": "eslint .",
25
+ "typecheck": "tsc --noEmit",
26
+ "test": "vitest run"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/PeerbitsSolution/fhir-validator.git"
31
+ },
32
+ "keywords": [
33
+ "fhir",
34
+ "fhir-validation",
35
+ "healthcare",
36
+ "typescript",
37
+ "conformance"
38
+ ],
39
+ "devDependencies": {
40
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
41
+ "@typescript-eslint/parser": "^8.0.0",
42
+ "eslint": "^9.0.0",
43
+ "typescript": "^5.5.0",
44
+ "vitest": "^2.0.0"
45
+ },
46
+ "engines": {
47
+ "node": ">=18"
48
+ }
49
+ }