Firely FHIR validator in .NET: caught 120 of 180, and my bug

Firely's validator 3.6.0 rejected 120 of 180 defective FHIR resources, against 80 for HealthChain. Only 20 of the 100 content defects failed for the planted reason; the broken reference was rejected in 0 of 20.

Rogério Rodrigues

Rogério Rodrigues

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

Planta técnica azul: o Firely Validator 3.6.0 em .NET 8 reprovou 120 de 180 recursos FHIR R4B com defeito, contra 80 do HealthChain. Invariante obs-6: 20 de 20. Referência quebrada com UUID válido: 0 de 20. Outros 20 Bundles caíram porque o fullUrl urn:uuid:pac-000 do gabarito não é UUID.

Originally published in Portuguese on 17 November 2026: Portuguese version.

Firely’s validator, in C#, rejected 120 of 180 defective FHIR resources. On the same data, HealthChain had rejected 80. These are the same 240 synthetic resources from the HealthChain test. The 80 structural ones, again. And 40 of the 100 content ones.

But of those 40, only 20 failed because of the defect I planted: the obs-6 invariant. The other 20 failed because of a mistake of mine, in the test set. With that mistake fixed, the broken reference I wanted to test was rejected in 0 of 20. On the 60 valid resources, no false positives.

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

Where did the question come from?

On 30 October I showed that HealthChain 0.16.1’s validate_resource() catches structural errors and lets every content error through: invented LOINC code, unit outside UCUM, reference to a patient that doesn’t exist, CPF (Brazilian taxpayer ID) with a wrong check digit, and the obs-6 invariant. The module itself warns, in its docstring, that it doesn’t check code bindings, FHIRPath invariants, profiles or reference integrity, and tells you to use a FHIR server’s $validate.

That left the question: what about a real validator? I ran the one from the Firely .NET SDK on the same 240 files, with the same test set. When the parser rejects something, it returns the partial resource, and the validator runs on top of it.

How did I run it?

  • .NET 8 (runtime 8.0.34), CPU, no key, no account.
  • Hl7.Fhir.R4B 6.8.0 and Firely.Fhir.Validation.R4B 3.6.0, published on NuGet on 29 October 2026. Both BSD-3-Clause. The R4B specification package (Hl7.Fhir.Specification.Data.R4B 6.8.0) comes as a local specification.zip.
  • R4B, not R4. The 240 resources from demo 11 are R4B, and running with the R4 package could give a different result.
  • The data is a copy of the recursos.jsonl from demo 11, checked by SHA-256 in the test: 60 valid and 180 with one defect each, 20 per category, across 9 categories.
  • Local terminology only. No terminology server, no referenceResolver, no profile beyond the base.

Each resource goes through two layers: the SDK’s strict deserializer and the Validator. I count it as rejected if either one reports an error. The core of it, simplified:

var fonte = new CachedResolver(ZipSource.CreateValidationSource());
var terminologia = LocalTerminologyService.CreateDefaultForCore(fonte);
var validador = new Validator(fonte, terminologia);
var parser = new FhirJsonDeserializer();

var ok = parser.TryDeserializeResource(json, out var recurso, out var errosParser);
var issues = validador.Validate(recurso).Issue;
var reprovou = !ok || errosParser.Any() || issues.Any(i => i.Severity is OperationOutcome.IssueSeverity.Error
                             or OperationOutcome.IssueSeverity.Fatal);

What did I expect?

My bet, written down in the outline for this post: the obs-6 invariant and the broken reference inside the Bundle, the validator would catch. Invented LOINC and a unit outside UCUM, only with a terminology server. CPF, no, because the CPF check digit is not a FHIR rule.

I got obs-6 and CPF right. I got the reference wrong. And I got LOINC and UCUM right for the wrong reason, as you’ll see further down.

What came out?

Numbers from results/tabela.md, with the labels rewritten and HealthChain alongside:

Planted defect Type HealthChain 0.16.1 Firely 3.6.0
Required field missing structure 20 of 20 20 of 20
Date in dd/mm/yyyy structure 20 of 20 20 of 20
Field that doesn’t exist in the resource structure 20 of 20 20 of 20
Value outside the required set (code) structure 20 of 20 20 of 20
Invented code under the LOINC system content 0 of 20 0 of 20
Unit that isn’t valid UCUM content 0 of 20 0 of 20
Reference to a Patient that isn’t in the Bundle content 0 of 20 20 of 20 (*)
CPF with a wrong check digit content 0 of 20 0 of 20
Value and dataAbsentReason together (obs-6) content 0 of 20 20 of 20
Control: valid resource – 0 of 60 0 of 60

(*) For the wrong reason. I explain below.

Structure: 80 of 80. Content: 40 of 100. Total: 120 of 180.

Two things the table doesn’t show and raw.jsonl does. First, the parser alone already rejects the 80 structural ones. For those four defects, you don’t even need the validator. Second, obs-6 is the only category that only the validator catches. The parser lets all 20 through, and the validator answers like this:

Error 1012 Observation
Instance failed constraint obs-6 "dataAbsentReason SHALL only be present if Observation.value[x] is not present"

That is the real gain over HealthChain: a FHIRPath invariant from the base profile, actually checked.

What mistake did the validator find in my test set?

When I saw 20 of 20 on the broken reference, I went to read the message. It wasn’t the reference:

string 'urn:uuid:pac-000' is not a correct literal for a uri. At Bundle.entry[0].fullUrl

I’ll admit this mistake is mine. When I built the Bundles for demo 11, I used urn:uuid:pac-000 as the fullUrl. It looks like a UUID, but it isn’t. A urn:uuid: needs a real UUID, with 32 hexadecimal digits. HealthChain accepted it without complaint, and I didn’t even notice. Firely rejected it in the parser, before looking at the reference.

In other words: the 20 of 20 measures the fullUrl, not the reference. To measure the reference, I added a control. I replaced the fullUrl with a valid UUID (derived from the id, so it’s reproducible) and ran the 20 Bundles in two variants:

  • A: the Observation points to Patient/fantasma-N, which isn’t in the Bundle. This is the defect I wanted to test.
  • B: the Observation points to the fullUrl of the Patient that is in the Bundle. This is the control; it should pass.

Result from controle-referencia.jsonl: A, 0 of 20 rejected and 0 with a warning. B, 0 of 20. With the default configuration, a reference to a patient that doesn’t exist passes clean.

I checked the specification package itself to see whether this is a bug. By the base profile, it isn’t. The conferir_perfil.py script reads the specification.zip that NuGet brought in and lists the Bundle invariants in the R4B base profile (results/perfil-base.txt). None of them requires an entry’s reference to resolve to another entry. My Bundles are of type collection. The validator followed the base profile.

It’s like a door guard who checks that the badge is in the right format and doesn’t check whether the name is on the list in his hand. The wrong format, he caught. The name that isn’t on the list, he didn’t, because nobody told him to check.

What does the validator NOT catch on its own?

  • A reference that doesn’t resolve. No Bundle invariant requires it. Without a referenceResolver and without a profile that requires aggregation bundled, nobody checks. I didn’t test either of them; with them the result may change.
  • Invented LOINC code. In the base profile, the binding for Observation.code is example only (example). That’s why FC-0000 under http://loinc.org passed without a warning. My bet was “missing terminology server”. That may also be missing, but the first reason is the profile. I didn’t test with a server.
  • Unit outside UCUM. Quantity.code has no binding in the base profile, and the only rule is qty-3: if there’s a code, there must be a system. batimentos/minuto with the UCUM system meets the rule and passes.
  • CPF with a wrong digit. It’s not a FHIR rule. Only a Brazilian profile with its own rule would catch it, and I didn’t validate against a BR profile. You check the digit outside FHIR, as I showed in the HealthChain post.

In other words: to catch content errors, the validator needs a profile that asks for it. The base profile asks for little.

How much does it cost in the pipeline?

Building the validator took 240 ms. The first resource of each type pays for compiling the profile: the first Patient took 2.4 s, the first Observation 2.9 s, the first Condition 0.29 s and the first Bundle 0.33 s. Excluding the first resource, the median was 1.42 ms per resource. The 240 took 6.3 s in total. I ran it two more times on the same machine and the median stayed between 1.37 and 1.59 ms, with the same counts (results/rodadas.txt).

For an API that validates on input, this calls for a warm validator, created once and reused. But the package README warns: the Validator and the resolvers are not thread-safe. On a server with multiple threads, it’s one instance per thread.

Who is it for, and when wouldn’t I use it?

It’s for people already on .NET who want to validate FHIR in-process, without calling a server: the four structural defects I tested, required bindings and the base profile’s obs-6 invariant, at about 1.4 ms per resource once warmed up. It did better than HealthChain on what HealthChain says it doesn’t do, and it got the 60 valid ones right.

What wasn’t good: in the default configuration it doesn’t check whether the reference resolves, and I expected it to. That was part of my bet. But the base profile doesn’t even ask for it.

I wouldn’t use it with the base profile alone when the risk is a wrong clinical code: the invented LOINC passes. That’s where a profile that pins down the code comes in and, probably, a terminology server. I also wouldn’t use it in a pure Python pipeline just for this. There, the way to go is a FHIR server’s $validate or the HL7 Java validator, which I didn’t run here.

For me, the most useful finding was something else: my test set had a defect that one validator let through and the other caught.

Limitations

  • The test set is mine. 9 categories, 20 cases each, chosen by me. The 120 of 180 describes this set, not real-world FHIR.
  • Default configuration: R4B base profile, local terminology, no reference resolver, no custom profile. Only the POCO path, with the strict deserializer. I didn’t test permissive mode or Validate(ElementNode).
  • I didn’t run the HL7 Java validator or a terminology server.
  • Timings from one cloud machine, CPU. Yours will differ.
  • All data is synthetic. The CPF passes the check digit, but it’s drawn with a fixed seed and belongs to nobody.

Source and how to reproduce

Clone https://github.com/rogeriorrodrigues/nursia-research-lab and go into demos/15-firely-validacao-fhir. You need the .NET 8 SDK, and Python only for the test. Three commands:

dotnet run -c Release --project src -- data/recursos.jsonl results
pip install -r requirements.txt
python3 -m pytest tests -q -s

The test runs the demo from scratch in a temporary folder and checks the numbers in this post: 80 of 80, 40 of 100, 0 of 60 and the reference control at 0 of 20.

How about you: do you validate FHIR in the pipeline or only at the API entry point? And does anyone out there turn on the reference resolver in production?

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