| Crates.io | octofhir-fhirschema |
| lib.rs | octofhir-fhirschema |
| version | 0.3.14 |
| created_at | 2025-08-06 21:15:27.009237+00 |
| updated_at | 2026-01-07 11:03:20.138215+00 |
| description | FHIR schema conversion and validation library |
| homepage | |
| repository | https://github.com/octofhir/fhirschema-rs |
| max_upload_size | |
| id | 1784334 |
| size | 8,864,251 |
FHIR schema conversion and validation library for Rust.
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.
Add this to your Cargo.toml:
[dependencies]
octofhir-fhirschema = "0.3.2"
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);
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);
}
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");
}
FhirSchema - Converted schema formatStructureDefinition - Input FHIR structure definitionValidationContext - Container for schemas used in validationValidationResult - Validation results with success flag and errorsFhirSchemaValidator - Main validator implementationThe 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();
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),
}
Licensed under either of
at your option.
This crate is part of the OctoFHIR project.