openehr 0.3.0

openEHR Reference Model types, validation, paths, AQL parsing, and change-control security primitives
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
//! Reference Model invariant checking.
//!
//! # Why this exists when the constructors already check
//!
//! Every builder in this crate enforces the invariants of the class it builds.
//! **Deserialization does not.** `serde` writes fields directly, so a
//! `COMPOSITION` read from JSON has never passed through `Composition::new`,
//! and an `ELEMENT` read from JSON can carry a `value` *and* a `null_flavour`
//! — a combination no constructor here will produce and every openEHR
//! implementation must be prepared to receive.
//!
//! So there are two gates, and they are not redundant:
//!
//! | Gate | Covers | Enforced by |
//! | --- | --- | --- |
//! | construction | data this program builds | `Result`-returning builders |
//! | validation | data this program **receives** | this module |
//!
//! A service that deserializes a composition and stores it without calling
//! [`Validate::validate`] has no invariant checking at all, whatever its
//! constructors do.
//!
//! # What it reports, and what it cannot
//!
//! Validation here is **RM-level**: the invariants stated in the openEHR class
//! definitions. It is not archetype validation. Checking that
//! `openEHR-EHR-OBSERVATION.blood_pressure.v2` permits exactly these elements
//! at these node ids requires the archetype, and archetypes are out of scope
//! (`S1.4`). A composition that passes here can still violate its archetype,
//! and the documentation says so rather than letting "valid" be read as more
//! than it is.
//!
//! ```
//! use openehr::validation::Validate;
//! use openehr::rm::data_structures::Element;
//!
//! // An ELEMENT that serde built with both a value and a null flavour: no
//! // constructor produces it, and a sender can still send it.
//! let json = r#"{
//!   "name": {"value": "Systolic"},
//!   "archetype_node_id": "at0004",
//!   "value": {"_type": "DV_COUNT", "magnitude": 1},
//!   "null_flavour": {"value": "unknown", "defining_code":
//!     {"terminology_id": {"value": "openehr"}, "code_string": "253"}}
//! }"#;
//! let element: Element = serde_json::from_str(json).unwrap();
//! let report = element.validate();
//! assert!(!report.is_empty());
//! assert_eq!(report.violations()[0].invariant, "Inv_null_flavour_indicated");
//! ```

use crate::error::{ValidationReport, Violation};
use crate::rm::common::{Attestation, Locatable as _, Participation, PartyProxy};
use crate::rm::data_structures::{Cluster, Element, Event, History, Item, ItemStructure};
use crate::rm::data_types::{DataValue, DvCodedText, DvOrdered, DvQuantity, OrderedAttrs, Text};
use crate::rm::ehr::{Composition, ContentItem, EhrStatus, Entry, EventContext, Section};
use crate::terminology;

/// Accumulates violations while walking a structure, tracking where it is.
#[derive(Debug, Default)]
pub struct Context {
    path: Vec<String>,
    report: ValidationReport,
}

impl Context {
    /// A fresh context rooted at the object being validated.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Records a violation at the current path.
    pub fn violation(
        &mut self,
        class: &'static str,
        invariant: &'static str,
        detail: &'static str,
    ) {
        self.report.push(Violation {
            path: self.path.join(""),
            class,
            invariant,
            detail,
        });
    }

    /// Runs `f` with one more path segment pushed.
    fn nested<F: FnOnce(&mut Self)>(&mut self, segment: String, f: F) {
        self.path.push(segment);
        f(self);
        self.path.pop();
    }

    /// The accumulated report.
    #[must_use]
    pub fn finish(self) -> ValidationReport {
        self.report
    }
}

/// Checks a structure's Reference Model invariants.
pub trait Validate {
    /// Walks the structure, recording every violation found.
    fn visit(&self, ctx: &mut Context);

    /// Validates the structure and returns everything that failed.
    fn validate(&self) -> ValidationReport {
        let mut ctx = Context::new();
        self.visit(&mut ctx);
        ctx.finish()
    }

    /// Validates and converts to a `Result`.
    ///
    /// # Errors
    ///
    /// Returns the [`ValidationReport`] if any invariant failed.
    fn validate_ok(&self) -> Result<(), ValidationReport> {
        self.validate().into_result()
    }
}

/// Checks the invariants shared by every `LOCATABLE`.
fn check_locatable<L: crate::rm::common::Locatable>(node: &L, ctx: &mut Context) {
    if node.archetype_node_id().is_empty() {
        ctx.violation(
            "LOCATABLE",
            "Archetype_node_id_valid",
            "archetype_node_id is empty, so the node cannot be reached by any path",
        );
    }
    // openEHR's `LOCATABLE.Name_valid` is only `name /= Void` — an *empty* name
    // breaks `DV_TEXT.Value_valid` instead, and attributing it to `Name_valid`
    // would send a reader to the wrong class definition (`L10.4`).
    check_text(node.name().as_text(), ctx);
    if node.name().value().is_empty() {
        ctx.nested("/name".to_owned(), |c| {
            c.violation("DV_TEXT", "Valid_value", "value is empty");
        });
    }
    // An archetype root whose archetype id constrains a different RM class is
    // the single most common structural error in hand-built or
    // transform-produced instances: an OBSERVATION archetype id on an
    // EVALUATION node. Nothing downstream detects it, and the data looks fine.
    if let Some(details) = node.archetype_details() {
        let entity = details.archetype_id().rm_entity();
        if entity != node.rm_type_name() {
            ctx.violation(
                "ARCHETYPED",
                "Archetype_id_rm_entity_matches",
                "archetype id constrains a different RM class than the node it annotates",
            );
        }
        if details.rm_version().is_empty() {
            ctx.violation("ARCHETYPED", "Rm_version_valid", "rm_version is empty");
        }
    }
}

/// Checks a `DV_CODED_TEXT` against the openEHR support terminology.
/// `TERM_MAPPING.Purpose_valid` — a mapping's purpose must come from openEHR's
/// own group.
///
/// The group has shipped as `terminology::term_mapping_purpose` since the
/// terminology was written, and nothing consulted it (`lib:A-24`). That is the
/// same shape as `A-22`, which found three `DV_MULTIMEDIA` rules unenforced
/// while the crate carried exactly the code sets they needed.
///
/// Reached from [`check_locatable`] for every node name and from
/// [`check_coded_text`] for every coded text — `DV_CODED_TEXT` embeds a
/// `DV_TEXT`, so one helper covers both. A `DV_TEXT` inside a data value the
/// walk does not descend into is not reached, which is a property of the walk
/// rather than of this rule.
fn check_text(text: &crate::rm::data_types::DvText, ctx: &mut Context) {
    for mapping in text.mappings() {
        if let Some(purpose) = mapping.purpose()
            && !crate::terminology::term_mapping_purpose::GROUP
                .contains(purpose.defining_code().code_string())
        {
            ctx.violation(
                "TERM_MAPPING",
                "Purpose_valid",
                "a term mapping's purpose is not from the openEHR                  term_mapping_purpose group",
            );
        }
    }
}

fn check_coded_text(coded: &DvCodedText, ctx: &mut Context) {
    check_text(coded.as_text(), ctx);
    if coded.check_openehr_rubric() == Some(false) {
        ctx.violation(
            "DV_CODED_TEXT",
            "Value_is_rubric",
            "value does not match the openEHR rubric for defining_code",
        );
    }
}

/// Checks the two `DV_ORDERED` invariants that relate a value to its own
/// recorded reference range.
///
/// `is_abnormal` is the value's own answer to "am I outside my normal range?",
/// which is `None` when no range was recorded — see [`DvOrdered::is_abnormal`].
fn check_ordered(attrs: &OrderedAttrs, is_abnormal: Option<bool>, ctx: &mut Context) {
    // `REFERENCE_RANGE.Range_is_simple`: a reference range's endpoints must
    // themselves be simple — carrying no normal range and no reference ranges
    // of their own.
    //
    // The model really is this cyclic: a quantity's normal range is expressed
    // as quantities, each of which can carry its own reference ranges. Without
    // this rule a range can nest to any depth, and a renderer walking it to
    // show "normal: 4.0–6.0" either recurses forever or silently shows the
    // outermost layer.
    for range in attrs.other_reference_ranges() {
        // `DataValue` has no single accessor for ordered attributes — only the
        // ordered variants carry them — so the check names them. Written out
        // rather than wildcarded: a new ordered variant should be a decision
        // here, not a silent pass.
        let simple = |bound: Option<&crate::rm::data_types::DataValue>| {
            use crate::rm::data_types::DataValue;
            let attrs = match bound {
                Some(DataValue::Quantity(v)) => Some(v.ordered_attrs()),
                Some(DataValue::Count(v)) => Some(v.ordered_attrs()),
                Some(DataValue::Proportion(v)) => Some(v.ordered_attrs()),
                Some(DataValue::Ordinal(v)) => Some(v.ordered_attrs()),
                Some(DataValue::Scale(v)) => Some(v.ordered_attrs()),
                _ => None,
            };
            attrs.is_none_or(|o| {
                o.normal_range().is_none() && o.other_reference_ranges().is_empty()
            })
        };
        if !simple(range.range().lower()) || !simple(range.range().upper()) {
            ctx.violation(
                "REFERENCE_RANGE",
                "Range_is_simple",
                "a reference range's endpoint carries reference ranges of its own",
            );
        }
    }

    let Some(status) = attrs.normal_status() else {
        return;
    };
    // `Normal_status_validity`: the status comes from the openEHR
    // "normal statuses" code set — the HL7 abnormal-flag letters. A status
    // outside it is not comparable with anything, and a renderer will show it
    // verbatim next to a result.
    if !crate::terminology::normal_status::GROUP.contains(status.code_string()) {
        ctx.violation(
            "DV_ORDERED",
            "Normal_status_validity",
            "normal_status is not from the openEHR normal-statuses code set",
        );
    }
    // `Normal_range_and_status_consistency`: with both a range and a status
    // present, `N` and "inside the range" must agree. They disagree when a
    // result is copied from one system and its flag from another — exactly the
    // case where a clinician sees a normal flag beside an abnormal number.
    if let Some(abnormal) = is_abnormal {
        let says_normal = status.code_string() == crate::terminology::normal_status::NORMAL;
        if says_normal == abnormal {
            ctx.violation(
                "DV_ORDERED",
                "Normal_range_and_status_consistency",
                "normal_status and the value's position in its normal_range disagree",
            );
        }
    }
}

