AI Engineer Problems › Evaluation & Testing

FHIR JSON Schema Validator

Medium · Coding · Evaluation & Testing · Asked at Flatiron Health, Google Health

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:

  1. Must have "resourceType" matching either "Patient" or "Observation".
  2. Must have "id" (string) containing only alphanumeric characters and hyphens.
  3. 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).
  4. 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

Solve it in the browser on GenAIDojo