openehr-rm 0.0.128

openEHR RM Reference Model, generated from BMM: generations v1_1 (RM 1.1.0) and v1_2 (RM 1.2.0, current)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// @generated-from-template templates/openehr-rm/validate/terminology.rs — DO NOT EDIT; edit the source and re-run `openehr-codegen -- emit`.
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-FileCopyrightText: openEHR Foundation
// SPDX-License-Identifier: Apache-2.0
//! The **terminology-backed RM class invariants**.
//!
//! These are the RM invariants whose
//! evaluation needs a terminology-group or code-set membership lookup, which
//! the generated invariant cores cannot express mechanically (the BMM
//! assertion dialect calls out to `terminology (…)` / `code_set (…)`).
//!
//! Two families of terminology binding are enforced, both properties of the RM
//! instance value alone (independent of any archetype / template):
//!
//! 1. **openEHR terminology *group* codes** (`has_code_for_group_id`) — the
//!    `defining_code` of a coded slot must be a member of the openEHR group
//!    fixed by the owning RM type. These carry `terminology_id = "openehr"`; a
//!    non-`openehr` terminology binding is out of scope for the openEHR-group
//!    check and is skipped (matching `terminology (Terminology_id_openehr)…`).
//! 2. **openEHR / ISO / IANA code-set codes** (`code_set (id).has_code`) — a
//!    `CODE_PHRASE` (or bare code) whose code value must be a member of the
//!    external or internal code set fixed by the invariant. The RM invariant
//!    here has **no** `terminology_id` guard, so the code value is validated
//!    against the set directly.
//!
//! This module is the single source of the slot → vocabulary binding table
//! ([`slots_for`]) plus the membership decisions ([`Group`]/[`CodeSet`], backed
//! by [`openehr_term::bundle`], TERM 3.1.0). Two presentation adapters consume
//! it: [`validate_rm_terminology`] (the core form, emitting
//! [`InvariantViolation`]s — what the `openehr-its` wire-boundary dispatcher
//! runs as a post-core check) and the `openehr-its` RM-instance terminology
//! pass (its own `ValidationKind::Terminology` message rendering). Neither
//! re-derives the bindings.
//!
//! # Spec
//!
//! Each entry cites the RM class-invariant source
//! (`docs/specs/openehr/RM/docs/UML/classes/org.openehr.rm.*.adoc`) and the
//! openEHR terminology group / code-set id it resolves against (defined in the
//! openEHR Terminology, `docs/specs/openehr/TERM/docs/SupportTerminology/`).
//! The bundle codes themselves come from the vendored TERM assets in
//! [`openehr_term::bundle`].

#![expect(
    clippy::disallowed_types,
    reason = "the wire-boundary validation reads the canonical JSON node before the typed decode \
              (#1694 boundary class)"
)]

use openehr_base::validate::InvariantViolation;
use openehr_term::bundle::{OpenehrTerminology, openehr};
use serde_json::Value;

/// An openEHR terminology *group* a coded slot must draw from.
///
/// The membership check is guarded by `terminology_id == "openehr"` (the
/// `has_code_for_group_id` invariants bind against the openEHR terminology
/// only).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Group {
    /// `composition_category` — `COMPOSITION.category`.
    CompositionCategory,
    /// `setting` — `EVENT_CONTEXT.setting`.
    Setting,
    /// `null_flavours` — `ELEMENT.null_flavour`.
    NullFlavour,
    /// `instruction_states` — `ISM_TRANSITION.current_state`.
    InstructionState,
    /// `instruction_transitions` — `ISM_TRANSITION.transition`.
    InstructionTransition,
    /// `participation_function` — `PARTICIPATION`/`EXTRACT_PARTICIPATION.function`.
    ParticipationFunction,
    /// `participation_mode` — `PARTICIPATION`/`EXTRACT_PARTICIPATION.mode`.
    ParticipationMode,
    /// `event_math_function` — `INTERVAL_EVENT.math_function`.
    EventMathFunction,
    /// `term_mapping_purpose` — `TERM_MAPPING.purpose`.
    TermMappingPurpose,
    /// `audit_change_type` — `AUDIT_DETAILS.change_type`.
    AuditChangeType,
    /// `attestation_reason` — `ATTESTATION.reason`.
    AttestationReason,
    /// `subject_relationship` — `PARTY_RELATED.relationship`.
    SubjectRelationship,
    /// `version_lifecycle_state` — `VERSION.lifecycle_state`.
    VersionLifecycleState,
}