/// Checks a `DV_TEXT`-typed attribute that openEHR requires to come from a
/// terminology group **when it happens to be coded**.
///
/// `PARTICIPATION.function`, `PARTICIPATION.mode`, and `ATTESTATION.reason` are
/// all shaped this way: the attribute is `DV_TEXT` because the useful
/// vocabulary lives in external terminologies, and the openEHR group applies
/// only if the value is a `DV_CODED_TEXT` from openEHR's own terminology.
/// Checking unconditionally would reject a SNOMED-coded participation function,
/// which is the commonest real case.
fn check_optional_group(
    text: &Text,
    group: crate::terminology::Group,
    class: &'static str,
    invariant: &'static str,
    ctx: &mut Context,
) {
    let Text::Coded(coded) = text else {
        return;
    };
    check_coded_text(coded, ctx);
    if coded.defining_code().is_openehr() && !group.contains(coded.defining_code().code_string()) {
        ctx.violation(
            class,
            invariant,
            "coded value is not from its openEHR group",
        );
    }
}

/// Checks one `PARTICIPATION`.
fn check_participation(participation: &Participation, ctx: &mut Context) {
    check_optional_group(
        participation.function(),
        crate::terminology::participation_function::GROUP,
        "PARTICIPATION",
        "Function_valid",
        ctx,
    );
    if let Some(mode) = participation.mode() {
        check_coded_text(mode, ctx);
        if mode.defining_code().is_openehr()
            && !crate::terminology::participation_mode::GROUP
                .contains(mode.defining_code().code_string())
        {
            ctx.violation(
                "PARTICIPATION",
                "Mode_valid",
                "mode is not from the openEHR participation-mode group",
            );
        }
    }
    check_party(
        participation.performer(),
        "PARTICIPATION",
        "Performer_valid",
        ctx,
    );
}

/// Checks one `ATTESTATION`.
///
/// Not reachable from a `COMPOSITION`: attestations hang off a `VERSION`, which
/// is why this is public — a caller validating a version calls it directly.
pub fn check_attestation(attestation: &Attestation, ctx: &mut Context) {
    check_optional_group(
        attestation.reason(),
        crate::terminology::attestation_reason::GROUP,
        "ATTESTATION",
        "Reason_valid",
        ctx,
    );
    check_party(
        attestation.audit().committer(),
        "AUDIT_DETAILS",
        "Committer_valid",
        ctx,
    );
}

impl Validate for DataValue {
    fn visit(&self, ctx: &mut Context) {
        match self {
            Self::CodedText(t) => check_coded_text(t, ctx),
            Self::Ordinal(o) => {
                check_coded_text(o.symbol(), ctx);
                check_ordered(o.ordered_attrs(), o.is_abnormal(), ctx);
            }
            Self::Scale(s) => {
                check_coded_text(s.symbol(), ctx);
                check_ordered(s.ordered_attrs(), s.is_abnormal(), ctx);
            }
            Self::Count(c) => check_ordered(c.ordered_attrs(), c.is_abnormal(), ctx),
            Self::Quantity(q) => {
                if !q.magnitude().is_finite() {
                    ctx.violation(
                        "DV_QUANTITY",
                        "Magnitude_finite",
                        "magnitude is not a finite number",
                    );
                }
                if q.units().is_empty() {
                    ctx.violation("DV_QUANTITY", "Units_valid", "units is empty");
                }
                // `precision >= -1`, not `>= 0`: -1 is openEHR's stated
                // "unlimited decimal places", not an out-of-range value.
                if q.precision()
                    .is_some_and(|p| p < DvQuantity::UNLIMITED_PRECISION)
                {
                    ctx.violation(
                        "DV_QUANTITY",
                        "Precision_valid",
                        "precision is below -1, the unlimited-precision sentinel",
                    );
                }
                check_ordered(q.ordered_attrs(), q.is_abnormal(), ctx);
            }
            Self::Proportion(p) => {
                if p.denominator() == 0.0 {
                    ctx.violation("DV_PROPORTION", "Valid_denominator", "denominator is zero");
                }
                if p.kind().requires_integral() && !p.is_integral() {
                    ctx.violation(
                        "DV_PROPORTION",
                        "Fraction_validity",
                        "a fractional kind has a non-integral numerator or denominator",
                    );
                }
                // `precision = 0 implies is_integral`: declaring precision zero
                // asserts the parts really are whole numbers.
                if p.precision() == Some(0) && !p.is_integral() {
                    ctx.violation(
                        "DV_PROPORTION",
                        "Precision_validity",
                        "precision is 0 but a part has a fractional component",
                    );
                }
                check_ordered(p.ordered_attrs(), p.is_abnormal(), ctx);
            }
            Self::Multimedia(m) => check_multimedia(m, ctx),
            Self::Text(t) if t.value().is_empty() => {
                ctx.violation("DV_TEXT", "Valid_value", "value is empty");
            }
            _ => {}
        }
    }
}

impl Validate for Element {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        // openEHR: `is_null xor null_flavour = Void`. Both present and both
        // absent are each a violation, and they fail differently: both present
        // is a contradiction, both absent is an element that says nothing.
        match (self.value().is_some(), self.null_flavour().is_some()) {
            (true, true) => ctx.violation(
                "ELEMENT",
                "Inv_null_flavour_indicated",
                "an element has both a value and a null_flavour",
            ),
            (false, false) => ctx.violation(
                "ELEMENT",
                "Inv_null_flavour_indicated",
                "an element has neither a value nor a null_flavour",
            ),
            _ => {}
        }
        if self.null_reason().is_some() && self.value().is_some() {
            ctx.violation(
                "ELEMENT",
                "Inv_null_reason_valid",
                "a reason for absence is recorded on an element that has a value",
            );
        }
        if let Some(flavour) = self.null_flavour() {
            if !flavour.defining_code().is_openehr()
                || !crate::terminology::null_flavour::GROUP
                    .contains(flavour.defining_code().code_string())
            {
                ctx.violation(
                    "ELEMENT",
                    "Inv_null_flavour_valid",
                    "null_flavour is not one of the four openEHR null flavours",
                );
            }
            check_coded_text(flavour, ctx);
        }
        if let Some(value) = self.value() {
            ctx.nested("/value".to_owned(), |c| value.visit(c));
        }
    }
}

impl Validate for Cluster {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        if self.items().is_empty() {
            ctx.violation("CLUSTER", "Items_non_empty", "a cluster has no items");
        }
        for (i, item) in self.items().iter().enumerate() {
            ctx.nested(format!("/items[{i}]"), |c| item.visit(c));
        }
    }
}

impl Validate for Item {
    fn visit(&self, ctx: &mut Context) {
        match self {
            Self::Cluster(v) => v.visit(ctx),
            Self::Element(v) => v.visit(ctx),
        }
    }
}

impl Validate for ItemStructure {
    fn visit(&self, ctx: &mut Context) {
        match self {
            Self::Single(s) => {
                check_locatable(s, ctx);
                ctx.nested("/item".to_owned(), |c| s.item().visit(c));
            }
            Self::List(s) => {
                check_locatable(s, ctx);
                for (i, e) in s.items().iter().enumerate() {
                    ctx.nested(format!("/items[{i}]"), |c| e.visit(c));
                }
            }
            Self::Table(s) => {
                check_locatable(s, ctx);
                if !s.is_regular() {
                    ctx.violation(
                        "ITEM_TABLE",
                        "Rows_regular",
                        "rows do not all have the same number of columns",
                    );
                }
                for (i, r) in s.rows().iter().enumerate() {
                    // `Valid_structure`: a table's cells are ELEMENTs. A row is
                    // a CLUSTER and a CLUSTER may hold either, so the type
                    // system permits a table cell that is itself a cluster —
                    // a nested structure in what a reader will render as one
                    // cell, and what a path expression will not find.
                    if r.items()
                        .iter()
                        .any(|item| !matches!(item, crate::rm::data_structures::Item::Element(_)))
                    {
                        ctx.nested(format!("/rows[{i}]"), |c| {
                            c.violation(
                                "ITEM_TABLE",
                                "Valid_structure",
                                "a row holds something other than ELEMENTs",
                            );
                        });
                    }
                    ctx.nested(format!("/rows[{i}]"), |c| r.visit(c));
                }
            }
            Self::Tree(s) => {
                check_locatable(s, ctx);
                for (i, item) in s.items().iter().enumerate() {
                    ctx.nested(format!("/items[{i}]"), |c| item.visit(c));
                }
            }
        }
    }
}

