Skip to main content

FHIR Compartment Validation in Medbackend

Core Concept: Logical Resource Grouping​

FHIR compartments provide virtual resource organization without physical storage changes. Think of them as dynamic filters that:

  • Group related resources through reference relationships
  • Enable context-specific access control
  • Maintain original resource locations while creating logical views

FHIR Compartment Fundamentals​

Key Characteristics​

  1. Reference-Based Membership
    Resources join compartments through specific reference fields (e.g., subject, patient)
  2. Multi-Compartment Resources
    A single resource can exist in multiple compartments simultaneously
  3. Dynamic Membership
    Compartment relationships update automatically with resource modifications

Common Compartment Types​

CompartmentCaller identityExample member resources
PatientPatient/<id>Observation, ServiceRequest, DiagnosticReport
PractitionerPractitioner/<id>Observation, ServiceRequest, DiagnosticReport
RelatedPersonRelatedPerson/<id>Observation, Communication, DocumentReference
DeviceDevice/<id>Observation, DeviceUseStatement, DeviceRequest

Which fields put a resource in a compartment is fixed by the FHIR R4 compartment definitions, per resource type, and it varies more than you might expect:

ResourceIn the Patient compartment viaIn the RelatedPerson compartment via
Observationsubject, performerperformer
Procedurepatient, performerperformer
Encounterpatientparticipant
CarePlanpatient, performerperformer
Communicationsubject, sender, recipientsender, recipient

Note that Observation uses subject while Procedure and Encounter use patient, for the same compartment. Do not assume a single field applies across resource types. You do not configure these paths, and you cannot extend them from a rule. Each compartment also has an excluded list — resource types that are never members, such as Organization and Practitioner in the Patient compartment — and an exclusion is checked before anything else.

Medbackend Validation Implementation​

Configuration Example​

{
"client_role": "Patient",
"entity_name": "Observation",
"operation": "read",
"validator": "patient_compartment"
}

Configuration Breakdown:

  • client_role: the caller's role, as carried in their token — Patient, Practitioner, Device, RelatedPerson
  • entity_name: the FHIR resource type this rule governs
  • operation: one of create, read, update, delete, search
  • validator: which of the ten validators decides the request
A rule has exactly these four keys, plus the optional role gate

The only other keys a validation rule may carry are required_role_system and required_role_code, which work as a pair.

There is no reference_paths key, no validators array, and no validation_logic setting. A rule names one validator, and the compartment fields come from the FHIR compartment definition for that resource type — not from anything you write in the rule. Extra keys are ignored silently, so a rule that appears to configure something is often configuring nothing.

Nor is there a wildcard. All three of client_role, entity_name and operation are compared as exact strings, so "*" matches nothing.

Validation Workflow​

  1. Extract the caller's identity and role from the access token.
  2. Find every rule matching this client_role / entity_name / operation.
  3. Run each matching rule's validator. All of them must pass.
  4. Before fetching anything, decide on the resource type alone — a refused request never reaches the FHIR server.
  5. On a read or search, re-run the check against each returned row, and drop rows that are not in the caller's compartment.
  6. On a write, check the submitted payload — and for updates and deletes, the stored resource too.

References are compared as resources, not as strings. Patient/123, Patient/123/_history/2 and an absolute URL ending in Patient/123 all denote the same patient and all match. A comparison on raw strings would deny legitimate callers over spelling alone, so it is not done that way.

Advanced Compartment Patterns​

Combining conditions​

Since every matching rule must pass, writing two rules for one triple gives you AND — the request must satisfy both validators. There is no OR: you cannot express "in the practitioner's compartment or in their organization's" as a pair of rules, because the second rule would restrict rather than widen.

Where you need a broader reach than one compartment gives, use a validator whose own definition is broader — organization_compartment for everything in a clinic, legitimate_interest for cross-organizational access, general_practitioner for the GP relationship — rather than trying to compose narrow ones.

Some resource types have no field connecting them to any organization. For those, the organization-compartment extension lets you tag the resource explicitly. See Organization Compartment — note in particular that the tag is honoured on reads and ignored on writes.

Troubleshooting Compartment Validation​

Common issues:

  1. A write is denied although the resource plainly concerns the caller. The write check looks only in that compartment's write fields, and the resource type may not have any of them. See What a compartment validator asks of a write.

  2. A search returns nothing, with no error. Rows outside the caller's compartment are filtered out rather than raising. An empty result usually means the rows exist but do not reference the caller — check the compartment field on the data, not the rule.

  3. Every request for a triple is denied. Check the rule exists for that exact triple, that the validator name is spelled exactly, and that default_access is exactly "Forbidden" or "Allowed". None of these produce a load-time error when wrong.

  4. A rule that previously worked now denies. Compartment membership is evaluated live against the data. A reference edited out of a resource removes it from the compartment immediately.