impl Group {
    /// Whether `code` is a member of this openEHR terminology group.
    #[must_use]
    pub fn is_valid(self, t: &OpenehrTerminology, code: &str) -> bool {
        match self {
            Group::CompositionCategory => t.is_valid_code("composition_category", code),
            Group::Setting => t.is_valid_code("setting", code),
            Group::NullFlavour => t.is_valid_code("null_flavours", code),
            Group::InstructionState => t.is_valid_code("instruction_states", code),
            Group::InstructionTransition => t.is_valid_code("instruction_transitions", code),
            Group::ParticipationFunction => t.is_valid_code("participation_function", code),
            Group::ParticipationMode => t.is_valid_code("participation_mode", code),
            Group::EventMathFunction => t.is_valid_code("event_math_function", code),
            Group::TermMappingPurpose => t.is_valid_code("term_mapping_purpose", code),
            Group::AuditChangeType => t.is_valid_code("audit_change_type", code),
            Group::AttestationReason => t.is_valid_code("attestation_reason", code),
            Group::SubjectRelationship => t.is_valid_code("subject_relationship", code),
            Group::VersionLifecycleState => t.is_valid_code("version_lifecycle_state", code),
        }
    }

    /// A human-readable label for the group (the `openehr-its` RM-instance
    /// terminology pass's message text).
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Group::CompositionCategory => "composition category",
            Group::Setting => "setting",
            Group::NullFlavour => "null flavour",
            Group::InstructionState => "instruction state",
            Group::InstructionTransition => "instruction transition",
            Group::ParticipationFunction => "participation function",
            Group::ParticipationMode => "participation mode",
            Group::EventMathFunction => "event math function",
            Group::TermMappingPurpose => "term mapping purpose",
            Group::AuditChangeType => "audit change type",
            Group::AttestationReason => "attestation reason",
            Group::SubjectRelationship => "subject relationship",
            Group::VersionLifecycleState => "version lifecycle state",
        }
    }
}

/// A code set (openEHR-internal or ISO/IANA-external) a coded slot must draw
/// from. Checked **without** a `terminology_id` guard — the RM invariant is
/// `code_set (id).has_code (code)`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CodeSet {
    /// `languages` (ISO 639-1).
    Languages,
    /// `countries` (ISO 3166-1).
    Countries,
    /// `character_sets` (IANA).
    CharacterSets,
    /// `media_types` (IANA).
    MediaTypes,
    /// `normal_statuses` (openEHR-internal).
    NormalStatuses,
    /// `compression_algorithms` (openEHR-internal).
    CompressionAlgorithms,
    /// `integrity_check_algorithms` (openEHR-internal).
    IntegrityCheckAlgorithms,
}

impl CodeSet {
    /// Whether `code` is a member of this code set.
    #[must_use]
    pub fn is_valid(self, t: &OpenehrTerminology, code: &str) -> bool {
        match self {
            CodeSet::Languages => t.is_valid_external_code("languages", code),
            CodeSet::Countries => t.is_valid_external_code("countries", code),
            CodeSet::CharacterSets => t.is_valid_external_code("character_sets", code),
            CodeSet::MediaTypes => t.is_valid_external_code("media_types", code),
            CodeSet::NormalStatuses => t.is_valid_normal_status(code),
            // No dedicated convenience method: check the internal code set directly.
            CodeSet::CompressionAlgorithms => t
                .code_set("compression_algorithms")
                .is_some_and(|cs| cs.codes.iter().flatten().any(|c| c.value == code)),
            CodeSet::IntegrityCheckAlgorithms => t
                .code_set("integrity_check_algorithms")
                .is_some_and(|cs| cs.codes.iter().flatten().any(|c| c.value == code)),
        }
    }

    /// A human-readable label for the code set (the `openehr-its` RM-instance
    /// terminology pass's message text).
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            CodeSet::Languages => "language (ISO 639-1)",
            CodeSet::Countries => "country (ISO 3166-1)",
            CodeSet::CharacterSets => "character set (IANA)",
            CodeSet::MediaTypes => "media type (IANA)",
            CodeSet::NormalStatuses => "normal status",
            CodeSet::CompressionAlgorithms => "compression algorithm",
            CodeSet::IntegrityCheckAlgorithms => "integrity check algorithm",
        }
    }
}

/// The binding of a coded slot: an openEHR group (guarded by `terminology_id ==
/// "openehr"`) or a code set (unguarded).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Binding {
    /// An openEHR terminology group (`has_code_for_group_id`).
    Group(Group),
    /// A code set (`code_set (id).has_code`).
    CodeSet(CodeSet),
}

impl Binding {
    /// The vocabulary label for this binding.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Binding::Group(g) => g.label(),
            Binding::CodeSet(cs) => cs.label(),
        }
    }
}