impl Validate for Event {
    fn visit(&self, ctx: &mut Context) {
        match self {
            Self::Point(e) => {
                check_locatable(e, ctx);
                ctx.nested("/data".to_owned(), |c| e.data().visit(c));
                if let Some(state) = e.state() {
                    ctx.nested("/state".to_owned(), |c| state.visit(c));
                }
            }
            Self::Interval(e) => {
                check_locatable(e, ctx);
                if e.width().value().is_negative() {
                    ctx.violation(
                        "INTERVAL_EVENT",
                        "Width_non_negative",
                        "width is negative, so the interval ends before it starts",
                    );
                }
                check_coded_text(e.math_function(), ctx);
                ctx.nested("/data".to_owned(), |c| e.data().visit(c));
                if let Some(state) = e.state() {
                    ctx.nested("/state".to_owned(), |c| state.visit(c));
                }
            }
        }
    }
}

impl Validate for History {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        if self.events().is_empty() && self.summary().is_none() {
            ctx.violation(
                "HISTORY",
                "Events_valid",
                "a history has neither events nor a summary",
            );
        }
        if self
            .period()
            .is_some_and(|p| p.value().is_zero() || p.value().is_negative())
        {
            ctx.violation(
                "HISTORY",
                "Periodic_validity",
                "a periodic history declares a zero or negative period",
            );
        }
        // `period_consistency`: in a periodic series every event must fall on a
        // multiple of the period. A series that says it is periodic and is not
        // will be resampled or graphed on the strength of that declaration.
        if self.is_period_consistent() == Some(false) {
            ctx.violation(
                "HISTORY",
                "Period_consistency",
                "a periodic history has an event whose offset is not a multiple of the period",
            );
        }
        for (i, event) in self.events().iter().enumerate() {
            // Every event must be at or after the history's origin: an event
            // before the zero point has a negative offset, and `EVENT.offset`
            // is defined as `time - origin` with no provision for one.
            if matches!(
                event.time().partial_cmp(self.origin()),
                Some(core::cmp::Ordering::Less)
            ) {
                ctx.nested(format!("/events[{i}]"), |c| {
                    c.violation(
                        "EVENT",
                        "Time_after_origin",
                        "event time is before the history origin",
                    );
                });
            }
            ctx.nested(format!("/events[{i}]"), |c| event.visit(c));
        }
        if let Some(summary) = self.summary() {
            ctx.nested("/summary".to_owned(), |c| summary.visit(c));
        }
    }
}

impl Validate for EventContext {
    fn visit(&self, ctx: &mut Context) {
        check_coded_text(self.setting(), ctx);
        if !self.setting().defining_code().is_openehr()
            || !crate::terminology::setting::GROUP
                .contains(self.setting().defining_code().code_string())
        {
            ctx.violation(
                "EVENT_CONTEXT",
                "Setting_valid",
                "setting is not from the openEHR setting group",
            );
        }
        if let Some(end) = self.end_time()
            && matches!(
                end.partial_cmp(self.start_time()),
                Some(core::cmp::Ordering::Less)
            )
        {
            ctx.violation(
                "EVENT_CONTEXT",
                "End_time_valid",
                "end_time is before start_time",
            );
        }
        if self.location().is_some_and(str::is_empty) {
            ctx.violation(
                "EVENT_CONTEXT",
                "location_valid",
                "location is present and empty",
            );
        }
        for (i, participation) in self.participations().iter().enumerate() {
            ctx.nested(format!("/participations[{i}]"), |c| {
                check_participation(participation, c);
            });
        }
        if let Some(other) = self.other_context() {
            ctx.nested("/other_context".to_owned(), |c| other.visit(c));
        }
    }
}

impl Validate for Entry {
    fn visit(&self, ctx: &mut Context) {
        check_locatable_entry(self, ctx);
        check_party(self.subject(), "ENTRY", "Subject_valid", ctx);
        // `Is_archetype_root`: openEHR states it as a bare assertion — every
        // ENTRY *is* the root of an entry archetype. One without
        // `archetype_details` cannot say which archetype shaped it, and nothing
        // downstream can validate it against one. Already checked for
        // COMPOSITION and EHR_STATUS; this is the third class that asserts it
        // and the one carrying the clinical statement.
        // `Subject_validity`: `subject_is_self implies subject.generating_type
        // = "PARTY_SELF"`. The BMM documents `subject_is_self` as *"True if
        // this Entry is about the subject of the EHR, in which case the subject
        // attribute is of type PARTY_SELF"* — so in openEHR the implication
        // holds by construction.
        //
        // It does not hold here. `PartyProxy::is_subject` answers true for a
        // `PARTY_RELATED` whose relationship is `self`, which is a legitimate
        // way to say "the patient" and is **not** a `PARTY_SELF`. So an entry
        // can claim to be about the record subject while naming a related
        // party, and the two readings of "who is this about" diverge silently.
        if self.subject().is_subject() && self.subject().type_name() != "PARTY_SELF" {
            ctx.violation(
                "ENTRY",
                "Subject_validity",
                "the subject is about the record subject but is not a PARTY_SELF",
            );
        }
        if let Some(provider) = self.entry_attrs().provider() {
            check_party(provider, "ENTRY", "Provider_valid", ctx);
        }
        for (i, participation) in self.entry_attrs().other_participations().iter().enumerate() {
            ctx.nested(format!("/other_participations[{i}]"), |c| {
                check_participation(participation, c);
            });
        }
        match self {
            Self::Observation(o) => {
                ctx.nested("/data".to_owned(), |c| o.data().visit(c));
                if let Some(state) = o.state() {
                    ctx.nested("/state".to_owned(), |c| state.visit(c));
                }
                if let Some(protocol) = o.care_entry().protocol() {
                    ctx.nested("/protocol".to_owned(), |c| protocol.visit(c));
                }
            }
            Self::Evaluation(e) => ctx.nested("/data".to_owned(), |c| e.data().visit(c)),
            Self::AdminEntry(a) => ctx.nested("/data".to_owned(), |c| a.data().visit(c)),
            Self::Instruction(i) => {
                if i.activities().is_empty() {
                    ctx.violation(
                        "INSTRUCTION",
                        "Activities_valid",
                        "an instruction has no activities",
                    );
                }
                if i.narrative().value().is_empty() {
                    ctx.violation(
                        "INSTRUCTION",
                        "Narrative_valid",
                        "an instruction has an empty narrative",
                    );
                }
                for (n, activity) in i.activities().iter().enumerate() {
                    ctx.nested(format!("/activities[{n}]/description"), |c| {
                        activity.description().visit(c);
                    });
                }
            }
            Self::Action(a) => {
                check_coded_text(a.ism_transition().current_state(), ctx);
                if !crate::terminology::instruction_state::GROUP.contains(
                    a.ism_transition()
                        .current_state()
                        .defining_code()
                        .code_string(),
                ) {
                    ctx.violation(
                        "ISM_TRANSITION",
                        "Current_state_valid",
                        "current_state is not an Instruction State Machine state",
                    );
                }
                ctx.nested("/description".to_owned(), |c| a.description().visit(c));
            }
        }
    }
}

fn check_locatable_entry(entry: &Entry, ctx: &mut Context) {
    // `ENTRY.Is_archetype_root` is checked here rather than beside the other
    // `ENTRY` rules, because `Locatable` is in scope only once the variant is
    // known. openEHR states it as a bare assertion — every ENTRY *is* the root
    // of an entry archetype — and one without `archetype_details` cannot say
    // which archetype shaped it, so nothing downstream can validate it against
    // one. Already checked for COMPOSITION and EHR_STATUS; this is the class
    // carrying the clinical statement.
    let root = |e: &dyn Fn() -> bool, ctx: &mut Context| {
        if !e() {
            ctx.violation(
                "ENTRY",
                "Is_archetype_root",
                "an entry has no archetype_details, so it is not an archetype root",
            );
        }
    };
    match entry {
        Entry::Observation(e) => {
            check_locatable(e, ctx);
            root(&|| e.is_archetype_root(), ctx);
        }
        Entry::Evaluation(e) => {
            check_locatable(e, ctx);
            root(&|| e.is_archetype_root(), ctx);
        }
        Entry::Instruction(e) => {
            check_locatable(e, ctx);
            root(&|| e.is_archetype_root(), ctx);
        }
        Entry::Action(e) => {
            check_locatable(e, ctx);
            root(&|| e.is_archetype_root(), ctx);
        }
        Entry::AdminEntry(e) => {
            check_locatable(e, ctx);
            root(&|| e.is_archetype_root(), ctx);
        }
    }
}

