Zod-powered runtime validation and type-safety for JSON-based FHIR resources.
npm install fhir-typed
In short, using the fhir-typed
FHIR validator API programmatically involves three steps:
- Create a validator instance
- Load packages or local files to validate against
- Validate the resource against one or more profiles or data types
import { FhirValidator } from 'fhir-typed';
const validator = new FhirValidator();
// Load some FHIR packages from the public NPM registry:
await validator.loadPackages("hl7.fhir.r4.core!4.0.1", "hl7.fhir.fi.base");
await validator.loadPackages("hl7.fhir.se.base!latest");
// Now, profiles from all three packages should be recognized:
await Promise.all([
"http://hl7.org/fhir/StructureDefinition/Patient",
"https://hl7.fi/fhir/finnish-base-profiles/StructureDefinition/fi-base-patient",
"http://hl7.org/fhir/uv/ipa/StructureDefinition/ipa-patient"
].map((schema) => validator.recognizes(schema))); // => [ true, true, true]
With the suitable profiles loaded, we can validate resources against them. By default, the validator will validate the resource against profiles the resource declares conformance to via meta.profile
:
const resource = {
resourceType: "Patient",
meta: {
profile: ["ttp://hl7.org/fhir/StructureDefinition/Patient"],
},
name: [{ use: "official", family: "Smith", given: ["John"] }],
};
await validator.validate(resource);
The resource to validate can also be passed as a raw JSON string:
const resource = fs.readFileSync(filePath, "utf-8").toString();
await validator.validate(resource);
Or by simply passing the file path to the validate(resource, options)
method:
await validator.validate("path/to/resource.json");
We can also specify which profiles to validate against explicitly, and use the validation options to specify additional validation rules such as instructing the validator to ignore any profiles the resource might self-declare conformance to:
import { ValidateOptions } from 'fhir-typed';
const options: ValidateOptions = {
profiles: ["http://hl7.org/fhir/StructureDefinition/patient"],
ignoreSelfDeclaredProfiles: true,
ignoreUnknownProfiles: true,
};
await validator.validate(resource, options);
Licensed under CC BY-NC-ND 4.0. Dual-licensing for commercial use available upon request.