Requiring a Practitioner Role
A validation rule normally decides access from the caller's identity and the resource in front of it. Sometimes that is not enough: you want a rule to apply only to clinicians who hold a particular role — a doctor, a nurse, a pharmacist — rather than to every practitioner in the project.
Two optional keys on a rule express that:
{
"client_role": "Practitioner",
"entity_name": "MedicationRequest",
"operation": "create",
"validator": "patient_compartment",
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "doctor"
}
The caller's roles are looked up on the FHIR server. If none of them carries this system/code pair, the rule fails and the request is denied — regardless of what the validator would otherwise have decided.
Which codes you may use
required_role_system names the CodeSystem that defines the code, and required_role_code names a code in it. The two are compared as exact strings against the caller's PractitionerRole.code.coding entries.
HL7's practitioner-role CodeSystem
http://terminology.hl7.org/CodeSystem/practitioner-role defines exactly six codes, and no others:
| Code | Meaning |
|---|---|
doctor | Doctor |
nurse | Nurse |
pharmacist | Pharmacist |
researcher | Researcher |
teacher | Teacher/educator |
ict | ICT professional |
Because that set is complete and enumerable, the platform refuses to publish a project whose rule names this system with any other code. The error names the code you used and lists the six that exist:
requires role 'prescriber', which HL7's practitioner-role CodeSystem does not
define. It contains exactly ['doctor', 'ict', 'nurse', 'pharmacist',
'researcher', 'teacher']. A code outside that set matches no PractitionerRole
and denies every caller.
That refusal is the reason a typo here is a publish failure rather than a rule that silently denies every request forever.
When six codes are not enough
Roles like "prescriber", "lab supervisor" or "records officer" are not in HL7's six. Two supported options:
- SNOMED CT —
http://snomed.info/sct. HL7's practitioner-role ValueSet admits several hundred SNOMED occupation codes, which covers most job titles the six do not. - Your own CodeSystem — a URL you control, with codes you define. Defining local role codes is legitimate FHIR.
Neither is validated at publish time, because neither set can be enumerated in advance. The platform does not pretend to check what it cannot enumerate — so with either of these, a typo is a rule that denies every caller, and the verification step below is the only thing that will tell you.
A ValueSet selects codes; it does not define them, and no conformant PractitionerRole carries a ValueSet URL in code.coding.system. Publishing is refused if required_role_system looks like a ValueSet URL.
Surrounding whitespace is refused too — "doctor " would compare unequal to every stored coding and deny every caller, so it is rejected at publish rather than allowed to become a silent permanent denial.
The three conditions
This is the part that catches people out. The role check runs only when all three of these hold:
required_role_systemis present as a direct key of the rulerequired_role_codeis present as a direct key of the rule- the caller is a practitioner
Miss condition 3, or leave both keys out entirely, and the check does not run: the rule applies to every practitioner the triple's other rules do not claim (see Several roles on one triple). Supply one key and not the other — or supply both with empty or null values — and the rule denies every practitioner instead. The two outcomes are opposite, so it matters which one you have; omitted and present but empty are not the same thing here.
The keys are read from the top level of the rule, beside validator. Put them inside a params object — or any other nested object — and neither is read. The rule then declares no role requirement at all and grants every caller the validator allows, which is more than the rule appears to permit. It is the one misconfiguration here that gives away access rather than withholding it.
{
"client_role": "Practitioner",
"entity_name": "Patient",
"operation": "update",
"validator": "legitimate_interest",
"params": {
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "doctor"
}
}
Every practitioner reaching that rule is treated as though it carried no role requirement. Move both keys up beside validator.
Saving a project refuses this shape, and the error names the path of each misplaced key. That check runs on what a save submits, so a rule which has not been through a save since it shipped has not met it, and the behaviour above is still what such a rule does. Put the project through a save, or use the verification request below.
A rule carrying only required_role_system, or only required_role_code, is refused at the point the role gate runs: every practitioner is denied, and the rule is unusable rather than ungated.
This reversed in 2026. It previously behaved like a rule carrying neither key — granting unconditionally — so documentation written before the change says so.
Role gating is evaluated against the caller's PractitionerRole resources, so it only ever applies to a practitioner. The pair on a Patient, Device or RelatedPerson rule is silently ignored — those callers hold no PractitionerRole, and a complete pair does not deny them either. The rule reads as though it is restricted to a subset of callers; it is not.
{
"client_role": "Device",
"entity_name": "Observation",
"operation": "create",
"validator": "device_compartment",
"required_role_system": "http://example.org/device-roles",
"required_role_code": "analyzer"
}
The two role keys above do nothing. Every Device caller reaching this rule is treated identically. If you need to distinguish devices, distinguish them in the data the validator inspects, not here.
What is checked for you
Five shapes are refused, at two different moments, and the difference decides how early you find out.
Saving a rule rejects a pair nested below the rule instead of set on it. The error names the path of each misplaced key; it does not name the rule.
Publishing the project rejects a half pair, a value with stray whitespace around it, a required_role_system that is a ValueSet URL, and a code that HL7's practitioner-role CodeSystem does not define. Each error names the offending rule by its client_role, resource and operation. These four can therefore reach the database and fail at the publish rather than at the keystroke — a single-rule save answers success and the refusal arrives the next time the project is published.
The nesting check is the one that runs earlier because it is the only one of the five that widens access. The other four describe a rule that would deny every practitioner reaching it.
The rest is unchecked. A gate on a rule for callers who are not practitioners, and a code that is well formed but wrong for your data — a plausible SNOMED concept, or one under your own CodeSystem that no PractitionerRole actually carries — pass every check there is. So does any configuration that reached the platform without being saved through the dashboard.
Verify it, do not assume it
Configuration that has never been tested against a caller who lacks the role has not been tested at all.
The check is one request:
- Obtain a token for a practitioner who does not hold the role.
- Issue the operation the rule governs.
- Confirm a denial.
If it succeeds, one of the three conditions above is unmet. A test that only ever presents a practitioner who does hold the role passes identically whether the gate works or is doing nothing, which is why it is not sufficient on its own.
Where the roles come from
Roles are read from the caller's PractitionerRole resources on the FHIR server, matched on the coding system and code you specify. Three consequences follow:
- The role must exist as data before the rule can pass. A practitioner with no matching
PractitionerRoleis refused, which is the correct behaviour but looks like a broken rule if the data was never created. - Only
activeroles count. The lookup filters onactive=true, so aPractitionerRolethat exists but is inactive does not satisfy the gate. Deactivating a role is a working revocation; it does not have to be deleted. - Role changes take effect immediately. Membership is evaluated per request rather than baked into the token, so revoking a role denies the next request without waiting for the token to expire.
If the role lookup cannot be completed at all, the request is denied. The gate fails closed.
Combining with a validator
The role gate does not replace the validator — both must pass. The rule above requires the caller to be a doctor and the MedicationRequest to be in the patient compartment the caller may reach.
This composes the way you would expect, and it is the intended way to express "clinicians with this role, acting within their normal scope":
[
{
"client_role": "Practitioner",
"entity_name": "Observation",
"operation": "read",
"validator": "practitioner_compartment"
},
{
"client_role": "Practitioner",
"entity_name": "Observation",
"operation": "delete",
"validator": "practitioner_compartment",
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "ict"
}
]
Any practitioner may read observations within their compartment; only an ICT professional may delete one. Note that these are two separate rules for two separate operations — the role requirement applies to the triple it is written on, and nothing else.
Several roles on one triple
One client_role / resource / operation triple may carry several rules, and for a practitioner the role keys decide how they combine.
If no rule on the triple names a role, every rule must pass. They combine with AND, and adding a rule can only narrow access.
If any rule on the triple names a role, the rules are grouped by their required_role_system / required_role_code pair, and the rules with no pair form the other practitioners group. Then, for a practitioner caller:
- The caller's active
PractitionerRolecodings determine which of the triple's roles they hold. - If the caller holds one or more of the triple's roles, the request is allowed when any of those roles' groups passes. Within a group every rule must pass. The other-practitioners group is not consulted for a role holder.
- If the caller holds none of the triple's roles, the other-practitioners group decides. If the triple has no such group, the request is denied.
[
{
"client_role": "Practitioner",
"entity_name": "Observation",
"operation": "update",
"validator": "practitioner_compartment",
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "doctor"
},
{
"client_role": "Practitioner",
"entity_name": "Observation",
"operation": "update",
"validator": "allowed",
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "ict"
},
{
"client_role": "Practitioner",
"entity_name": "Observation",
"operation": "update",
"validator": "forbidden"
}
]
A doctor may update observations in their own compartment. An ict professional may update any observation. A practitioner who is both passes if either group passes, so they may update any observation. A nurse, who holds neither role, reaches the other-practitioners group and is denied by forbidden. A triple with role rules and no other-practitioners group denies everyone outside those roles.
Three things do not change under this grouping:
- Callers who are not practitioners see every rule on their triple combined with AND, role keys or not, as described above.
- The platform's own rules (system rules) apply to every caller on their triple, in addition to whichever group decided, so a project rule cannot grant past them.
- A half pair and a failed role lookup still deny, exactly as on a single rule.
So "a doctor or a nurse may do this" is two rules on the triple, one per role, each with the validator that role should get. Writing the same validator twice with two roles is the plain way to say "either role".
Within one group, every rule must pass: adding a rule to a role's group never grants that role more. Adding a rule for a new role grants that role access it did not have, and that is the point. Rules for different roles are managed per role in the dashboard.
Related
- Validation rules — the rule format and how rules are matched
- Making requests — confirming which identity a token resolves to