fn check_party(
    party: &PartyProxy,
    class: &'static str,
    invariant: &'static str,
    ctx: &mut Context,
) {
    let identified = match party {
        PartyProxy::SelfParty(_) => return,
        PartyProxy::Identified(p) => p,
        PartyProxy::Related(p) => {
            check_coded_text(p.relationship(), ctx);
            // `Relationship_valid`: the relationship comes from the openEHR
            // `subject_relationship` group. This is the attribute that says
            // whom an entry is about, so an unrecognised code means a finding
            // may be attributed to the wrong person.
            let code = p.relationship().defining_code();
            if code.is_openehr()
                && !crate::terminology::subject_relationship::GROUP.contains(code.code_string())
            {
                ctx.violation(
                    "PARTY_RELATED",
                    "Relationship_valid",
                    "relationship is not from the openEHR subject-relationship group",
                );
            }
            p.as_identified()
        }
    };
    if identified.name().is_none()
        && identified.identifiers().is_empty()
        && identified.external_ref().is_none()
    {
        ctx.violation(
            class,
            invariant,
            "an identified party has no name, no identifiers, and no external reference",
        );
    }
}

impl Validate for Section {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        for (i, item) in self.items().iter().enumerate() {
            ctx.nested(format!("/items[{i}]"), |c| item.visit(c));
        }
    }
}

impl Validate for ContentItem {
    fn visit(&self, ctx: &mut Context) {
        match self {
            Self::Section(s) => s.visit(ctx),
            Self::Entry(e) => e.visit(ctx),
        }
    }
}

impl Validate for EhrStatus {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        // `Is_archetype_root`, as for `COMPOSITION`: an EHR_STATUS is a
        // versioned, archetyped object in its own right.
        if !self.is_archetype_root() {
            ctx.violation(
                "EHR_STATUS",
                "Is_archetype_root",
                "an EHR_STATUS has no archetype_details, so it is not an archetype root",
            );
        }
        if let Some(other) = self.other_details() {
            ctx.nested("/other_details".to_owned(), |c| other.visit(c));
        }
    }
}

impl Validate for Composition {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        check_coded_text(self.category(), ctx);
        if !crate::terminology::composition_category::GROUP.contains(self.category_code()) {
            ctx.violation(
                "COMPOSITION",
                "Category_validity",
                "category is not from the openEHR composition_category group",
            );
        }
        check_party(self.composer(), "COMPOSITION", "Composer_valid", ctx);
        // `Is_archetype_root`: a COMPOSITION is by definition the root of a
        // composition archetype. One without `archetype_details` cannot say
        // which archetype shaped it, and nothing downstream can validate it
        // against one.
        if !self.is_archetype_root() {
            ctx.violation(
                "COMPOSITION",
                "Is_archetype_root",
                "a composition has no archetype_details, so it is not an archetype root",
            );
        }
        // `Is_persistent_validity`: a persistent composition is a running
        // summary across many encounters, so one encounter's context would
        // assert that the whole list belongs to that visit.
        if self.is_persistent() && self.context().is_some() {
            ctx.violation(
                "COMPOSITION",
                "Is_persistent_validity",
                "a persistent composition carries an event context",
            );
        }
        if let Some(context) = self.context() {
            ctx.nested("/context".to_owned(), |c| context.visit(c));
        }
        for (i, item) in self.content().iter().enumerate() {
            ctx.nested(format!("/content[{i}]"), |c| item.visit(c));
        }
    }
}

/// `DV_MULTIMEDIA`'s invariants.
///
/// Lifted out of `visit` because it grew past the length lint, and because
/// these are the checks most likely to be extended: three of them compare
/// against openEHR code sets, and openEHR adds to those.
fn check_multimedia(m: &crate::rm::data_types::DvMultimedia, ctx: &mut Context) {
    if !m.has_content() {
        ctx.violation(
            "DV_MULTIMEDIA",
            "Not_empty",
            "neither inline data nor a uri is present",
        );
    }
    // A crate addition (`L10.9`), not openEHR's
    // `Integrity_check_validity` — which says only that a check
    // implies an algorithm, checked below. Reporting a digest
    // mismatch under that name sent a reader to an invariant about
    // something else (`A-22`).
    if m.verify_integrity() == crate::rm::data_types::IntegrityCheck::Failed {
        ctx.violation(
            "DV_MULTIMEDIA",
            "Integrity_check_matches",
            "the recorded digest does not match the inline data",
        );
    }
    // openEHR's actual `Integrity_check_validity`: a check with no
    // algorithm cannot be verified by anyone, so it is a claim of
    // integrity that nothing can act on.
    if m.integrity_check().is_some() && m.integrity_check_algorithm().is_none() {
        ctx.violation(
            "DV_MULTIMEDIA",
            "Integrity_check_validity",
            "an integrity check is present with no algorithm naming how it was made",
        );
    }
    if let Some(algorithm) = m.integrity_check_algorithm()
        && terminology::integrity_check_algorithm::GROUP
            .rubric(algorithm.code_string())
            .is_none()
    {
        ctx.violation(
            "DV_MULTIMEDIA",
            "Integrity_check_algorithm_validity",
            "the integrity check algorithm is not one openEHR names",
        );
    }
    if let Some(algorithm) = m.compression_algorithm()
        && terminology::compression_algorithm::GROUP
            .rubric(algorithm.code_string())
            .is_none()
    {
        ctx.violation(
            "DV_MULTIMEDIA",
            "Compression_algorithm_validity",
            "the compression algorithm is not one openEHR names",
        );
    }
    if m.size().is_some_and(|s| s < 0) {
        ctx.violation("DV_MULTIMEDIA", "Size_valid", "size is negative");
    }
}

/// Checks a `VERSION`'s own invariants — the envelope, not the content.
///
/// # Why this exists
///
/// [`OriginalVersion::new`] checks `Lifecycle_state_valid`, `Data_valid`, and
/// `Preceding_version_uid_validity`. **Deserialization checked none of them**,
/// because the type carries a derived `Deserialize` that writes the fields
/// straight in, and nothing else validated a version at all — the store
/// validated the composition inside it and never the envelope around it.
///
/// So a version arriving as JSON could name a lifecycle state openEHR does not
/// define, or claim `complete` and carry no content, and be committed. That is
/// `A-23`, and this is the half of the fix that covers the path an HTTP service
/// actually takes.
///
/// Validating rather than refusing to deserialize is deliberate and matches the
/// rest of the crate: reading is lenient so that a document can be inspected,
/// repaired, or reported on (`J9.9`), and `validate()` is where a caller finds
/// out what is wrong with it. What was missing was any way to ask.
impl<T: Validate> Validate for crate::rm::common::Version<T> {
    fn visit(&self, ctx: &mut Context) {
        if !crate::terminology::version_lifecycle_state::GROUP.contains(self.lifecycle_state_code())
        {
            ctx.violation(
                "ORIGINAL_VERSION",
                "Lifecycle_state_valid",
                "lifecycle_state is not from the openEHR version_lifecycle_state group",
            );
        }
        // A version with no data and a non-deleted state claims its content is
        // finished and then does not supply it.
        if self.data().is_none()
            && self.lifecycle_state_code() != crate::terminology::version_lifecycle_state::DELETED
        {
            ctx.violation(
                "ORIGINAL_VERSION",
                "Data_valid",
                "a version with no data must carry the deleted lifecycle state",
            );
        }
        // `uid.version_tree_id.is_first xor preceding_version_uid /= Void`.
        // Both halves are wrong in their own way: a first version naming a
        // predecessor claims a history that does not exist, and a successor
        // naming none starts a second history inside one container.
        if self.uid().version_tree_id().is_first() == self.preceding_version_uid().is_some() {
            ctx.violation(
                "VERSION",
                "Preceding_version_uid_validity",
                "the first version must have no preceding_version_uid and every \
                 later version must have one",
            );
        }
        // `Attestations_valid` and `Other_input_version_uids_valid` are
        // `X /= Void implies not X.is_empty`, which cannot fail here: both are
        // `Vec`, and an empty `Vec` *is* the absent case. Named so that a
        // reader looking for them finds why they are not checked rather than
        // concluding they were missed.
        if let Some(data) = self.data() {
            ctx.nested("/data".to_owned(), |c| data.visit(c));
        }
    }
}