/// One coded slot of an RM type, with the BMM invariant name it realizes and the
/// vocabulary its code must be drawn from.
#[derive(Clone, Copy, Debug)]
pub struct Slot {
    /// The RM attribute name carrying the coded value (`category`, `language`, …).
    pub field: &'static str,
    /// The BMM class-invariant name this slot enforces (`Category_validity`, …).
    pub invariant: &'static str,
    /// The vocabulary the code must belong to.
    pub binding: Binding,
}

/// The coded slots fixed by an owning RM `_type`, with the invariant +
/// vocabulary each realizes.
///
/// The single source of the terminology binding table; both
/// [`validate_rm_terminology`] and the `openehr-its` RM-instance terminology
/// pass resolve against it.
///
/// Scoping follows the BMM: `null_flavour` is `ELEMENT`-scoped and `normal_status`
/// is scoped to the concrete `DV_ORDERED` descendants (the invariants are declared
/// on `ELEMENT` / `DV_ORDERED`). `math_function` covers all `EVENT` subtypes; the
/// field is present only on `INTERVAL_EVENT`, so the other arms are inert (the
/// missing field skips).
#[must_use]
#[expect(
    clippy::too_many_lines,
    reason = "a flat `_type` -> `&[Slot]` terminology-binding table; the length is the size of the RM type set, not logic"
)]
pub fn slots_for(rm_type: &str) -> &'static [Slot] {
    // Reused vocab-per-field slot vectors.
    // ENTRY subtypes + DV_TEXT family share `language` (ISO 639-1) + `encoding`
    // (IANA character sets): RM `entry.adoc` Language_valid/Encoding_valid,
    // `dv_text.adoc` Language_valid/Encoding_valid.
    const LANG_ENCODING: &[Slot] = &[
        Slot {
            field: "language",
            invariant: "Language_valid",
            binding: Binding::CodeSet(CodeSet::Languages),
        },
        Slot {
            field: "encoding",
            invariant: "Encoding_valid",
            binding: Binding::CodeSet(CodeSet::CharacterSets),
        },
    ];
    match rm_type {
        // composition.adoc: Category_validity (composition_category group),
        // Language_valid (languages code set), Territory_valid (countries code set).
        "COMPOSITION" => &[
            Slot {
                field: "category",
                invariant: "Category_validity",
                binding: Binding::Group(Group::CompositionCategory),
            },
            Slot {
                field: "language",
                invariant: "Language_valid",
                binding: Binding::CodeSet(CodeSet::Languages),
            },
            Slot {
                field: "territory",
                invariant: "Territory_valid",
                binding: Binding::CodeSet(CodeSet::Countries),
            },
        ],
        // event_context.adoc: Setting_valid (setting group).
        "EVENT_CONTEXT" => &[Slot {
            field: "setting",
            invariant: "Setting_valid",
            binding: Binding::Group(Group::Setting),
        }],
        // element.adoc: Inv_null_flavour_valid (null_flavours group).
        "ELEMENT" => &[Slot {
            field: "null_flavour",
            invariant: "Inv_null_flavour_valid",
            binding: Binding::Group(Group::NullFlavour),
        }],
        // ism_transition.adoc: Current_state_valid + Transition_valid
        // (instruction_states / instruction_transitions groups).
        "ISM_TRANSITION" => &[
            Slot {
                field: "current_state",
                invariant: "Current_state_valid",
                binding: Binding::Group(Group::InstructionState),
            },
            Slot {
                field: "transition",
                invariant: "Transition_valid",
                binding: Binding::Group(Group::InstructionTransition),
            },
        ],
        // participation.adoc + extract_participation.adoc: Function_valid +
        // Mode_valid (both bind the same participation_function / participation_mode
        // groups). The per-type message takes the concrete `_type`.
        "PARTICIPATION" | "EXTRACT_PARTICIPATION" => &[
            Slot {
                field: "function",
                invariant: "Function_valid",
                binding: Binding::Group(Group::ParticipationFunction),
            },
            Slot {
                field: "mode",
                invariant: "Mode_valid",
                binding: Binding::Group(Group::ParticipationMode),
            },
        ],
        // interval_event.adoc: Math_function_validity (event_math_function group).
        // The field lives on INTERVAL_EVENT; the EVENT/POINT_EVENT arms are inert.
        "EVENT" | "POINT_EVENT" | "INTERVAL_EVENT" => &[Slot {
            field: "math_function",
            invariant: "Math_function_validity",
            binding: Binding::Group(Group::EventMathFunction),
        }],
        // entry.adoc / dv_text.adoc: language + encoding.
        "OBSERVATION" | "EVALUATION" | "INSTRUCTION" | "ACTION" | "ADMIN_ENTRY"
        | "GENERIC_ENTRY" | "DV_TEXT" | "DV_CODED_TEXT" => LANG_ENCODING,
        // term_mapping.adoc: Purpose_valid (term_mapping_purpose group).
        "TERM_MAPPING" => &[Slot {
            field: "purpose",
            invariant: "Purpose_valid",
            binding: Binding::Group(Group::TermMappingPurpose),
        }],
        // dv_encapsulated.adoc: Charset_valid / Language_valid (character_sets /
        // languages) + dv_multimedia.adoc: Media_type_valid (media_types),
        // Compression_algorithm_validity (compression_algorithms),
        // Integrity_check_algorithm_validity (integrity_check_algorithms).
        "DV_MULTIMEDIA" => &[
            Slot {
                field: "media_type",
                invariant: "Media_type_valid",
                binding: Binding::CodeSet(CodeSet::MediaTypes),
            },
            Slot {
                field: "charset",
                invariant: "Charset_valid",
                binding: Binding::CodeSet(CodeSet::CharacterSets),
            },
            Slot {
                field: "language",
                invariant: "Language_valid",
                binding: Binding::CodeSet(CodeSet::Languages),
            },
            Slot {
                field: "compression_algorithm",
                invariant: "Compression_algorithm_validity",
                binding: Binding::CodeSet(CodeSet::CompressionAlgorithms),
            },
            Slot {
                field: "integrity_check_algorithm",
                invariant: "Integrity_check_algorithm_validity",
                binding: Binding::CodeSet(CodeSet::IntegrityCheckAlgorithms),
            },
        ],
        // dv_encapsulated.adoc: Charset_valid / Language_valid on DV_PARSABLE.
        "DV_PARSABLE" => &[
            Slot {
                field: "charset",
                invariant: "Charset_valid",
                binding: Binding::CodeSet(CodeSet::CharacterSets),
            },
            Slot {
                field: "language",
                invariant: "Language_valid",
                binding: Binding::CodeSet(CodeSet::Languages),
            },
        ],
        // audit_details.adoc: Change_type_valid (audit_change_type group).
        "AUDIT_DETAILS" => &[Slot {
            field: "change_type",
            invariant: "Change_type_valid",
            binding: Binding::Group(Group::AuditChangeType),
        }],
        // attestation.adoc: Reason_valid (attestation_reason group), PLUS the
        // Change_type_valid it inherits from AUDIT_DETAILS — this table is keyed
        // on the exact wire `_type`, so an ancestor's slot reaches a descendant
        // only by being repeated here.
        "ATTESTATION" => &[
            Slot {
                field: "reason",
                invariant: "Reason_valid",
                binding: Binding::Group(Group::AttestationReason),
            },
            Slot {
                field: "change_type",
                invariant: "Change_type_valid",
                binding: Binding::Group(Group::AuditChangeType),
            },
        ],
        // party_related.adoc: Relationship_valid (subject_relationship group).
        "PARTY_RELATED" => &[Slot {
            field: "relationship",
            invariant: "Relationship_valid",
            binding: Binding::Group(Group::SubjectRelationship),
        }],
        // dv_ordered.adoc: Normal_status_validity (normal_statuses code set),
        // declared on DV_ORDERED — applied to each concrete descendant.
        "DV_QUANTITY" | "DV_COUNT" | "DV_PROPORTION" | "DV_ORDINAL" | "DV_SCALE" | "DV_DATE"
        | "DV_TIME" | "DV_DATE_TIME" | "DV_DURATION" => &[Slot {
            field: "normal_status",
            invariant: "Normal_status_validity",
            binding: Binding::CodeSet(CodeSet::NormalStatuses),
        }],
        // version.adoc: Lifecycle_state_valid (version_lifecycle_state group).
        "ORIGINAL_VERSION" | "IMPORTED_VERSION" => &[Slot {
            field: "lifecycle_state",
            invariant: "Lifecycle_state_valid",
            binding: Binding::Group(Group::VersionLifecycleState),
        }],
        // authored_resource.adoc: Original_language_valid (languages code set).
        "AUTHORED_RESOURCE" => &[Slot {
            field: "original_language",
            invariant: "Original_language_valid",
            binding: Binding::CodeSet(CodeSet::Languages),
        }],
        // resource_description_item.adoc + translation_details.adoc: Language_valid
        // (languages code set) — same `language` slot binding.
        "RESOURCE_DESCRIPTION_ITEM" | "TRANSLATION_DETAILS" => &[Slot {
            field: "language",
            invariant: "Language_valid",
            binding: Binding::CodeSet(CodeSet::Languages),
        }],
        _ => &[],
    }
}

