Skip to main content

patient_compartment Validator Documentation

patient_compartment Validator Documentation
FHIR Compartment-Based Access Control for Patient Resources


Intent​

The patient_compartment validator restricts access to resources explicitly linked to the authenticated patient’s FHIR compartment. This ensures patients can only interact with their own medical data through client applications (e.g., mobile apps, patient portals).

Key Use Cases:

  • Patients viewing his observations (Observations).
  • Patients updating their allergy list (AllergyIntolerance).
  • Patients creating self-reported observations (Observation).

Key Features​

  1. Role Enforcement:
    • Validates that the client has the Patient role. Requests from non-patient roles (e.g., Practitioner) are denied.
  2. Compartment Membership Check:
    • Confirms the requested resource type belongs to the FHIR Patient compartment (e.g., Observation, Condition, MedicationRequest).
  3. Reference Validation:
    • For create/update operations, ensures the resource references the patient’s ID (e.g., Observation.subject = Patient/<id>).

Validation Logic​

StepActionError If Failed
1Check the caller's role is Patient.403 Forbidden (Invalid role)
2Verify the resource type is in the Patient compartment and not excluded from it.403 Forbidden (Invalid compartment)
3On read and search, verify the returned row references the caller in one of that type's compartment fields.403 Forbidden (Not in compartment)
4On create and update, verify the submitted payload references the caller as Patient/<id> in one of subject, patient, performer.403 Forbidden (Reference mismatch)
5On delete, fetch the stored resource and verify it references the caller.403 Forbidden

Steps 3 and 4 ask different questions of different data. Step 3 inspects the row the FHIR server returned, using the compartment definition's inclusion criteria for that resource type. Step 4 inspects what you submitted, using the fixed three-field list. A resource can be readable under this compartment and still impossible to write under it.

When the resource type is Patient itself, the row must be the caller's own record — a patient reaches Patient/<their own id> and nothing else. On update and delete this is checked against the id the request targets, so it holds even before any row is fetched.

Patient + update under this validator always denies

A rule of Patient / Patient / update / patient_compartment cannot be satisfied by any request. Step 4 requires the payload to name the caller in subject, patient or performer, and a Patient resource has none of those elements — there is nowhere to put the reference. The denial reads:

No valid patient reference found. Expected Patient/<id> in one of ['subject', 'patient', 'performer']

The same reasoning applies to any resource type lacking all three fields. Before pairing a compartment validator with a write operation, confirm the resource type actually carries one of the fields the validator looks in.


Example Configurations​

Validator names are matched exactly. patient_compartment is the name; PatientCompartment and Patient_Compartment are not recognised, and a rule naming one of those denies every request.

1. Read Access to Lab Reports​

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

Explanation: A patient can read a DiagnosticReport only if it references them — for that resource type, through DiagnosticReport.subject.

2. Create a Blood Pressure Observation​

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

Requirements: The Observation body must reference the patient in subject, patient or performer. One is enough:

"subject": {
"reference": "Patient/<patient_id>"
}

Otherwise, the request is denied. A request carrying no resource body at all is also denied, rather than passed through.


FHIR References​

  1. Patient Compartment Definition:
    FHIR R4 Patient Compartment (Official HL7 documentation).
  2. Resource Examples:

Use Case: Patient Portal Integration​

A diabetes management app uses this validator to let patients:

  1. View glucose readings: Reads Observation resources with code.text = "Glucose" in their compartment.
  2. Log exercise data: Creates Observation resources with subject set to their Patient ID.

Error Handling:

  • A patient trying to access another patient’s Procedure resource receives a 403 Forbidden response.