Repository navigation
fix(fhirpath): evaluate standard R4 invariants (.not(), %resource, value[x]) - #55
Merged
Merged
Conversation
…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
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #54.
Problem
On
main, 42 of the 242 R4 invariants fail to compile in the invariant engine. The generated--validator native/zodschemas 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():notis lexed as a keyword, so.not()doesn't parse (18 invariants)%resource/%rootResource:%is rejected by the lexer, andcompileInvariantdidn't catch lexer errors (14)|union operator (8), infixis(2), infixcontains(2)valuedidn't resolvevalueQuantityetc., soobs-6passed when it should failThe examples also showed a pre-existing generator bug. For repeating elements, property-level invariants ran against the array instead of each item, so
ext-1rejected every resource with extensions.Changes
Invariant engine (
packages/fhirpath/src/invariant/).%resource,%rootResource,%context,%ucum,%sct,%loinc|, infixis/as, infixcontainsvalue→valueQuantity. It matches only known[x]bases (R4–R6) with a FHIR type-name suffix, soreferencenever resolves toreferenceRangeis()/as()/ofType(),union,combine,intersect,exclude,isDistinct,subsetOf,supersetOf,children,descendants,trace,toInteger,substring,lower,upper, and string+compileInvariantwraps lexer errors.validateInvariantsreports an expression it can't compile as a warning instead of throwingNo 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.
%resourceon an element evaluated outside its resource (e.g.ref-1onReference)is canonicalvsis urion a string2020-02-15vs2020-02) is indeterminate. This matters becauseper-1sits on everyPeriodGenerator: 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:mainext-1×484,drq-2×29,inv-1×3Remaining 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 nobaseDefinition), andbdl-7×1 (duplicatefullUrls).ext-1test expected{ valueString }to fail, which encoded the bug, so it was correctedFollow-ups (not in this PR)
src/eval/nav.ts) is missing about 50 R4 bases (answer,probability,subject, …)resolve(),htmlChecks()andreplaceMatches()are still unsupported. They are reported as indeterminatevalidate()call; compiled invariants could be cached