/// The `(code, terminology_id)` of a coded value node — a
/// `DV_CODED_TEXT`/state (via `defining_code`) or a bare `CODE_PHRASE`.
///
/// `None` when the node is not coded (e.g. a plain `DV_TEXT` participation
/// function, or an absent optional slot), which is skipped: the `has_code…`
/// invariants are guarded by a `generating_type = DV_CODED_TEXT` / `/= Void`
/// antecedent.
#[must_use]
pub fn openehr_code(node: &Value) -> Option<(&str, &str)> {
    let code_phrase = node.get("defining_code").unwrap_or(node);
    let code = code_phrase.get("code_string").and_then(Value::as_str)?;
    let terminology = code_phrase
        .get("terminology_id")
        .and_then(|t| t.get("value"))
        .and_then(Value::as_str)
        .unwrap_or("");
    Some((code, terminology))
}

/// Whether `slot` is satisfied by `node`'s coded value.
///
/// Returns `Some(false)` only for a present, in-scope code that is NOT a
/// member of the bound vocabulary; `None` when the slot is absent, uncoded,
/// or (for a group binding) carries a non-`openehr` terminology (out of scope
/// for the openEHR-group check).
#[must_use]
pub fn slot_is_violated(slot: &Slot, node: &Value) -> bool {
    let Some((code, terminology)) = openehr_code(node) else {
        return false;
    };
    match slot.binding {
        Binding::Group(group) => {
            // `terminology (Terminology_id_openehr).has_code_for_group_id (…)`:
            // a non-openEHR terminology binding is out of scope for the check.
            if terminology != "openehr" {
                return false;
            }
            !group.is_valid(openehr(), code)
        }
        // `code_set (id).has_code (code)`: unconditional on the code value.
        Binding::CodeSet(cs) => !cs.is_valid(openehr(), code),
    }
}

