@reasonhealth/fhir-zod 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +77 -3
  2. package/package.json +10 -1
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/@reasonhealth/fhir-zod)](https://www.npmjs.com/package/@reasonhealth/fhir-zod)
4
4
 
5
- Zod schemas for FHIR R4, R4B, and R5. Generated from official FHIR StructureDefinition packages via [`@reasonhealth/fhir-ts-codegen`](../fhir-ts-codegen).
5
+ Zod schemas for FHIR R4, R4B, and R5. Generated from official FHIR StructureDefinition packages via [`@reasonhealth/fhir-ts-codegen`](https://github.com/reason-healthcare/fhir-types-workspace/tree/main/packages/fhir-ts-codegen).
6
6
 
7
7
  ## Installation
8
8
 
@@ -38,8 +38,6 @@ import { PatientSchema } from '@reasonhealth/fhir-zod/r4'
38
38
  type Patient = z.infer<typeof PatientSchema>
39
39
  ```
40
40
 
41
- See [EXAMPLES.md](./EXAMPLES.md) for more usage patterns.
42
-
43
41
  ### Available entry points
44
42
 
45
43
  | Import | FHIR Version |
@@ -53,6 +51,82 @@ Every schema file exports:
53
51
  - A `*Schema` constant for each FHIR type (e.g. `PatientSchema`, `BundleSchema`)
54
52
  - An inferred TypeScript `type` for each schema (e.g. `type Patient = z.infer<typeof PatientSchema>`)
55
53
 
54
+ ## Examples
55
+
56
+ ### Safe parse with error handling
57
+
58
+ ```ts
59
+ import { ObservationSchema } from '@reasonhealth/fhir-zod/r4'
60
+
61
+ const result = ObservationSchema.safeParse(raw)
62
+ if (!result.success) {
63
+ console.error('Invalid Observation:', result.error.flatten())
64
+ } else {
65
+ console.log('Status:', result.data.status)
66
+ }
67
+ ```
68
+
69
+ ### Infer types from schemas
70
+
71
+ ```ts
72
+ import { z } from 'zod'
73
+ import { PatientSchema, BundleSchema } from '@reasonhealth/fhir-zod/r4'
74
+
75
+ type Patient = z.infer<typeof PatientSchema>
76
+ type Bundle = z.infer<typeof BundleSchema>
77
+ ```
78
+
79
+ ### Using with `@types/fhir`
80
+
81
+ `@types/fhir` provides ambient namespace types (`fhir4.Patient`, etc.). Build a type-guard that bridges the two:
82
+
83
+ ```ts
84
+ import { PatientSchema } from '@reasonhealth/fhir-zod/r4'
85
+
86
+ function isPatient(resource: fhir4.Resource): resource is fhir4.Patient {
87
+ return PatientSchema.safeParse(resource).success
88
+ }
89
+ ```
90
+
91
+ ### Bundle unpacking
92
+
93
+ ```ts
94
+ import { BundleSchema, PatientSchema } from '@reasonhealth/fhir-zod/r4'
95
+
96
+ const bundle = BundleSchema.parse(raw)
97
+ const patients = (bundle.entry ?? [])
98
+ .map(e => e.resource)
99
+ .filter(r => r?.resourceType === 'Patient')
100
+ .map(r => PatientSchema.parse(r))
101
+ ```
102
+
103
+ ### Discriminated union
104
+
105
+ ```ts
106
+ import { z } from 'zod'
107
+ import { PatientSchema, PractitionerSchema, OrganizationSchema } from '@reasonhealth/fhir-zod/r4'
108
+
109
+ const SubjectSchema = z.discriminatedUnion('resourceType', [
110
+ PatientSchema,
111
+ PractitionerSchema,
112
+ OrganizationSchema,
113
+ ])
114
+
115
+ type Subject = z.infer<typeof SubjectSchema>
116
+ ```
117
+
118
+ ### Extend or restrict for profiles
119
+
120
+ ```ts
121
+ import { PatientSchema } from '@reasonhealth/fhir-zod/r4'
122
+
123
+ const PatientSummarySchema = PatientSchema.pick({ id: true, name: true, birthDate: true })
124
+
125
+ const IdentifiedPatientSchema = PatientSchema.extend({
126
+ id: PatientSchema.shape.id.unwrap(), // make id required
127
+ })
128
+ ```
129
+
56
130
  ## What's generated
57
131
 
58
132
  Notable schema features:
package/package.json CHANGED
@@ -1,9 +1,18 @@
1
1
  {
2
2
  "name": "@reasonhealth/fhir-zod",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Zod schemas for FHIR R4, R4B, R5",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/reason-healthcare/fhir-types-workspace.git",
10
+ "directory": "packages/fhir-zod"
11
+ },
12
+ "homepage": "https://github.com/reason-healthcare/fhir-types-workspace/tree/main/packages/fhir-zod#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/reason-healthcare/fhir-types-workspace/issues"
15
+ },
7
16
  "exports": {
8
17
  "./r4": "./src/r4.ts",
9
18
  "./r4b": "./src/r4b.ts",