Swift utilities for working with FHIR R4 resources from apple/FHIRModels: helpers that make building and modifying resources concise, a builder API for FHIR extensions, questionnaire conveniences, and a FHIRPath parser for date expressions.
The package provides three products.
Extends the FHIRModels R4 types:
- Building resources:
ObservationgainsappendCoding(_:),appendCategory(_:),appendComponent(_:),appendIdentifier(_:),setEffective(startDate:endDate:timeZone:), andsetIssued(on:timeZone:). Generic keypath helpers append to and remove from any collection-typed property of any FHIR type. - FHIR extensions: every type that carries extensions offers
extensions(for:),appendExtension(_:replaceAllExistingWithSameUrl:), and removal by URL.FHIRExtensionBuilderpackages extension logic into reusable,Sendablevalues that are applied to anObservationwith a given input. - Dates and decimals: conversions between Foundation and FHIR dates, times, and instants, plus
asFHIRDecimalPrimitiveSafe(), which throws on infinite values instead of trapping.setEffectiveandsetIssuedwrite each date with the GMT offset its time zone has at that date, so a date in the hour that repeats when daylight saving time ends is not shifted by an hour; useTimeZone.fixedOffset(at:)to build other FHIR date-times the same way. - Questionnaires: access to contained value sets, and typed accessors for the rendering and validation extensions used by questionnaire renderers, such as item control, slider step, minimum and maximum values, regular expressions, and keyboard hints.
Ready-to-use Questionnaire resources: the PHQ-9, GAD-7, IPSS, and GCS instruments, and examples that exercise skip logic, text validation, sliders, dates, image capture, and contained value sets.
Parses FHIRPath expressions and evaluates the date arithmetic that questionnaire constraints use, for example today() + 3 months.
import FHIRModelsExtensions
import Foundation
import ModelsR4
let endDate = Date()
let startDate = endDate.addingTimeInterval(-60)
var observation = Observation(code: CodeableConcept(), status: FHIRPrimitive(.final))
observation.appendCoding(Coding(code: "8867-4", display: "Heart rate", system: "http://loinc.org"))
try observation.setEffective(startDate: startDate, endDate: endDate, timeZone: .current)
let trackTimeZone = FHIRExtensionBuilder<TimeZone> { timeZone, observation in
observation.appendExtension(
Extension(url: "https://example.org/fhir/timeZone", value: .string(timeZone.identifier.asFHIRStringPrimitive())),
replaceAllExistingWithSameUrl: true
)
}
try observation.apply(trackTimeZone, input: .current)The FHIRModels types are structs, so every helper that modifies a resource is mutating and extension builders receive their target inout.
The full API reference is available on the Swift Package Index.
Add the package with the Swift Package Manager. In Xcode, follow adding package dependencies to your app. In a Swift package, add it to your Package.swift:
dependencies: [
.package(url: "https://github.com/StanfordBDHG/FHIRModelsExtensions.git", from: "0.2.0")
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "FHIRModelsExtensions", package: "FHIRModelsExtensions")
]
)
]- Swift 6.2 or later.
- iOS 17, watchOS 10, tvOS 17, visionOS 1, macOS 14, or Mac Catalyst 17.
- FHIRModels 0.9.4 or later. FHIRModels 0.9 turned every model type into a struct, which is why 0.2.0 is a breaking release.
If your package still depends on FHIRModels 0.8, stay on the 0.1 releases with .upToNextMinor(from: "0.1.2"). A requirement of from: "0.1.0" also allows 0.2.0, so it only keeps resolving to 0.1.x for as long as your package caps FHIRModels below 0.9.
This project is licensed under the MIT License. See Licenses for more information.
This project is developed as part of the Stanford Mussallem Center for Biodesign at Stanford University. See CONTRIBUTORS.md for a full list of all FHIRModelsExtensions contributors.