/// Run the terminology-backed RM class invariants for a single canonical-JSON
/// node, dispatching on its `_type`.
///
/// Appends one [`InvariantViolation`] per violated coded slot, in the uniform
/// `Invariant <Name> failed on type <RM_TYPE>` form and keyed to the offending
/// attribute path. A node whose `_type` binds no coded slot appends nothing.
///
/// This is the core form of the RM terminology/code-set invariants; the
/// `openehr-its` wire-boundary dispatcher runs it as a post-core check, and the
/// `openehr-its` RM-instance terminology pass resolves the same [`slots_for`]
/// table for its own message rendering.
pub fn validate_rm_terminology(ty: &str, value: &Value, out: &mut Vec<InvariantViolation>) {
    for slot in slots_for(ty) {
        let Some(node) = value.get(slot.field) else {
            continue;
        };
        if slot_is_violated(slot, node) {
            out.push(InvariantViolation::at(
                slot.field,
                format!("Invariant {} failed on type {ty}", slot.invariant),
            ));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use openehr_term::codes::AuditChangeType;

    /// This table is keyed on the exact wire `_type`, so an invariant declared
    /// on an ancestor reaches a descendant only by being repeated. `ATTESTATION`
    /// inherits `AUDIT_DETAILS`, so an out-of-group `change_type` must be
    /// refused on both — it was accepted on `ATTESTATION` while the identical
    /// `AUDIT_DETAILS` node was refused.
    #[test]
    fn an_inherited_coded_invariant_reaches_the_descendant() {
        let node = serde_json::json!({
            "change_type": {
                "value": "creation",
                "defining_code": {
                    "terminology_id": { "value": "openehr" },
                    "code_string": "9999"
                }
            }
        });
        for ty in ["AUDIT_DETAILS", "ATTESTATION"] {
            let mut out = Vec::new();
            validate_rm_terminology(ty, &node, &mut out);
            assert!(
                out.iter().any(
                    |v| v.message == format!("Invariant Change_type_valid failed on type {ty}")
                ),
                "{ty}: an out-of-group change_type must be refused, got {out:?}",
            );
        }

        // The accepting twin: a code the group does carry raises nothing.
        let valid = serde_json::json!({
            "change_type": {
                "value": AuditChangeType::Creation.rubric(),
                "defining_code": {
                    "terminology_id": { "value": "openehr" },
                    "code_string": AuditChangeType::Creation.code()
                }
            }
        });
        for ty in ["AUDIT_DETAILS", "ATTESTATION"] {
            let mut out = Vec::new();
            validate_rm_terminology(ty, &valid, &mut out);
            assert!(
                out.is_empty(),
                "{ty}: a valid change_type raises nothing, got {out:?}"
            );
        }
    }
}