HealthChain FHIR validation test: 80 of 180 defects caught

HealthChain 0.16.1 validate_resource() rejected 80 of 180 defective synthetic FHIR resources: all 80 structural defects and 0 of the 100 content defects, such as a CPF with a wrong check digit.

Rogério Rodrigues

Rogério Rodrigues

Engenheiro de IA · mestrando em Informática em Saúde (UFSC)

Boletim de correção em papel pautado: 80 de 180 defeitos pegos pelo validate_resource() do HealthChain, todos de forma. Campo obrigatório, data, campo desconhecido e valor fora do conjunto 20 de 20; LOINC inventado, unidade UCUM, referência quebrada, CPF com dígito errado e invariante obs-6 0 de 20.

Originally published in Portuguese on 30 October 2026: Portuguese version.

I ran 240 synthetic FHIR resources through validate_resource() from HealthChain 0.16.1. 60 were valid and 180 had one known defect each. It rejected 80 of the 180: all 80 with a structural defect, meaning a missing required field, a date in dd/mm/yyyy, a field that does not exist and a gender outside the allowed set. Of the 100 with a content defect, it rejected none. A CPF (Brazilian taxpayer ID) with a wrong check digit passed as valid. On the 60 correct resources, 0 false positives.

Code, data and recorded output: demos/11-healthchain-validacao-fhir in nursia-research-lab.

How I ran it

  • HealthChain 0.16.1 (pip install healthchain==0.16.1), Apache-2.0, Python 3.11, CPU. Under the hood it uses fhir.resources 8.1.0. Both versions are pinned in requirements.txt.
  • I generated 240 FHIR R4B resources with a fixed seed: Patient, heart rate Observation (LOINC 8867-4) and Condition (ICD-10).
  • 60 are valid and act as the control. The other 180 have one defect each, 20 per category, across 9 categories. The defects and the categories are my choice. The 20 broken-reference cases are Bundles (one Patient and one Observation). The rest are standalone resources.
  • Each resource went through validate_resource(), which returns a report with valid and the list of issues. I counted the rejected ones (valid == False).

The patients’ CPFs pass the check digit, but they are random and belong to nobody. Names come from a short made-up list.

Results

Defect introduced Type Rejected
Missing required field structure 20 of 20
Date in dd/mm/yyyy structure 20 of 20
Field that does not exist in the resource structure 20 of 20
Value outside the required set (gender, status) structure 20 of 20
Made-up code under the LOINC system content 0 of 20
Unit that is not valid UCUM content 0 of 20
Reference to a Patient that is not in the Bundle content 0 of 20
CPF with wrong check digit content 0 of 20
Value and dataAbsentReason together (invariant obs-6) content 0 of 20
Control: valid resource – 0 of 60

Structure: 80 of 80. Content: 0 of 100. I call “structure” whatever the Pydantic model or the primitive code binding rejects, and “content” everything else. Since the label follows what the module says it checks, the result confirms the docstring and discovers nothing new. Average time of about 0.2 ms per resource: the recorded run gave 0.17 ms, and across runs and machines I saw 0.11 to 0.25 ms. The time changes every run. The rest is deterministic.

What does it catch?

Two kinds of error: what the Pydantic model in fhir.resources rejects (missing field, badly written date, unknown field) and what HealthChain checks next, which is the values of code fields with a required value set. A gender written as “feminino” falls into the second case. I build the output from the report’s expression, code and diagnostics:

Patient.gender (code-invalid)
Value 'feminino' is not in the required value set. Allowed values: male, female, other, unknown

The full message is in results/exemplo.txt. Each issue comes with the field path (Patient.gender, Observation.status) and a code from the FHIR OperationOutcome vocabulary. The report converts to an OperationOutcome with to_operation_outcome(). My reading is that an agent that builds FHIR resources could use the field path to fix what it got wrong. I did not test this with an agent.

What does it let through?

The five content defects come back as valid=True, with an empty issue list. An example with Brazilian data:

from healthchain.fhir import validate_resource

paciente = {
    "resourceType": "Patient",
    "identifier": [{
        "system": "http://www.saude.gov.br/fhir/r4/NamingSystem/cpf",
        "value": "44797837682",   # wrong check digit
    }],
}
rel = validate_resource(paciente)
print(rel.valid, rel.issues)    # True []

That is one of the CPFs with a wrong digit from the demo, and the validator did not look at the identifier value in any of the 20 cases. The same goes for the made-up LOINC code, the non-UCUM unit and the reference to a Patient that does not exist in the Bundle. Invariant obs-6 (an Observation with a value and a reason for missing data) also passed in all 20.

The docstring of healthchain/fhir/validation.py lists what is not checked: CodeableConcept and Coding bindings, FHIRPath invariants, profile conformance and reference integrity. For full validation, the module itself tells you to use the $validate operation of a FHIR server. Three of the five content categories fall under those items: the made-up LOINC code, the broken reference and obs-6. The CPF with a wrong digit and the non-UCUM unit are not on the list. Those are my own extension. The demo measures that slice: 80 of 180 in this set.

What would I do?

  • Use validate_resource() as a structural gatekeeper on the output of an agent that generates FHIR. It is fast and gives you the path of the error.
  • Do not treat valid=True as “the FHIR is correct”. For content, run the resource through a real validator, such as the official HL7 validator or your server’s $validate, and use the profile your case requires. I ran neither in this demo.
  • For CPF, validate the check digit outside FHIR. It takes a few lines, and these do not handle repeated sequences (11111111111) or a formatted CPF:
def digito_cpf(base):
    soma = sum(d * p for d, p in zip(base, range(len(base) + 1, 1, -1)))
    resto = (soma * 10) % 11
    return 0 if resto == 10 else resto

def cpf_confere(cpf):
    d = [int(c) for c in cpf]
    return len(d) == 11 and digito_cpf(d[:9]) == d[9] and digito_cpf(d[:10]) == d[10]

Limits

  • The answer key is mine. I chose the 9 categories and built the defect for each one. With other categories the ratio would be different. The 80 of 180 describes this set, not the error rate of real-world FHIR.
  • I did not compare against the official HL7 validator. The demo tells you what validate_resource() lets through, not what another validator would catch.
  • No Brazilian profile. I used the CPF identifier system from Brazil’s Ministry of Health only to build the data.
  • I only tested validate_resource(), on standalone resources and on a hand-built Bundle. I did not test load_bundle() or the rest of the SDK (gateway, MCP, sandbox).
  • 240 synthetic resources, one seed, FHIR R4B, three resource types and Bundle. The 0 of 60 false positives holds for the control I generated myself.

Source and how to reproduce

Clone https://github.com/rogeriorrodrigues/nursia-research-lab and go into demos/11-healthchain-validacao-fhir. From there it is three commands:

pip install -r requirements.txt
python3 demo.py
python3 -m pytest tests -q

The demo is part of the NursIA research repository (PPGINFOS/UFSC, FAPESC grant).

Conhece alguém que precisa ler isso?

Manda pra essa pessoa. Leva dez segundos e é o que mais ajuda esse trabalho a chegar em quem usa IA com dado de verdade.

Rogério Rodrigues

Rogério Rodrigues

Engenheiro de IA há 20 anos em software, mestrando em Informática em Saúde na UFSC. Escrevo o que rodei: IA com dado de saúde, FHIR e LGPD, com código aberto.

Ficou uma dúvida? Discorda de algo? Me chama.

Ver publicações científicas