/// Checks an `EHR`'s four reference collections and its two required
/// references.
///
/// # Why the constructor was not enough
///
/// `A-21` made `Ehr::new` check `Ehr_status_valid` and `Ehr_access_valid`. The
/// four collections are filled by infallible `with_*` builders, which a
/// constructor cannot see, and `Deserialize` is derived — so an `EHR` read from
/// JSON reached neither check. That is `A-23`'s shape again, in a second class,
/// and it is why both the constructor's rules are repeated here rather than
/// assumed.
///
/// What these catch is a reference pointing at the wrong kind of thing: an
/// `EHR` whose `compositions` list names a `CONTRIBUTION`, or whose `directory`
/// names a versioned composition. Rust's type system cannot tell them apart —
/// every one is an `OBJECT_REF` — so the type name is the only thing that can.
impl Validate for crate::rm::ehr::Ehr {
    fn visit(&self, ctx: &mut Context) {
        // Written out rather than looped, because the class and invariant must
        // be **literals** at the call site. `openehr-assets` reads these two
        // call forms to decide which invariants the crate can name, and a
        // violation raised through a variable is one it cannot see — which is
        // how the first version of this impl left all four rules counted as
        // unnamed (`lib:A-25`).
        if self.ehr_status().type_name() != "VERSIONED_EHR_STATUS" {
            ctx.violation(
                "EHR",
                "Ehr_status_valid",
                "ehr_status does not reference a VERSIONED_EHR_STATUS",
            );
        }
        if self.ehr_access().type_name() != "VERSIONED_EHR_ACCESS" {
            ctx.violation(
                "EHR",
                "Ehr_access_valid",
                "ehr_access does not reference a VERSIONED_EHR_ACCESS",
            );
        }
        if self
            .compositions()
            .iter()
            .any(|r| r.type_name() != "VERSIONED_COMPOSITION")
        {
            ctx.violation(
                "EHR",
                "Compositions_valid",
                "a composition reference does not name a VERSIONED_COMPOSITION",
            );
        }
        if self
            .contributions()
            .iter()
            .any(|r| r.type_name() != "CONTRIBUTION")
        {
            ctx.violation(
                "EHR",
                "Contributions_valid",
                "a contribution reference does not name a CONTRIBUTION",
            );
        }
        if self
            .folders()
            .iter()
            .any(|r| r.type_name() != "VERSIONED_FOLDER")
        {
            ctx.violation(
                "EHR",
                "Folders_valid",
                "a folder reference does not name a VERSIONED_FOLDER",
            );
        }
        if self
            .directory()
            .is_some_and(|d| d.type_name() != "VERSIONED_FOLDER")
        {
            ctx.violation(
                "EHR",
                "Directory_valid",
                "directory does not reference a VERSIONED_FOLDER",
            );
        }
    }
}

/// Checks a `VERSIONED_OBJECT`'s identity and every version it holds.
///
/// `Uid_validity` requires the container's uid to carry **no extension**. A
/// `HIER_OBJECT_ID` may have one — `root::extension` — and openEHR forbids it
/// here because the extension is what distinguishes two objects sharing a root,
/// and a version container's identity is the root. One with an extension names
/// something narrower than the thing all its versions belong to.
///
/// `All_versions_valid`, `All_version_ids_valid`, and `Latest_version_valid`
/// are not checked and cannot fail: `version_count()` returns `versions.len()`
/// and `latest_version()` returns `versions.last()`, so each is true by
/// construction.
impl<T: Validate> Validate for crate::rm::common::VersionedObject<T> {
    fn visit(&self, ctx: &mut Context) {
        if self.uid().extension().is_some() {
            ctx.violation(
                "VERSIONED_OBJECT",
                "Uid_validity",
                "the container's uid carries an extension, so it names something \
                 narrower than the object its versions belong to",
            );
        }
        for (i, version) in self.all_versions().iter().enumerate() {
            ctx.nested(format!("/versions[{i}]"), |c| version.visit(c));
        }
    }
}

/// `EHR_ACCESS.Is_archetype_root`.
///
/// The fourth class to assert it, after `COMPOSITION`, `EHR_STATUS`, and
/// `ENTRY`. An `EHR_ACCESS` is a versioned, archetyped object in its own right,
/// and one without `archetype_details` cannot say which archetype shaped it.
///
/// `Scheme_valid` is **not** checked here, and the reason is a departure worth
/// naming. openEHR derives `scheme` from the concrete `settings` instance and
/// requires it non-empty, which means an `EHR_ACCESS` must always carry a
/// policy. This crate's `EhrAccess::new` deliberately builds one with **no
/// policy recorded**, because "no access policy has been set" and "the policy
/// is deny-all" are different facts and collapsing them would invent one. The
/// divergence is recorded rather than enforced away.
impl Validate for crate::security::access::EhrAccess {
    fn visit(&self, ctx: &mut Context) {
        check_locatable(self, ctx);
        if !self.is_archetype_root() {
            ctx.violation(
                "EHR_ACCESS",
                "Is_archetype_root",
                "an EHR_ACCESS has no archetype_details, so it is not an archetype root",
            );
        }
    }
}

/// `PARTY.Is_archetype_root`.
///
/// A demographic `PARTY` is the root of a party archetype, exactly as a
/// `COMPOSITION` is the root of a composition archetype.
impl Validate for crate::rm::demographic::Party {
    fn visit(&self, ctx: &mut Context) {
        use crate::rm::demographic::Party;
        // Per variant, because `Locatable` is in scope only once the concrete
        // type is known — the same shape as `check_locatable_entry`.
        let check = |rooted: bool, ctx: &mut Context| {
            if !rooted {
                ctx.violation(
                    "PARTY",
                    "Is_archetype_root",
                    "a party has no archetype_details, so it is not an archetype root",
                );
            }
        };
        match self {
            Party::Person(p) => {
                check_locatable(p, ctx);
                check(p.is_archetype_root(), ctx);
            }
            Party::Organisation(p) => {
                check_locatable(p, ctx);
                check(p.is_archetype_root(), ctx);
            }
            Party::Group(p) => {
                check_locatable(p, ctx);
                check(p.is_archetype_root(), ctx);
            }
            Party::Agent(p) => {
                check_locatable(p, ctx);
                check(p.is_archetype_root(), ctx);
            }
            // A ROLE is a PARTY too, and openEHR declares the invariant on
            // PARTY, so the violation is reported against PARTY rather than
            // against the subtype a reader happens to be holding.
            Party::Role(p) => {
                check_locatable(p, ctx);
                check(p.is_archetype_root(), ctx);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::base::Interval;
    use crate::rm::common::{Archetyped, LocatableAttrs, PartyIdentified};
    use crate::rm::data_structures::{ItemSingle, PointEvent};
    use crate::rm::data_types::{CodePhrase, DvCount, DvDateTime, DvQuantity, DvText};
    use crate::rm::ehr::{EntryAttrs, Evaluation, Observation};
    use crate::terminology;

    fn attrs(name: &str, node: &str) -> LocatableAttrs {
        LocatableAttrs::named(name, node).unwrap()
    }

    fn structure() -> ItemStructure {
        ItemSingle::new(
            attrs("d", "at0001"),
            Element::new(attrs("v", "at0002"), DataValue::Count(DvCount::new(1))),
        )
        .into()
    }

    fn entry_attrs() -> EntryAttrs {
        EntryAttrs::about_subject(
            CodePhrase::new("ISO_639-1", "en").unwrap(),
            CodePhrase::new("IANA_character-sets", "UTF-8").unwrap(),
        )
    }

    fn composition() -> Composition {
        Composition::new(
            attrs("Encounter", "openEHR-EHR-COMPOSITION.encounter.v1").with_archetype_details(
                Archetyped::new("openEHR-EHR-COMPOSITION.encounter.v1", "1.1.0").unwrap(),
            ),
            terminology::composition_category::EVENT,
            PartyIdentified::named("Dr A Nurse").unwrap().into(),
            CodePhrase::new("ISO_639-1", "en").unwrap(),
            CodePhrase::new("ISO_3166-1", "GB").unwrap(),
        )
        .unwrap()
    }

    #[test]
    fn a_well_formed_composition_has_no_violations() {
        let c = composition().with_content(
            Evaluation::new(
                attrs("Problem", "openEHR-EHR-EVALUATION.problem.v1").with_archetype_details(
                    Archetyped::new("openEHR-EHR-EVALUATION.problem.v1", "1.1.0").unwrap(),
                ),
                entry_attrs(),
                structure(),
            )
            .into(),
        );
        let report = c.validate();
        assert!(report.is_empty(), "{report}");
    }

    #[test]
    fn deserialization_bypasses_constructors_and_validation_catches_it() {
        // The mutation check for this whole module: if `visit` stopped
        // checking the null invariant, this element would validate clean.
        let json = r#"{
          "name": {"value": "Systolic"},
          "archetype_node_id": "at0004",
          "value": {"_type": "DV_COUNT", "magnitude": 1},
          "null_flavour": {"value": "unknown", "defining_code":
            {"terminology_id": {"value": "openehr"}, "code_string": "253"}}
        }"#;
        let element: Element = serde_json::from_str(json).unwrap();
        let report = element.validate();
        assert_eq!(report.len(), 1);
        assert_eq!(
            report.violations()[0].invariant,
            "Inv_null_flavour_indicated"
        );
    }

    #[test]
    fn an_element_with_nothing_at_all_is_also_a_violation() {
        let json = r#"{"name": {"value": "Systolic"}, "archetype_node_id": "at0004"}"#;
        let element: Element = serde_json::from_str(json).unwrap();
        assert_eq!(element.validate().len(), 1);
    }

    #[test]
    fn an_archetype_id_on_the_wrong_class_is_reported() {
        let wrong = Evaluation::new(
            attrs("Problem", "openEHR-EHR-OBSERVATION.blood_pressure.v2").with_archetype_details(
                Archetyped::new("openEHR-EHR-OBSERVATION.blood_pressure.v2", "1.1.0").unwrap(),
            ),
            entry_attrs(),
            structure(),
        );
        let report = Entry::from(wrong).validate();
        assert_eq!(report.len(), 1);
        assert_eq!(
            report.violations()[0].invariant,
            "Archetype_id_rm_entity_matches"
        );
    }

    #[test]
    fn a_coded_text_that_contradicts_its_own_code_is_reported() {
        let json = r#"{
          "name": {"value": "Category"},
          "archetype_node_id": "at0001",
          "value": {"_type": "DV_CODED_TEXT", "value": "deletion",
            "defining_code": {"terminology_id": {"value": "openehr"}, "code_string": "249"}}
        }"#;
        let element: Element = serde_json::from_str(json).unwrap();
        let report = element.validate();
        assert_eq!(report.len(), 1);
        assert_eq!(report.violations()[0].invariant, "Value_is_rubric");
        // The path locates the offending node without repeating its content.
        assert_eq!(report.violations()[0].path, "/value");
    }

    #[test]
    fn violation_paths_locate_the_node_and_never_the_value() {
        let marker = "ZZ-DISTINCTIVE-9999";
        let bad_element: Element = serde_json::from_str(&format!(
            r#"{{"name": {{"value": "{marker}"}}, "archetype_node_id": "at0004"}}"#
        ))
        .unwrap();
        let structure: ItemStructure = ItemSingle::new(attrs("d", "at0001"), bad_element).into();
        let report = structure.validate();
        assert_eq!(report.violations()[0].path, "/item");
        assert!(!report.to_string().contains(marker), "{report}");
    }

    #[test]
    fn an_event_before_its_history_origin_is_reported() {
        let event = PointEvent::new(
            attrs("early", "at0006"),
            DvDateTime::new("2026-07-31T08:00:00Z").unwrap(),
            structure(),
        );
        let history = History::new(
            attrs("Event Series", "at0001"),
            DvDateTime::new("2026-07-31T09:00:00Z").unwrap(),
            vec![event.into()],
            None,
        )
        .unwrap();
        let observation = Observation::new(
            attrs("Obs", "openEHR-EHR-OBSERVATION.blood_pressure.v2").with_archetype_details(
                Archetyped::new("openEHR-EHR-OBSERVATION.blood_pressure.v2", "1.1.0").unwrap(),
            ),
            entry_attrs(),
            history,
        );
        let report = Entry::from(observation).validate();
        assert_eq!(report.len(), 1);
        assert_eq!(report.violations()[0].invariant, "Time_after_origin");
        assert_eq!(report.violations()[0].path, "/data/events[0]");
    }

    #[test]
    fn every_violation_is_reported_not_just_the_first() {
        let json = r#"{
          "name": {"value": ""},
          "archetype_node_id": "",
          "value": {"_type": "DV_QUANTITY", "magnitude": 1.0, "units": "", "precision": -3}
        }"#;
        let element: Element = serde_json::from_str(json).unwrap();
        let report = element.validate();
        // empty node id, empty name, empty units, negative precision.
        assert_eq!(report.len(), 4, "{report}");
    }

