Skip to content

fix(fhirpath): evaluate standard R4 invariants (.not(), %resource, value[x]) - #55

Merged
awbx merged 1 commit into
mainfrom
fix/invariant-not-resource-choice
Nov 16, 2026
Merged

awbx merged 1 commit into
mainfrom
fix/invariant-not-resource-choice

Conversation

@awbx

@awbx awbx commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Closes #54.

Problem

On main, 42 of the 242 R4 invariants fail to compile in the invariant engine. The generated --validator native / zod schemas turn invariants on by default, so validation throws. dom-3 (%resource) is inherited by every DomainResource, which makes this widespread: 2,802 of the 2,911 official R4 examples crash validation.

Causes:

  • .not(): not is lexed as a keyword, so .not() doesn't parse (18 invariants)
  • %resource / %rootResource: % is rejected by the lexer, and compileInvariant didn't catch lexer errors (14)
  • | union operator (8), infix is (2), infix contains (2)
  • value didn't resolve valueQuantity etc., so obs-6 passed when it should fail

The examples also showed a pre-existing generator bug. For repeating elements, property-level invariants ran against the array instead of each item, so ext-1 rejected every resource with extensions.

Changes

Invariant engine (packages/fhirpath/src/invariant/)

  • Keywords are allowed as member names after .
  • %resource, %rootResource, %context, %ucum, %sct, %loinc
  • |, infix is / as, infix contains
  • Choice-type navigation: value → valueQuantity. It matches only known [x] bases (R4–R6) with a FHIR type-name suffix, so reference never resolves to referenceRange
  • is() / as() / ofType(), union, combine, intersect, exclude, isDistinct, subsetOf, supersetOf, children, descendants, trace, toInteger, substring, lower, upper, and string +
  • compileInvariant wraps lexer errors. validateInvariants reports an expression it can't compile as a warning instead of throwing

No new false errors. When the JSON can't settle the answer, the engine returns indeterminate. Generated schemas ignore indeterminate results; they don't reject on them.

  • %resource on an element evaluated outside its resource (e.g. ref-1 on Reference)
  • is canonical vs is uri on a string
  • Date/dateTime comparison uses instants and precision, not string order. Overlapping precision (2020-02-15 vs 2020-02) is indeterminate. This matters because per-1 sits on every Period

Generator: a repeating property's invariants now refine each item rather than the array.

Verification

All 146 R4 resources were generated with --validator native, and every official R4 example (examples-json.zip) was run through them:

main this PR
R4 invariants that fail to compile 42 / 242 0 / 242
Examples that crash validation 2,802 0
Valid examples falsely rejected by invariants ext-1 ×484, drq-2 ×29, inv-1 ×3 0

Remaining invariant errors were checked against the data and are real violations in HL7's examples: que-1 ×86 (groups with no items; 86/86 match), sdf-4 ×4 (logical models with no baseDefinition), and bdl-7 ×1 (duplicate fullUrls).

  • 16 regression tests and 1 emitter test added. The existing ext-1 test expected { valueString } to fail, which encoded the bug, so it was corrected
  • Full suite, typecheck and lint pass

Follow-ups (not in this PR)

  • The full engine's choice-type allowlist (src/eval/nav.ts) is missing about 50 R4 bases (answer, probability, subject, …)
  • resolve(), htmlChecks() and replaceMatches() are still unsupported. They are reported as indeterminate
  • Generated schemas re-parse invariants on every validate() call; compiled invariants could be cached

…lue[x])

Native/zod validators crashed on 2,802 of the 2,911 official R4 examples:
42 of 242 R4 invariants failed to compile, including dom-3 on every
DomainResource.

Invariant engine:
- accept keywords as member names after "." so `.not()` parses
- lex and evaluate %resource, %rootResource, %context, %ucum, %sct, %loinc;
  %resource on an element outside its resource is indeterminate
- add the `|` union, infix `is`/`as` and `contains` operators
- resolve choice types (`value` -> `valueQuantity`), gated on known [x]
  bases plus a FHIR type suffix
- is()/as()/ofType(), union/combine/intersect/exclude, isDistinct,
  subsetOf/supersetOf, children/descendants, trace, toInteger, substring,
  lower/upper, string `+`
- compare date/dateTime values by instant and precision; overlapping
  precision is indeterminate instead of a string comparison
- compileInvariant wraps lexer errors; validateInvariants reports an
  unsupported expression as a warning instead of throwing

Generator: apply a repeating property's invariants to each item, not the
array. ext-1 rejected every resource with extensions.

Closes #54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validateInvariants fails on standard FHIR R4 constraints (.not(), %resource, and value[x])

1 participant