@reasonhealth/fhir-zod 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 (5) hide show
  1. package/README.md +74 -0
  2. package/package.json +26 -0
  3. package/src/r4.ts +13904 -0
  4. package/src/r4b.ts +14286 -0
  5. package/src/r5.ts +17709 -0
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @reasonhealth/fhir-zod
2
+
3
+ [![npm](https://img.shields.io/npm/v/@reasonhealth/fhir-zod)](https://www.npmjs.com/package/@reasonhealth/fhir-zod)
4
+
5
+ Zod schemas for FHIR R4, R4B, and R5. Generated from official FHIR StructureDefinition packages via [`@reasonhealth/fhir-ts-codegen`](../fhir-ts-codegen).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @reasonhealth/fhir-zod zod
11
+ # or
12
+ bun add @reasonhealth/fhir-zod zod
13
+ ```
14
+
15
+ Pair with [`@types/fhir`](https://www.npmjs.com/package/@types/fhir) for ambient TypeScript types if you need them alongside runtime validation:
16
+
17
+ ```bash
18
+ npm install --save-dev @types/fhir
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```ts
24
+ import { PatientSchema, BundleSchema } from '@reasonhealth/fhir-zod/r4'
25
+
26
+ // Parse and validate (throws ZodError on failure)
27
+ const patient = PatientSchema.parse(rawJson)
28
+
29
+ // Safe parse (no throw)
30
+ const result = PatientSchema.safeParse(rawJson)
31
+ if (result.success) {
32
+ console.log(result.data.resourceType) // 'Patient'
33
+ }
34
+
35
+ // Infer types directly from the schema
36
+ import type { z } from 'zod'
37
+ import { PatientSchema } from '@reasonhealth/fhir-zod/r4'
38
+ type Patient = z.infer<typeof PatientSchema>
39
+ ```
40
+
41
+ See [EXAMPLES.md](./EXAMPLES.md) for more usage patterns.
42
+
43
+ ### Available entry points
44
+
45
+ | Import | FHIR Version |
46
+ |--------|-------------|
47
+ | `@reasonhealth/fhir-zod/r4` | FHIR R4 (4.0.1) |
48
+ | `@reasonhealth/fhir-zod/r4b` | FHIR R4B (4.3.0) |
49
+ | `@reasonhealth/fhir-zod/r5` | FHIR R5 (5.0.0) |
50
+
51
+ Every schema file exports:
52
+
53
+ - A `*Schema` constant for each FHIR type (e.g. `PatientSchema`, `BundleSchema`)
54
+ - An inferred TypeScript `type` for each schema (e.g. `type Patient = z.infer<typeof PatientSchema>`)
55
+
56
+ ## What's generated
57
+
58
+ Notable schema features:
59
+
60
+ - **BackboneElements** use `BackboneElementSchema.extend({})` for clean inheritance
61
+ - **Choice types** (`value[x]`) expand to individual optional fields
62
+ - **Primitive shadow fields** (`_birthDate`) included for FHIR primitive extensions
63
+ - **Inline enums** for required-strength bindings (`z.enum(['male', 'female', 'other', 'unknown'])`)
64
+ - **`resourceType`** as `z.literal('Patient')` for discriminated union support
65
+ - **Circular references** (e.g. `Questionnaire.item.item`, `ValueSet.compose`) handled with `z.lazy()`
66
+ - Schemas emitted in topological order to minimise forward references
67
+
68
+ ## Regenerating
69
+
70
+ ```bash
71
+ bun run generate
72
+ ```
73
+
74
+ Requires `@reasonhealth/fhir-ts-codegen` to be available (installed via workspace). FHIR packages are downloaded automatically on first run and cached in `.fhir-cache/`.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@reasonhealth/fhir-zod",
3
+ "version": "1.0.0",
4
+ "description": "Zod schemas for FHIR R4, R4B, R5",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": {
8
+ "./r4": "./src/r4.ts",
9
+ "./r4b": "./src/r4b.ts",
10
+ "./r5": "./src/r5.ts"
11
+ },
12
+ "files": [
13
+ "src"
14
+ ],
15
+ "scripts": {
16
+ "generate": "fhir-generate --config ./generate.config.ts",
17
+ "typecheck": "tsc --noEmit"
18
+ },
19
+ "dependencies": {
20
+ "@reasonhealth/fhir-ts-codegen": "workspace:*",
21
+ "zod": "^3.24.0"
22
+ },
23
+ "devDependencies": {
24
+ "@types/bun": "latest"
25
+ }
26
+ }