    #[test]
    fn a_normal_flag_beside_an_abnormal_number_is_reported() {
        // `DV_ORDERED.Normal_range_and_status_consistency`. This is the
        // real-world shape of the defect: a result arrives from one system and
        // its abnormal flag from another, and the clinician sees "N" next to a
        // potassium of 9.9.
        let range = Interval::closed(
            DataValue::Quantity(DvQuantity::new(3.5, "mmol/l").unwrap()),
            DataValue::Quantity(DvQuantity::new(5.5, "mmol/l").unwrap()),
        )
        .unwrap();
        let lying = DvQuantity::new(9.9, "mmol/l")
            .unwrap()
            .with_normal_range(range.clone())
            .with_normal_status(CodePhrase::openehr(terminology::normal_status::NORMAL).unwrap());
        let report = Element::new(attrs("K", "at0004"), DataValue::Quantity(lying)).validate();
        assert_eq!(report.len(), 1, "{report}");
        assert_eq!(
            report.violations()[0].invariant,
            "Normal_range_and_status_consistency"
        );

        // The same value flagged `HHH` is consistent, and so is an in-range
        // value flagged `N`.
        let truthful = DvQuantity::new(9.9, "mmol/l")
            .unwrap()
            .with_normal_range(range.clone())
            .with_normal_status(
                CodePhrase::openehr(terminology::normal_status::VERY_HIGH).unwrap(),
            );
        assert!(
            Element::new(attrs("K", "at0004"), DataValue::Quantity(truthful))
                .validate()
                .is_empty()
        );
        let in_range = DvQuantity::new(4.2, "mmol/l")
            .unwrap()
            .with_normal_range(range)
            .with_normal_status(CodePhrase::openehr(terminology::normal_status::NORMAL).unwrap());
        assert!(
            Element::new(attrs("K", "at0004"), DataValue::Quantity(in_range))
                .validate()
                .is_empty()
        );
    }

    #[test]
    fn a_normal_status_outside_the_code_set_is_reported() {
        // `DV_ORDERED.Normal_status_validity`. A renderer shows this letter
        // verbatim beside a result, so an invented one reaches a clinician.
        let odd = DvQuantity::new(4.2, "mmol/l")
            .unwrap()
            .with_normal_status(CodePhrase::openehr("VERY-HIGH-INDEED").unwrap());
        let report = Element::new(attrs("K", "at0004"), DataValue::Quantity(odd)).validate();
        assert_eq!(report.len(), 1, "{report}");
        assert_eq!(report.violations()[0].invariant, "Normal_status_validity");
    }

    #[test]
    fn the_unlimited_precision_sentinel_validates() {
        // The mutation check for the A-01 fix: a validator requiring
        // `precision >= 0` reports a violation here, and the data is
        // conformant.
        let unlimited = DvQuantity::new(1.5, "mg")
            .unwrap()
            .with_precision(-1)
            .unwrap();
        assert!(
            Element::new(attrs("q", "at0004"), DataValue::Quantity(unlimited))
                .validate()
                .is_empty()
        );
    }

    #[test]
    fn a_valid_quantity_with_a_normal_range_still_validates() {
        let q = DvQuantity::new(4.2, "mmol/l").unwrap();
        let element = Element::new(attrs("Glucose", "at0004"), DataValue::Quantity(q));
        assert!(element.validate().is_empty());
        let _ = DvText::new("unused");
    }

    #[test]
    fn an_ehr_reference_pointing_at_the_wrong_kind_of_thing_is_a_violation() {
        use crate::base::{HierObjectId, ObjectId, ObjectRef};
        use crate::rm::data_types::DvDateTime;
        use crate::rm::ehr::Ehr;

        let uid = HierObjectId::from_uid_str("87284370-2D4B-4E3D-A3F3-F303D2F4F34B").unwrap();
        let reference =
            |ty: &str| ObjectRef::new("local", ty, ObjectId::HierObjectId(uid.clone())).unwrap();
        let ehr = || {
            Ehr::new(
                HierObjectId::from_uid_str("11111111-2222-3333-4444-555555555555").unwrap(),
                uid.clone(),
                reference("VERSIONED_EHR_STATUS"),
                reference("VERSIONED_EHR_ACCESS"),
                DvDateTime::new("2026-08-01T09:00:00Z").unwrap(),
            )
            .unwrap()
        };

        assert!(ehr().validate().is_empty());

        // Every one of these is an OBJECT_REF, so nothing but the type name can
        // tell them apart. The builders are infallible, which is why the
        // constructor could never have caught them.
        let cases = [
            (
                ehr().with_composition(reference("CONTRIBUTION")),
                "Compositions_valid",
            ),
            (
                ehr().with_contribution(reference("VERSIONED_COMPOSITION")),
                "Contributions_valid",
            ),
            // `with_folders` sets `directory` to the first folder, so one bad
            // folder reference trips both rules — which is the model's own
            // doing (`Directory_in_folders`) and worth seeing.
            (
                ehr()
                    .with_folders(vec![reference("VERSIONED_COMPOSITION")])
                    .unwrap(),
                "Folders_valid",
            ),
            (
                ehr()
                    .with_folders(vec![reference("VERSIONED_COMPOSITION")])
                    .unwrap(),
                "Directory_valid",
            ),
        ];
        for (subject, invariant) in cases {
            let report = subject.validate();
            assert!(
                report.violations().iter().any(|v| v.invariant == invariant),
                "{invariant} not reported: {report:?}"
            );
        }
    }

