22 releases
Uses new Rust 2024
| new 0.3.14 | Jan 7, 2026 |
|---|---|
| 0.3.13 | Jan 5, 2026 |
| 0.3.12 | Dec 30, 2025 |
| 0.3.5 | Nov 24, 2025 |
| 0.1.0 | Aug 6, 2025 |
#141 in Science
85 downloads per month
Used in 5 crates
(3 directly)
1MB
6K
SLoC
octofhir-fhirschema
FHIR schema conversion and validation library for Rust.
Overview
This crate provides tools for converting FHIR StructureDefinitions to FHIRSchema format and validating FHIR resources against those schemas. It supports FHIR versions R4, R4B, R5, and R6.
Installation
Add this to your Cargo.toml:
[dependencies]
octofhir-fhirschema = "0.3.2"
Quick Start
Converting StructureDefinition to FHIRSchema
use octofhir_fhirschema::{translate, StructureDefinition};
// Load your StructureDefinition
let structure_definition: StructureDefinition = serde_json::from_str(json_string)?;
// Convert to FHIRSchema
let schema = translate(structure_definition, None)?;
println!("Converted schema: {}", schema.name);
Validating FHIR Resources
use octofhir_fhirschema::{validate, create_validation_context, FhirVersion};
// Create validation context with embedded schemas
let context = create_validation_context(FhirVersion::R4)?;
// Validate a FHIR resource
let patient_data = serde_json::json!({
"resourceType": "Patient",
"id": "example",
"active": true
});
let result = validate(&context, vec![], &patient_data);
if result.valid {
println!("Resource is valid!");
} else {
println!("Validation errors: {:?}", result.errors);
}
Using Embedded Schemas
The crate includes precompiled schemas for faster startup:
use octofhir_fhirschema::{FhirVersion, get_schema, list_resources};
// List available resource types
let resources = list_resources(FhirVersion::R4);
println!("Available resources: {:?}", resources);
// Get a specific schema
if let Some(patient_schema) = get_schema(FhirVersion::R4, "Patient") {
println!("Patient schema loaded");
}
Features
- Schema Conversion: Convert FHIR StructureDefinitions to FHIRSchema format
- Resource Validation: Validate FHIR resources against schemas
- Multi-Version Support: R4, R4B, R5, and R6 FHIR versions
- Embedded Schemas: Precompiled schemas for fast startup
- Type Safety: Full Rust type safety and error handling
- Performance: Efficient validation with minimal allocations
Core Types
FhirSchema- Converted schema formatStructureDefinition- Input FHIR structure definitionValidationContext- Container for schemas used in validationValidationResult- Validation results with success flag and errorsFhirSchemaValidator- Main validator implementation
Provider Pattern
The crate supports different schema providers:
use octofhir_fhirschema::{EmbeddedSchemaProvider, DynamicSchemaProvider, FhirVersion};
// Use embedded schemas (fastest)
let provider = EmbeddedSchemaProvider::new(FhirVersion::R4)?;
// Use dynamic schemas (more flexible)
let provider = DynamicSchemaProvider::new();
Error Handling
All operations return Result types with detailed error information:
use octofhir_fhirschema::{FhirSchemaError, Result};
match translate(structure_def, None) {
Ok(schema) => println!("Success: {}", schema.name),
Err(FhirSchemaError::JsonError(e)) => eprintln!("JSON error: {}", e),
Err(FhirSchemaError::ValidationError(e)) => eprintln!("Validation error: {}", e),
Err(e) => eprintln!("Other error: {}", e),
}
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Repository
This crate is part of the OctoFHIR project.
Dependencies
~40–81MB
~1M SLoC