FHIR JSON Schema Validator
Clinical intelligence extractions require structured outputs conforming to FHIR schemas.
Write a function validate_fhir_record(record) that validates a dictionary against a simplified FHIR specification:
- Must have
"resourceType"matching either"Patient"or"Observation". - Must have
"id"(string) containing only alphanumeric characters and hyphens. - If
"Patient":"gender"must be one of"male","female","other","unknown"."name"must be a list of dictionaries, where each has"given"(list of strings) and"family"(string).
- If
"Observation":"status"must be a non-empty string."code"must contain a"coding"list of dictionaries, each having"system"(string) and"code"(string)."valueQuantity"must contain"value"(int or float) and"unit"(string).
If validation fails, raise a ValueError with descriptive text. Return True if valid.
Example
record = {
"resourceType": "Patient",
"id": "pat-001",
"gender": "female",
"name": [{"given": ["Jane"], "family": "Smith"}]
}
validate_fhir_record(record)
# Returns: True