    #[test]
    fn an_ehr_that_never_went_through_the_constructor_is_still_checked() {
        use crate::rm::ehr::Ehr;

        // `A-21` made `Ehr::new` refuse these two. Deserialization does not call
        // it, so until `Validate for Ehr` existed an EHR read from JSON was
        // checked by nothing at all — the same hole as `A-23`, one class along.
        let json = r#"{
          "system_id": {"_type":"HIER_OBJECT_ID","value":"11111111-2222-3333-4444-555555555555"},
          "ehr_id": {"_type":"HIER_OBJECT_ID","value":"87284370-2D4B-4E3D-A3F3-F303D2F4F34B"},
          "ehr_status": {"_type":"OBJECT_REF","namespace":"local","type":"EHR",
            "id":{"_type":"HIER_OBJECT_ID","value":"87284370-2D4B-4E3D-A3F3-F303D2F4F34B"}},
          "ehr_access": {"_type":"OBJECT_REF","namespace":"local","type":"EHR",
            "id":{"_type":"HIER_OBJECT_ID","value":"87284370-2D4B-4E3D-A3F3-F303D2F4F34B"}},
          "time_created": {"_type":"DV_DATE_TIME","value":"2026-08-01T09:00:00Z"}
        }"#;
        let ehr: Ehr = serde_json::from_str(json).expect("deserialization is lenient (J9.9)");
        let report = ehr.validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Ehr_status_valid"),
            "{report:?}"
        );
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Ehr_access_valid"),
            "{report:?}"
        );
    }

    #[test]
    fn an_interval_reports_openehrs_own_name_for_its_bounds_rule() {
        use crate::base::Interval;

        // `lib:A-24`: enforced all along, reported as `INTERVAL` with prose, so
        // a reader could not find the rule in any class definition (`L10.4`).
        let err = Interval::closed(10i64, 1i64).unwrap_err().to_string();
        assert!(err.contains("DV_INTERVAL"), "{err}");
        assert!(err.contains("Limits_consistent"), "{err}");
    }

    #[test]
    fn a_term_mapping_purpose_outside_openehrs_group_is_a_violation() {
        use crate::rm::common::LocatableAttrs;
        use crate::rm::data_structures::Element;
        use crate::rm::data_types::{
            CodePhrase, DataValue, DvCodedText, DvText, MappingMatch, TermMapping,
        };

        let mapped = |purpose_code: &str| {
            let mapping = TermMapping::new(
                CodePhrase::new("SNOMED-CT", "73211009").unwrap(),
                MappingMatch::Equivalent,
            )
            .with_purpose(
                DvCodedText::new("purpose", CodePhrase::new("openehr", purpose_code).unwrap())
                    .unwrap(),
            );
            // Carried on the node's *name*, which is where `check_locatable`
            // reaches a text.
            let name = DvText::new("Diabetes").unwrap().with_mapping(mapping);
            Element::new(
                LocatableAttrs::new(name.into(), "at0004").unwrap(),
                DataValue::Text(DvText::new("present").unwrap()),
            )
        };

        // `669` is `public health` — a real member of the group that has
        // shipped all along while nothing consulted it (`A-24`).
        let ok = mapped("669");
        assert!(ok.validate().is_empty(), "{:?}", ok.validate());

        let bad = mapped("9999");
        let report = bad.validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Purpose_valid"),
            "{report:?}"
        );
    }

    #[test]
    fn a_versioned_object_uid_may_not_carry_an_extension() {
        use crate::base::{HierObjectId, ObjectId, ObjectRef};
        use crate::rm::common::VersionedObject;
        use crate::rm::data_types::DvDateTime;

        let owner = ObjectRef::new(
            "local",
            "EHR",
            ObjectId::HierObjectId(
                HierObjectId::from_uid_str("87284370-2D4B-4E3D-A3F3-F303D2F4F34B").unwrap(),
            ),
        )
        .unwrap();
        let at = DvDateTime::new("2026-08-01T09:00:00Z").unwrap();

        // `Composition` rather than `String`: the impl requires `T: Validate`,
        // because a container that checked its own uid and not its versions
        // would be a walk that stops at the interesting part.
        let plain: VersionedObject<crate::rm::ehr::Composition> = VersionedObject::new(
            HierObjectId::from_uid_str("87284370-2D4B-4E3D-A3F3-F303D2F4F34B").unwrap(),
            owner.clone(),
            at.clone(),
        );
        assert!(plain.validate().is_empty());

        // The extension distinguishes two objects sharing a root, and a version
        // container's identity is the root.
        let extended: VersionedObject<crate::rm::ehr::Composition> = VersionedObject::new(
            "87284370-2D4B-4E3D-A3F3-F303D2F4F34B::narrower"
                .parse()
                .unwrap(),
            owner,
            at,
        );
        let report = extended.validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Uid_validity"),
            "{report:?}"
        );
    }

    #[test]
    fn an_entry_that_is_not_an_archetype_root_is_reported() {
        // openEHR states `ENTRY.Is_archetype_root` as a bare assertion: an
        // ENTRY *is* the root of an entry archetype. Enforcing it found seven
        // fixtures that were not, including the README's example of "a
        // composition another implementation wrote" — the `A-21` shape again.
        let entry = Entry::from(Evaluation::new(
            attrs("Problem", "at0000"),
            entry_attrs(),
            structure(),
        ));
        let report = entry.validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Is_archetype_root"),
            "{report:?}"
        );
    }

    #[test]
    fn an_entry_about_the_record_subject_must_name_a_party_self() {
        use crate::rm::common::{PartyRelated, PartySelf};

        let rooted = || {
            attrs("Problem", "openEHR-EHR-EVALUATION.problem.v1").with_archetype_details(
                Archetyped::new("openEHR-EHR-EVALUATION.problem.v1", "1.1.0").unwrap(),
            )
        };
        let about = |subject: crate::rm::common::PartyProxy| {
            Entry::from(Evaluation::new(
                rooted(),
                crate::rm::ehr::EntryAttrs::about(
                    CodePhrase::new("ISO_639-1", "en").unwrap(),
                    CodePhrase::new("IANA_character-sets", "UTF-8").unwrap(),
                    subject,
                ),
                structure(),
            ))
        };

        // A PARTY_SELF subject is the ordinary case and says nothing.
        assert!(
            !about(PartySelf::anonymous().into())
                .validate()
                .violations()
                .iter()
                .any(|v| v.invariant == "Subject_validity")
        );

        // `PARTY_RELATED` with a `self` relationship answers true to
        // `is_subject` and is not a `PARTY_SELF`. openEHR's BMM documents
        // `subject_is_self` as implying the type, so in openEHR the two agree
        // by construction; here they can diverge, and an entry can claim to be
        // about the patient while naming a related party.
        let related = PartyRelated::new(
            PartyIdentified::named("The patient").unwrap(),
            crate::terminology::subject_relationship::SELF,
        )
        .unwrap();
        let report = about(related.into()).validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Subject_validity"),
            "{report:?}"
        );
    }

    #[test]
    fn a_reference_range_endpoint_may_not_carry_reference_ranges_of_its_own() {
        use crate::base::Interval;
        use crate::rm::data_types::{DataValue, DvQuantity, ReferenceRange};

        // The model is genuinely this cyclic: a quantity's reference range is
        // expressed as quantities, each of which can carry reference ranges.
        // Without `Range_is_simple` a range nests to any depth, and a renderer
        // showing "normal: 4.0–6.0" either recurses forever or shows only the
        // outermost layer.
        let plain = |v: f64| DvQuantity::new(v, "mmol/l").unwrap();
        let nested = plain(4.0).with_other_reference_range(ReferenceRange::new(
            DvText::new("inner").unwrap(),
            Interval::closed(
                DataValue::Quantity(plain(1.0)),
                DataValue::Quantity(plain(2.0)),
            )
            .unwrap(),
        ));

        let subject = plain(5.0).with_other_reference_range(ReferenceRange::new(
            DvText::new("normal").unwrap(),
            Interval::closed(
                DataValue::Quantity(nested),
                DataValue::Quantity(plain(6.0)),
            )
            .unwrap(),
        ));
        let report = DataValue::Quantity(subject).validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.invariant == "Range_is_simple"),
            "{report:?}"
        );

        // A simple range says nothing.
        let ok = plain(5.0).with_other_reference_range(ReferenceRange::new(
            DvText::new("normal").unwrap(),
            Interval::closed(
                DataValue::Quantity(plain(4.0)),
                DataValue::Quantity(plain(6.0)),
            )
            .unwrap(),
        ));
        assert!(
            !DataValue::Quantity(ok)
                .validate()
                .violations()
                .iter()
                .any(|v| v.invariant == "Range_is_simple")
        );
    }

    /// The version envelope, checked here rather than only from the crates
    /// that call it.
    ///
    /// `A-23` added this impl and its tests live in `openehr-loco` and
    /// `openehr-sqlite`. `cargo mutants` runs the tests of the crate it
    /// mutates, so replacing the whole `visit` body with `()` left `openehr`
    /// green — the fix for a High finding, removable without this crate
    /// noticing (`lib:A-09`).
    #[test]
    fn a_version_envelope_is_checked_on_data_that_arrived_as_json() {
        // Deserialization never calls a constructor, which is the whole of
        // `A-23`. Each of these is well-formed JSON and an impossible version.
        let base = |lifecycle: &str, extra: &str| {
            format!(
                r#"{{
                  "_type": "ORIGINAL_VERSION",
                  "uid": {{"_type":"OBJECT_VERSION_ID","value":"87284370-2D4B-4E3D-A3F3-F303D2F4F34B::s::1"}},
                  "lifecycle_state": {{"_type":"DV_CODED_TEXT","value":"x",
                    "defining_code":{{"_type":"CODE_PHRASE","terminology_id":{{"value":"openehr"}},"code_string":"{lifecycle}"}}}},
                  "commit_audit": {{"_type":"AUDIT_DETAILS","system_id":"s",
                    "time_committed":{{"_type":"DV_DATE_TIME","value":"2026-08-01T09:00:00Z"}},
                    "change_type":{{"_type":"DV_CODED_TEXT","value":"creation",
                      "defining_code":{{"_type":"CODE_PHRASE","terminology_id":{{"value":"openehr"}},"code_string":"249"}}}},
                    "committer":{{"_type":"PARTY_IDENTIFIED","name":"N"}}}},
                  "contribution": {{"_type":"OBJECT_REF","namespace":"local","type":"EHR",
                    "id":{{"_type":"HIER_OBJECT_ID","value":"87284370-2D4B-4E3D-A3F3-F303D2F4F34B"}}}}
                  {extra}
                }}"#
            )
        };
        let parse = |json: &str| {
            serde_json::from_str::<crate::rm::common::Version<crate::rm::ehr::Composition>>(json)
                .expect("deserialization is lenient by design (J9.9)")
        };
        let reports = |json: &str| parse(json).validate();

        // `Lifecycle_state_valid`: a code openEHR does not define.
        assert!(
            reports(&base("9999", ""))
                .violations()
                .iter()
                .any(|v| v.invariant == "Lifecycle_state_valid")
        );

        // `Data_valid`: `complete` with no data. 532 is `complete`.
        assert!(
            reports(&base("532", ""))
                .violations()
                .iter()
                .any(|v| v.invariant == "Data_valid")
        );

        // `Preceding_version_uid_validity`: version 1 naming a predecessor.
        let with_predecessor = base(
            "532",
            r#", "preceding_version_uid": {"_type":"OBJECT_VERSION_ID","value":"87284370-2D4B-4E3D-A3F3-F303D2F4F34B::s::9"}"#,
        );
        assert!(
            reports(&with_predecessor)
                .violations()
                .iter()
                .any(|v| v.invariant == "Preceding_version_uid_validity")
        );

        // `validate_ok` must actually return `Err`, which nothing asserted —
        // it could have returned `Ok(())` unconditionally.
        assert!(parse(&base("9999", "")).validate_ok().is_err());

        // And a deleted version with no data is the one legal absence. 523 is
        // `deleted`; this is the case `Data_valid` exists to permit, so it must
        // not be reported.
        let deleted = parse(&base("523", ""));
        assert!(
            !deleted
                .validate()
                .violations()
                .iter()
                .any(|v| v.invariant == "Data_valid"),
            "a deleted version may carry no data"
        );
    }

    /// `EHR_ACCESS` and `PARTY` archetype-root checks.
    ///
    /// Both impls were added in the same sitting as this comment **with no
    /// test at all** — the whole of each `visit` could be replaced with `()`
    /// and nothing failed. Mutation testing found it; reading the diff had
    /// not, including by the person who wrote it.
    #[test]
    fn an_ehr_access_and_a_party_must_be_archetype_roots() {
        use crate::rm::demographic::{Party, PartyAttrs, PartyIdentity, Person};
        use crate::security::access::EhrAccess;

        let rooted = |archetype: &str| {
            attrs("subject", archetype).with_archetype_details(
                Archetyped::new(archetype, "1.1.0").expect("literal"),
            )
        };

        let bare = EhrAccess::new(attrs("access", "at0000"));
        assert!(
            bare.validate()
                .violations()
                .iter()
                .any(|v| v.invariant == "Is_archetype_root" && v.class == "EHR_ACCESS")
        );
        assert!(
            EhrAccess::new(rooted("openEHR-EHR-EHR_ACCESS.generic.v1"))
                .validate()
                .is_empty()
        );

        let identity = PartyIdentity::new(
            attrs("legal name", "at0001"),
            crate::rm::data_structures::ItemStructure::Single(
                crate::rm::data_structures::ItemSingle::new(
                    attrs("name", "at0002"),
                    Element::new(
                        attrs("full name", "at0003"),
                        DataValue::Text(DvText::new("A Patient").expect("literal")),
                    ),
                ),
            ),
        );
        // A `PARTY` must carry a uid (`PARTY.Uid_mandatory`), which is a
        // separate rule from being an archetype root and is already enforced
        // in the constructor.
        let person = |locatable: crate::rm::common::LocatableAttrs| {
            let with_uid = locatable.with_uid(crate::base::UidBasedId::HierObjectId(
                crate::base::HierObjectId::from_uid_str("6BA7B810-9DAD-11D1-80B4-00C04FD430C8")
                    .expect("literal"),
            ));
            Party::Person(Person::new(
                PartyAttrs::new(with_uid, vec![identity.clone()]).expect("literal"),
            ))
        };
        assert!(
            person(attrs("patient", "at0000"))
                .validate()
                .violations()
                .iter()
                .any(|v| v.invariant == "Is_archetype_root" && v.class == "PARTY")
        );
        assert!(
            person(rooted("openEHR-DEMOGRAPHIC-PERSON.person.v1"))
                .validate()
                .is_empty()
        );
    }

    /// Validation descends through a `SECTION` and through a `HISTORY`'s
    /// events.
    ///
    /// `Section::visit`, `ContentItem::visit` and `Event::visit` could each be
    /// replaced with `()` and nothing failed: every existing test puts its
    /// entry directly in `content` and its element directly in a tree, so the
    /// two nesting paths a real composition uses were never walked
    /// (`lib:A-09`).
    ///
    /// A composition organised into sections is the ordinary case, not an edge
    /// one, and an unreported violation inside a section is a document that
    /// validates and is wrong.
    #[test]
    fn a_violation_nested_in_a_section_or_an_event_is_still_reported() {
        use crate::rm::data_structures::{History, PointEvent};
        use crate::rm::ehr::{ContentItem, Evaluation, Observation, Section};

        // An element whose name is empty — `DV_TEXT.Valid_value`, the same
        // violation the flat tests use, so only the *path* to it is new.
        // Violations that a *constructor* permits, since the point is the path
        // the walk takes and not the rule. An `ITEM_TABLE` whose rows have
        // different widths breaks `Rows_regular`, and it is checked by
        // validation rather than refused at construction.
        let irregular = || -> crate::rm::data_structures::ItemStructure {
            let cell = |node: &str| {
                Element::new(attrs("cell", node), DataValue::Count(DvCount::new(1))).into()
            };
            crate::rm::data_structures::ItemTable::new(
                attrs("table", "at0009"),
                vec![
                    Cluster::new(attrs("row 1", "at0010"), vec![cell("at0011")]).expect("literal"),
                    Cluster::new(
                        attrs("row 2", "at0012"),
                        vec![cell("at0013"), cell("at0014")],
                    )
                    .expect("literal"),
                ],
            )
            .into()
        };
        let entry_root = |archetype: &str| {
            attrs("nested", archetype)
                .with_archetype_details(Archetyped::new(archetype, "1.1.0").expect("literal"))
        };

        // Through a SECTION: composition -> section -> entry -> element.
        let sectioned = composition().with_content(
            Section::new(
                attrs("Findings", "at0002"),
                vec![ContentItem::Entry(
                    Evaluation::new(
                        entry_root("openEHR-EHR-EVALUATION.problem.v1"),
                        entry_attrs(),
                        irregular(),
                    )
                    .into(),
                )],
            )
            .into(),
        );
        let report = sectioned.validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.path.contains("/items")),
            "nothing inside the section was walked: {report}"
        );

        // Through an EVENT: observation -> history -> event -> element.
        let observed = Observation::new(
            entry_root("openEHR-EHR-OBSERVATION.blood_pressure.v2"),
            entry_attrs(),
            History::new(
                attrs("Event Series", "at0001"),
                DvDateTime::new("2026-07-31T09:00:00Z").expect("literal"),
                vec![
                    PointEvent::new(
                        attrs("any event", "at0006"),
                        DvDateTime::new("2026-07-31T09:15:00Z").expect("literal"),
                        irregular(),
                    )
                    .into(),
                ],
                None,
            )
            .expect("literal"),
        );
        let report = Entry::from(observed).validate();
        assert!(
            report
                .violations()
                .iter()
                .any(|v| v.path.contains("/events")),
            "nothing inside the event was walked: {report}"
        );
    }

    /// Every ordered data value has its `normal_status` checked, not just
    /// `DV_QUANTITY`.
    ///
    /// `check_ordered` is reached through a `match` with one arm per ordered
    /// variant, and deleting the `Count`, `Proportion`, `Ordinal` or `Scale`
    /// arm changed nothing: every existing test used a quantity. A count with
    /// a normal status outside openEHR's code set is a result a renderer shows
    /// verbatim beside a number, and nothing was checking it (`lib:A-09`).
    #[test]
    fn a_bad_normal_status_is_reported_for_every_ordered_type() {
        use crate::rm::data_types::{DvOrdinal, DvProportion, DvScale, ProportionKind};

        // Not in the openEHR normal-statuses code set.
        let bogus = || CodePhrase::new("openehr", "ZZ").expect("literal");
        let symbol = || {
            DvCodedText::new("symbol", CodePhrase::new("local", "at0001").expect("literal"))
                .expect("literal")
        };

        let values = [
            DataValue::Count(DvCount::new(1).with_normal_status(bogus())),
            DataValue::Proportion(
                DvProportion::new(1.0, 2.0, ProportionKind::Ratio)
                    .expect("literal")
                    .with_normal_status(bogus()),
            ),
            DataValue::Ordinal(DvOrdinal::new(1, symbol()).with_normal_status(bogus())),
            DataValue::Scale(
                DvScale::new(1.0, symbol())
                    .expect("literal")
                    .with_normal_status(bogus()),
            ),
            DataValue::Quantity(
                DvQuantity::new(1.0, "mm[Hg]")
                    .expect("literal")
                    .with_normal_status(bogus()),
            ),
        ];

        for value in values {
            let kind = value.type_name();
            let report = value.validate();
            assert!(
                report
                    .violations()
                    .iter()
                    .any(|v| v.invariant == "Normal_status_validity"),
                "{kind} did not have its normal_status checked: {report}"
            );
        }
    }
}