openehr-sdt 0.0.95

openEHR Simplified Data Template formats (FLAT / STRUCTURED / Web Template / TDD), template-independent RM-instance validation, and the SMART on openEHR scope grammar, hand-written over the openehr-its wire layer
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
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! **Archetype-conformance validation** — the template-driven pass, plus the
//! Simplified-Formats input checks.
//!
//! [`validate_archetype_conformance`] walks a canonical-JSON instance guided by
//! its operational template (as a flattened [`WebTemplate`]), matching by
//! `aql_path` + `archetype_node_id`, and checks type conformance, occurrences,
//! cardinality, and leaf domain constraints (coded lists / numeric ranges /
//! string patterns), collecting *all* violations (not fail-fast). Paths are the
//! archetype `aqlPath` of the constraining node.
//!
//! Everything here needs a template. The template-independent passes live in
//! [`crate::rm_instance`], which also owns the shared [`ValidationMessage`]
//! shape and the composed [`crate::rm_instance::validate_composition`] entry
//! point.
//!
//! Two of the Simplified-Formats input rules
//! (`simplified_formats/master04-basic_concepts.adoc` §Validation) are
//! properties of the FLAT input rather than of the RM tree, so they are checked
//! before conversion: [`validate_flat_other`] for the `|other` open-value-set
//! rules and [`validate_context`] for the mandatory `ctx/language` +
//! `ctx/territory` fields.
//!
//! # Fidelity
//!
//! The instance-validation algorithm is spec-underdetermined: the AOM 1.4
//! constraint model defines a positive-only `Valid_value` cascade over the
//! archetype constraint tree and is silent on unmatched instance nodes. The walk
//! here approximates it over the COMPACTED `WebTemplate` — the wrapper nodes the
//! AOM tree carries are folded into a child's `aqlPath`, so the instance is
//! navigated by the RM-attribute + `[archetype_node_id]` chain that separates a
//! child from its compacted parent, counting occurrences per intermediate
//! container. `C_DV_*` leaf semantics are approximated from the `WebTemplate`
//! `inputs`. A check that cannot be made reliably is skipped rather than
//! over-rejected, so only confident violations are reported.

#![expect(
    clippy::disallowed_types,
    reason = "canonical JSON / Simplified Formats operate on the wire value by definition \
              (ITS-JSON; ITS-REST Simplified Formats) — serde_json::Value IS the subject matter \
              (#1694)"
)]

mod leaf;
mod subtype;

use indexmap::IndexMap;
use openehr_rm::v1_2::paths::PathSegment;
use serde_json::{Map, Value};

use crate::rm_instance::{ValidationKind, ValidationMessage};

use crate::flat::path;
use crate::flat::rmpath;
use crate::flat::sim::{SimDocument, SimNode, is_present};
use crate::flat::webtemplate::model::{WebTemplate, WebTemplateArchetypeSlot, WebTemplateNode};

/// Validate only the archetype-conformance pass against a resolved
/// [`WebTemplate`] (type conformance, occurrences, cardinality, and leaf
/// domain constraints).
///
/// Callers run [`crate::rm_instance::validate_rm_and_terminology`] separately
/// for the template-independent checks, so this is the additional pass a
/// *declared* template contributes.
#[must_use]
pub fn validate_archetype_conformance(
    composition: &Value,
    wt: &WebTemplate,
) -> Vec<ValidationMessage> {
    let mut v = Validator::default();
    v.walk(composition, &wt.tree);
    v.out
}

/// Archetype-conformance pass for a `553|incomplete|` commit.
///
/// Identical to
/// [`validate_archetype_conformance`] but with existence/occurrences/cardinality
/// **lower** limits treated as zero (RM common master06 §"Incomplete Content":
/// "in an `incomplete` commit, data may be missing, but it may not be wrong …
/// all existence and cardinality lower limits set to zero"). Type conformance,
/// upper limits, and every leaf/value constraint are still enforced — the
/// relaxation tolerates *missing* data, not *wrong* data. The caller must run
/// the RM-invariant + terminology passes at full strictness regardless
/// (they are properties of the instance, not archetype lower bounds).
#[must_use]
pub fn validate_archetype_conformance_incomplete(
    composition: &Value,
    wt: &WebTemplate,
) -> Vec<ValidationMessage> {
    let mut v = Validator {
        relax_lower_bounds: true,
        ..Validator::default()
    };
    v.walk(composition, &wt.tree);
    v.out
}

/// One terminology-server question the template's archetype **constraint
/// bindings** raise about a coded value in this instance.
///
/// A `CONSTRAINT_REF` bound to an external terminology query names a value set
/// the instance's code must belong to (BASE
/// `architecture_overview/master12-terminology.adoc` §"Binding Terminology
/// Value-sets to Archetypes": the ac-code "is bound to queries to one or more
/// external terminologies, whose result would be a … value set from that
/// terminology"). The query lives in a "terminology query server", which this
/// crate has no access to — so the walk collects the questions and a caller
/// that owns a terminology service answers them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstraintBindingCheck {
    /// The archetype `aqlPath` of the constrained node (the path a resulting
    /// violation is keyed by).
    pub path: String,
    /// The archetype constraint code (`ac0004`) the binding is keyed by.
    pub ac_code: String,
    /// The terminology the binding names (`SNOMED-CT`, `LOINC`, …) — the
    /// routing key for choosing a terminology server.
    pub binding_terminology: String,
    /// The bound terminology-query URI identifying the admissible value set.
    pub query_uri: String,
    /// The instance code's own terminology id (`CODE_PHRASE.terminology_id`).
    pub instance_terminology: String,
    /// The instance code (`CODE_PHRASE.code_string`) whose membership is in
    /// question.
    pub instance_code: String,
}

/// Collects the [`ConstraintBindingCheck`]s a template's bindings raise for an
/// instance.
///
/// The checks are gathered by the same archetype-conformance walk
/// [`validate_archetype_conformance`] performs (so a check is raised only for a
/// node the template actually matched). Pure: no violation is decided here —
/// the caller resolves each query against its terminology service.
#[must_use]
pub fn collect_constraint_binding_checks(
    composition: &Value,
    wt: &WebTemplate,
) -> Vec<ConstraintBindingCheck> {
    let mut v = Validator {
        collect_bindings: true,
        ..Validator::default()
    };
    v.walk(composition, &wt.tree);
    v.bindings
}

/// Validate the `|other` open-value-set rules on a **FLAT input** map, before
/// conversion to RM (ITS-REST `simplified_formats/master04-basic_concepts.adoc`
/// §"Open Value-Sets and the `|other` Suffix"):
///
/// * `|other` MUST NOT co-occur with `|code`/`|value`/`|terminology`/
///   `|preferred_term` on the same leaf path;
/// * `|other` MUST be rejected when the leaf's WT constraint is a **closed**
///   coded list (`listOpen: false`).
///
/// This is the validation-**report** form of the same two rules the conversion
/// path enforces as hard rejections: `crate::flat::map::build_leaf` returns
/// [`crate::flat::error::FlatError::OtherSuffixConflict`] /
/// [`crate::flat::error::FlatError::OtherOnClosedValueSet`] when it hits them during a
/// build, so a converter fails fast. This function instead collects them as
/// [`ValidationMessage`]s (validators report, converters reject), for a caller
/// that wants to surface every violation on the raw FLAT input without running a
/// conversion. An empty result means the input satisfies the `|other` rules this
/// check covers.
#[must_use]
pub fn validate_flat_other(doc: &Map<String, Value>, wt: &WebTemplate) -> Vec<ValidationMessage> {
    const EXCLUSIVE: &[&str] = &["code", "value", "terminology", "preferred_term"];
    let mut out = Vec::new();
    // Group each FLAT key by its segment path (`master04 §Field Identifiers`),
    // collecting the datum suffix (`master05` per-type suffix) present on each.
    let mut per_leaf: IndexMap<String, LeafGroup> = IndexMap::new();
    for key in doc.keys() {
        let Ok(fk) = path::FlatKey::parse(key) else {
            continue; // a malformed key is the conversion path's rejection, not this one's
        };
        if fk.is_ctx() {
            continue;
        }
        // The segment portion is everything before the first pipe (the printed
        // path); the first suffix is the leaf's datum suffix (`master05`).
        let path_str = key.split('|').next().unwrap_or(key).to_owned();
        let suffix = fk.suffixes.first().map(|s| s.name.clone());
        let entry = per_leaf.entry(path_str).or_insert_with(|| LeafGroup {
            segments: fk.segments,
            suffixes: Vec::new(),
        });
        if let Some(s) = suffix {
            entry.suffixes.push(s);
        }
    }
    for (path_str, group) in &per_leaf {
        if !group.suffixes.iter().any(|s| s == "other") {
            continue;
        }
        // Mutual exclusion.
        if let Some(conflict) = group
            .suffixes
            .iter()
            .find(|s| EXCLUSIVE.contains(&s.as_str()))
        {
            out.push(ValidationMessage {
                path: path_str.clone(),
                message: format!(
                    "`|other` is mutually exclusive with `|{conflict}` on the same leaf \
                     (master04 §Open Value-Sets)"
                ),
                kind: ValidationKind::CodedValue,
            });
        }
        // Closed-list rejection: resolve the WT node for this path.
        if let Some(node) = find_node_by_segments(wt, &group.segments)
            && node.inputs.iter().any(|i| i.list_open == Some(false))
        {
            out.push(ValidationMessage {
                path: path_str.clone(),
                message: "`|other` is not allowed on a closed value-set (listOpen: false) \
                          (master04 §Open Value-Sets)"
                    .to_owned(),
                kind: ValidationKind::CodedValue,
            });
        }
    }
    out
}

/// One FLAT leaf-path grouping for [`validate_flat_other`]: the parsed segments
/// (for WT resolution) and the datum suffixes present on the path.
struct LeafGroup {
    segments: Vec<path::Segment>,
    suffixes: Vec<String>,
}

/// The `(path, code)` of every FLAT leaf whose `|code` names a code outside
/// its closed web-template value set while no `|value` suffix supplies the
/// text.
///
/// Such a datum cannot be resolved to a valid coded value (master04
/// §Validation: "Terminology bindings are valid"); the closed-set predicate
/// and the terminology-scope bias mirror the archetype pass's
/// `check_code_membership` (confident violations only — an explicit
/// differently-scoped `|terminology` is left to the terminology pass).
#[must_use]
pub fn unresolvable_coded_leaves(
    doc: &Map<String, Value>,
    wt: &WebTemplate,
) -> Vec<(String, String)> {
    struct CodedLeaf {
        segments: Vec<path::Segment>,
        code: Option<String>,
        has_value: bool,
        terminology: Option<String>,
    }
    let mut per_leaf: IndexMap<String, CodedLeaf> = IndexMap::new();
    for (key, value) in doc {
        let Ok(fk) = path::FlatKey::parse(key) else {
            continue; // a malformed key is the conversion path's rejection, not this one's
        };
        if fk.is_ctx() {
            continue;
        }
        let path_str = key.split('|').next().unwrap_or(key).to_owned();
        let suffix = fk.suffixes.first().map(|s| s.name.clone());
        let entry = per_leaf.entry(path_str).or_insert_with(|| CodedLeaf {
            segments: fk.segments,
            code: None,
            has_value: false,
            terminology: None,
        });
        match suffix.as_deref() {
            Some("code") => entry.code = value.as_str().map(str::to_owned),
            Some("value") => entry.has_value = true,
            Some("terminology") => entry.terminology = value.as_str().map(str::to_owned),
            _ => {}
        }
    }
    let mut out = Vec::new();
    for (path_str, leaf) in per_leaf {
        let Some(code) = leaf.code else { continue };
        if leaf.has_value {
            continue;
        }
        let Some(node) = find_node_by_segments(wt, &leaf.segments) else {
            continue;
        };
        let miss = node.inputs.iter().any(|input| {
            !input.list.is_empty()
                && input.list_open != Some(true)
                && leaf::terminology_matches(
                    input.terminology.as_deref(),
                    leaf.terminology.as_deref(),
                )
                && !input.list.iter().any(|cv| cv.value == code)
        });
        if miss {
            out.push((path_str, code));
        }
    }
    out
}

/// Validates that the **mandatory context fields** are present.
///
/// The fields are checked on a parsed simplified document (ITS-REST
/// `simplified_formats/master04-basic_concepts.adoc`
/// §Validation: "Mandatory context fields (language, territory) are present";
/// §Context: "Mandatory: language, territory").
///
/// A field counts as present when the `ctx/` node carries it with a non-empty
/// bare value, or — because master05 §COMPOSITION also permits the root-level
/// path spelling (`<root>/language|code`, `<root>/territory|code`) — when a
/// non-`ctx` data root carries a child of that name. Returns one
/// [`ValidationMessage`] (`Required`, keyed `ctx/<field>`) per absent field.
#[must_use]
pub fn validate_context(doc: &SimDocument) -> Vec<ValidationMessage> {
    let mut out = Vec::new();
    let ctx = doc.child("ctx");
    for field in ["language", "territory"] {
        let in_ctx = ctx
            .and_then(|c| c.child(field))
            .and_then(SimNode::bare)
            .is_some_and(is_present);
        // master05 §COMPOSITION permits the root-level path spelling too; a value
        // there satisfies the mandatory-field rule, so it is not falsely flagged.
        let in_root = doc.children.iter().any(|(name, child)| {
            name.as_str() != "ctx"
                && child
                    .occurrences
                    .iter()
                    .any(|occ| occ.child(field).is_some())
        });
        if !in_ctx && !in_root {
            out.push(ValidationMessage {
                path: format!("ctx/{field}"),
                message: format!(
                    "mandatory context field '{field}' is absent (language and territory \
                     are required)"
                ),
                kind: ValidationKind::Required,
            });
        }
    }
    out
}

/// Resolve the [`WebTemplateNode`] a FLAT leaf's parsed `segments` address by
/// descending the WT tree on json-id segments (indices ignored, the leading root
/// id checked). Returns `None` for an unknown path or a `_`-prefixed RM-attribute
/// segment (which addresses no template node).
fn find_node_by_segments<'a>(
    wt: &'a WebTemplate,
    segments: &[path::Segment],
) -> Option<&'a WebTemplateNode> {
    let mut iter = segments.iter();
    // The first segment is the root template id.
    let first = iter.next()?;
    if first.name != wt.tree.id {
        return None;
    }
    let mut node = &wt.tree;
    for seg in iter {
        if seg.is_rm_attribute() {
            return None; // an RM-attribute segment addresses no template node
        }
        node = node
            .children
            .iter()
            .find(|c| c.id == seg.name || c.alt_json_id.as_deref() == Some(seg.name.as_str()))?;
    }
    Some(node)
}

/// Name-based routing for same-archetype-id sibling constraints.
///
/// Maps each identity `(attribute, archetype_node_id)` that the template repeats
/// across more than one distinct sibling path (relative to `parent_aql`) — i.e.
/// the template differentiates same-`archetype_node_id` siblings by their
/// runtime `name` (RM common `master03-archetyped_package.adoc` §"The
/// `LOCATABLE` class" L33-35; AOM 1.4 `master04-constraint_model_package.adoc`
/// §`node_id` L41) — to the set of explicit `name/value` `C_STRING` constraints
/// its *name-qualified* siblings carry.
///
/// A present key marks the identity as name-differentiated, which routing uses
/// two ways ([`select_group_children`]): a name-qualified sibling
/// matches strictly (id + name, no id-only fallback), and the one *unqualified*
/// sibling (whose `name` is unconstrained) admits every same-id instance
/// **except** those bearing a name a name-qualified sibling claims.
pub(crate) type SiblingNameIndex = std::collections::HashMap<(String, String), Vec<String>>;

// ── pre-parsed archetype-conformance walk plan ─────────────────────────────────
//
// The archetype-conformance walk ([`Validator::walk`]) is guided by the
// constraint paths a [`WebTemplateNode`] carries, which are template-static:
// [`prepare_walk`] parses them once at build time into the cached
// [`WebTemplateNode::walk`]; a node without a cached plan gets an identical
// [`NodeWalk`] built per visit, so there is one code path. No openEHR spec
// governs the WebTemplate model or this plan — our own design/extension.

/// A pre-parsed archetype-conformance walk plan for one [`WebTemplateNode`].
///
/// Every `Vec` is index-aligned with the corresponding node constraint vector so
/// the walk can zip them; `None` marks a constraint path that is not an
/// extension of the node's own `aqlPath`.
#[derive(Debug, Clone)]
pub(crate) struct NodeWalk {
    /// Child sibling groups, in first-seen order (grouping the node's `children`
    /// by `aqlPath`).
    pub(crate) child_groups: Vec<ChildGroup>,
    /// Name-based sibling routing index over the child groups.
    pub(crate) child_names: SiblingNameIndex,
    /// Parsed relative segments per `existence` constraint (index-aligned with
    /// [`WebTemplateNode::existence`]).
    pub(crate) existence: Vec<Option<Vec<PathSegment>>>,
    /// Parsed relative segments per `card_all` constraint (index-aligned with
    /// [`WebTemplateNode::card_all`]).
    pub(crate) cardinalities: Vec<Option<Vec<PathSegment>>>,
    /// Parsed relative segments per `closed_attributes` constraint (index-aligned
    /// with [`WebTemplateNode::closed_attributes`]).
    pub(crate) closed: Vec<Option<Vec<PathSegment>>>,
    /// Slot groups (grouped by slot path, first-seen order).
    pub(crate) slot_groups: Vec<SlotGroup>,
    /// Name-based sibling routing index over the slot groups.
    pub(crate) slot_names: SiblingNameIndex,
    /// Per-step id-only name fallback over every template path under this node
    /// ([`rmpath::NameFallback`]), so a step whose name conjunct a renamed
    /// instance does not match still reaches it when the name is redundant.
    pub(crate) name_fallback: rmpath::NameFallback,
}

/// A sibling group of `WebTemplate` children sharing one `aqlPath` (a single
/// node, or a set of polymorphic type alternatives).
#[derive(Debug, Clone)]
pub(crate) struct ChildGroup {
    /// Indices into [`WebTemplateNode::children`] of the group's members.
    pub(crate) members: Vec<usize>,
    /// The group's `aqlPath` parsed relative to the parent node's `aqlPath`; an
    /// empty vec means the child path is not a strict extension of the parent's
    /// (or equals it) — the walk skips such a group.
    pub(crate) segments: Vec<PathSegment>,
}

/// A group of hoisted-wrapper slot constraints sharing one absolute path.
#[derive(Debug, Clone)]
pub(crate) struct SlotGroup {
    /// The allowed RM types (first-seen order — the `join(", ")` message order).
    pub(crate) allowed: Vec<String>,
    /// The absolute slot path (the violation path the walk pushes).
    pub(crate) path: String,
    /// The slot path parsed relative to the node's `aqlPath` (empty = not an
    /// extension; the walk skips it).
    pub(crate) segments: Vec<PathSegment>,
}

impl NodeWalk {
    /// Build the walk plan for `node` from its (final, post-compaction) constraint
    /// vectors and children. Parses each constraint path once.
    #[expect(
        clippy::indexing_slicing,
        reason = "`gi` is either an index already recorded in the `child_idx`/`slot_idx` map or `next == len` immediately after the matching `push`, so both `child_groups[gi]` and `slot_groups[gi]` are in bounds by construction"
    )]
    pub(crate) fn build(node: &WebTemplateNode) -> Self {
        let aql = node.aql_path.as_str();

        // Child groups: iterate children in order, grouping by `aqlPath`.
        let mut child_groups: Vec<ChildGroup> = Vec::new();
        let mut child_idx: std::collections::HashMap<&str, usize> =
            std::collections::HashMap::new();
        for (i, child) in node.children.iter().enumerate() {
            let key = child.aql_path.as_str();
            let next = child_groups.len();
            let gi = *child_idx.entry(key).or_insert(next);
            if gi == next {
                let segments = key.strip_prefix(aql).map(rmpath::parse).unwrap_or_default();
                child_groups.push(ChildGroup {
                    members: Vec::new(),
                    segments,
                });
            }
            child_groups[gi].members.push(i);
        }
        let child_names =
            sibling_index_from_segments(child_groups.iter().map(|g| g.segments.as_slice()));

        // Slot groups: grouped by absolute slot path, first-seen order.
        let mut slot_groups: Vec<SlotGroup> = Vec::new();
        let mut slot_idx: std::collections::HashMap<&str, usize> = std::collections::HashMap::new();
        for slot in &node.slots {
            let key = slot.path.as_str();
            let next = slot_groups.len();
            let gi = *slot_idx.entry(key).or_insert(next);
            if gi == next {
                let segments = key.strip_prefix(aql).map(rmpath::parse).unwrap_or_default();
                slot_groups.push(SlotGroup {
                    allowed: Vec::new(),
                    path: slot.path.clone(),
                    segments,
                });
            }
            slot_groups[gi].allowed.push(slot.rm_type.clone());
        }
        let slot_names =
            sibling_index_from_segments(slot_groups.iter().map(|g| g.segments.as_slice()));
        let name_fallback = rmpath::name_fallback_from_segments(
            child_groups
                .iter()
                .map(|g| g.segments.as_slice())
                .chain(slot_groups.iter().map(|g| g.segments.as_slice())),
        );

        let rel = |p: &str| p.strip_prefix(aql).map(rmpath::parse);

        Self {
            child_groups,
            child_names,
            existence: node.existence.iter().map(|e| rel(&e.path)).collect(),
            cardinalities: node.card_all.iter().map(|c| rel(&c.path)).collect(),
            closed: node
                .closed_attributes
                .iter()
                .map(|c| rel(&c.path))
                .collect(),
            slot_groups,
            slot_names,
            name_fallback,
        }
    }
}

/// Build the name-based sibling routing index from a set of already-parsed
/// relative sibling paths (one per distinct child/slot `aqlPath`).
///
/// Per identity `(attribute, archetype_node_id)`, count the distinct sibling
/// paths carrying it and collect the explicit `name/value` constraints; keep only
/// identities carried by more than one sibling. A path with no predicate-bearing
/// segment, or no `archetype_node_id`, is skipped.
fn sibling_index_from_segments<'a>(
    groups: impl Iterator<Item = &'a [PathSegment]>,
) -> SiblingNameIndex {
    let mut acc: std::collections::HashMap<(String, String), (u32, Vec<String>)> =
        std::collections::HashMap::new();
    for segs in groups {
        let Some(id_seg) = segs.iter().rfind(|s| !s.predicate.is_empty()) else {
            continue;
        };
        let Some(id) = &id_seg.predicate.archetype_node_id else {
            continue;
        };
        let entry = acc
            .entry((id_seg.attribute.clone(), id.clone()))
            .or_default();
        entry.0 += 1;
        if let Some(name) = &id_seg.predicate.name_value
            && !entry.1.contains(name)
        {
            entry.1.push(name.clone());
        }
    }
    acc.into_iter()
        .filter(|(_, (count, _))| *count > 1)
        .map(|(k, (_, names))| (k, names))
        .collect()
}

/// Populate the archetype-conformance walk plan on `node` and, recursively, every
/// descendant — called once by [`crate::flat::webtemplate::builder::build_web_template`] on the
/// finalized tree so the validation walk never re-parses a template-static
/// constraint path.
pub(crate) fn prepare_walk(node: &mut WebTemplateNode) {
    let plan = NodeWalk::build(node);
    node.walk = Some(Box::new(plan));
    for child in &mut node.children {
        prepare_walk(child);
    }
}

#[derive(Default)]
struct Validator {
    out: Vec<ValidationMessage>,
    /// When set, existence/occurrences/cardinality **lower** limits are treated
    /// as zero (missing/empty is tolerated), realizing the RM common master06
    /// §"Incomplete Content" relaxation for a `553|incomplete|` commit: "in an
    /// `incomplete` commit, data may be missing, but it may not be wrong … all
    /// existence and cardinality lower limits set to zero". Upper limits, type
    /// conformance, and every leaf/value constraint (`RangeError`,
    /// `PatternError`, `CodedValue`, `WrongType`) are still enforced — only the
    /// "not enough / absent" violations are suppressed.
    relax_lower_bounds: bool,
    /// When set, the archetype-conformance walk records the terminology
    /// questions the matched nodes' constraint bindings raise into
    /// [`Self::bindings`] (see [`collect_constraint_binding_checks`]). Off for
    /// the ordinary validation passes, which decide nothing about bindings.
    collect_bindings: bool,
    /// The collected constraint-binding checks (empty unless
    /// [`Self::collect_bindings`]).
    bindings: Vec<ConstraintBindingCheck>,
}

/// Does the generated RM model declare an attribute named `attr` on ANY class?
///
/// Name-only by design: an OPT constrains attributes at several RM levels and
/// the walker does not carry the declaring class here, while the defect this
/// answers — a constraint on an attribute NO RM class declares — is fully
/// decided by the name.
fn rm_declares_attribute(attr: &str) -> bool {
    static DECLARED: std::sync::LazyLock<std::collections::BTreeSet<&str>> =
        std::sync::LazyLock::new(|| {
            openehr_rm::v1_2::model::classes()
                .flat_map(|c| c.attributes.iter().map(|a| a.name))
                .collect()
        });
    DECLARED.contains(attr)
}

/// Why a template constraint cannot be evaluated against any instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnenforceableReason {
    /// The constrained attribute is declared by no RM class, so no conformant
    /// instance can carry it as a stored member.
    ///
    /// Two shapes reach this in the deployed OPT 1.4 corpus: a constraint on a
    /// computed FUNCTION written as if it were stored (`EVENT.offset`,
    /// `DV_PROPORTION.is_integral` — RM
    /// `UML/classes/org.openehr.rm.data_structures.event.adoc` §Functions and
    /// `UML/classes/org.openehr.rm.data_types.dv_proportion.adoc`
    /// §Functions declare them as functions, not attributes), and the US
    /// spelling `null_flavor` for the RM's `null_flavour`.
    AttributeNotInRmModel,
}

/// A template constraint the archetype-conformance walk cannot evaluate.
///
/// Enforcing one would demand an instance member the canonical reader refuses,
/// so the walk skips it. The skip is reported rather than dropped: template
/// content that can never be checked is a property of the template a caller is
/// entitled to see. These are NOT validation failures — a conformant instance
/// is unaffected, and nothing here rejects a commit.
#[derive(Debug, Clone)]
pub struct UnenforceableConstraint {
    /// Absolute archetype path of the constrained attribute.
    pub path: String,
    /// The constrained attribute name.
    pub attribute: String,
    /// Why the walk cannot evaluate it.
    pub reason: UnenforceableReason,
}

/// Reports every existence constraint in `wt` that the conformance walk cannot
/// evaluate.
///
/// A template-level property: the answer depends only on the template, so it is
/// computed once per template rather than per commit. Pair it with
/// [`validate_archetype_conformance`], which skips exactly these constraints.
///
/// # Examples
///
/// ```no_run
/// # use openehr_sdt::flat::validation::unenforceable_existence_constraints;
/// # fn demo(wt: &openehr_sdt::flat::webtemplate::model::WebTemplate) {
/// for skipped in unenforceable_existence_constraints(wt) {
///     eprintln!("unenforceable at {}: {}", skipped.path, skipped.attribute);
/// }
/// # }
/// # let _ = demo;
/// ```
#[must_use]
pub fn unenforceable_existence_constraints(wt: &WebTemplate) -> Vec<UnenforceableConstraint> {
    let mut out = Vec::new();
    collect_unenforceable(&wt.tree, &mut out);
    out
}

/// Walks the node tree accumulating unenforceable existence constraints.
fn collect_unenforceable(node: &WebTemplateNode, out: &mut Vec<UnenforceableConstraint>) {
    for ex in &node.existence {
        let attribute = ex.path.rsplit('/').next().unwrap_or_default();
        if attribute.is_empty() || rm_declares_attribute(attribute) {
            continue;
        }
        out.push(UnenforceableConstraint {
            path: ex.path.clone(),
            attribute: attribute.to_owned(),
            reason: UnenforceableReason::AttributeNotInRmModel,
        });
    }
    for child in &node.children {
        collect_unenforceable(child, out);
    }
}

impl Validator {
    fn push(&mut self, path: impl Into<String>, message: impl Into<String>, kind: ValidationKind) {
        self.out.push(ValidationMessage {
            path: path.into(),
            message: message.into(),
            kind,
        });
    }

    // ── Pass 3: WebTemplate archetype-conformance walk ────────────────────────

    /// Visit an instance node matched to a `WebTemplate` node: check type
    /// conformance, leaf domain constraints, then descend into the `WebTemplate`
    /// children and container cardinalities.
    /// Record the terminology questions this matched node's constraint
    /// bindings raise about the instance's coded value. The `CODE_PHRASE` is
    /// read from the binding's `attr` (`defining_code` for a `DV_CODED_TEXT`);
    /// an empty `attr` means the node itself IS the `CODE_PHRASE`. A node the
    /// instance leaves uncoded raises no question — the binding constrains a
    /// value that is not there.
    fn collect_node_bindings(&mut self, instance: &Value, wt: &WebTemplateNode) {
        for binding in &wt.constraint_bindings {
            let code_phrase = if binding.attr.is_empty() {
                Some(instance)
            } else {
                instance.get(&binding.attr)
            };
            let Some(cp) = code_phrase else { continue };
            let Some(code) = cp.get("code_string").and_then(Value::as_str) else {
                continue;
            };
            if code.is_empty() {
                continue;
            }
            let instance_terminology = cp
                .get("terminology_id")
                .and_then(|t| t.get("value"))
                .and_then(Value::as_str)
                .unwrap_or_default();
            self.bindings.push(ConstraintBindingCheck {
                path: wt.aql_path.clone(),
                ac_code: binding.ac_code.clone(),
                binding_terminology: binding.terminology.clone(),
                query_uri: binding.query_uri.clone(),
                instance_terminology: instance_terminology.to_owned(),
                instance_code: code.to_owned(),
            });
        }
    }

    fn walk(&mut self, instance: &Value, wt: &WebTemplateNode) {
        if let Some(inst_type) = instance.get("_type").and_then(Value::as_str)
            && !subtype::conforms(inst_type, &wt.rm_type)
        {
            self.push(
                &wt.aql_path,
                format!(
                    "expected RM type conforming to {} but found {inst_type}",
                    wt.rm_type
                ),
                ValidationKind::WrongType,
            );
            // A wrong-typed node cannot be meaningfully checked further.
            return;
        }

        if !wt.inputs.is_empty() {
            leaf::check_inputs(self, instance, wt);
        }

        if self.collect_bindings && !wt.constraint_bindings.is_empty() {
            self.collect_node_bindings(instance, wt);
        }

        // The archetype-conformance walk is driven by the node's pre-parsed
        // [`NodeWalk`]: child sibling groups (polymorphic type choices resolved
        // together, never each flagged), the name-routing index, and the parsed
        // constraint paths, all computed ONCE at build time. A hand-built node
        // with no cached plan builds one on the fly (identical result), so every
        // check reads exactly one code path.
        let built;
        let plan: &NodeWalk = if let Some(p) = &wt.walk {
            p
        } else {
            built = NodeWalk::build(wt);
            &built
        };
        for group in &plan.child_groups {
            self.check_group(instance, wt, group, &plan.child_names, &plan.name_fallback);
        }

        self.check_cardinalities(instance, wt, plan);
        self.check_existence(instance, wt, plan);
        self.check_slots(instance, plan);
        self.check_closure(instance, wt, plan);
    }

    /// Closed-archetype walk. Under each constrained
    /// attribute this node records (an attribute with fixed archetype-node
    /// alternatives and/or open `ARCHETYPE_SLOT`s), an instance child bearing an
    /// `archetype_node_id` (i.e. a LOCATABLE — the archetyped-content
    /// discriminator; no RM metadata value carries one) must match a fixed
    /// sibling identity **or** an open slot; any other archetyped child is an
    /// "unexpected node". RM-permitted unconstrained metadata attributes and
    /// wholly-unconstrained attributes are never recorded, so stay open (closed-world capture
    /// rule 2). A rejected node is not descended into (the walk already skips it).
    ///
    /// NOTE: AOM 1.4 `valid_value`
    /// (`AM/docs/AOM1.4/master04-constraint_model_package.adoc` §`Valid_value`
    /// L60-62) is a positive-only cascade, silent on unmatched instance nodes;
    /// closed-world rejection follows the AOM2 direction + de-facto CDR behaviour
    /// and lands only behind the ECC zero-drift gate.
    fn check_closure(&mut self, instance: &Value, wt: &WebTemplateNode, plan: &NodeWalk) {
        for (ca, segs) in wt.closed_attributes.iter().zip(&plan.closed) {
            let Some(segments) = segs else {
                continue;
            };
            let Some((last, intermediate)) = segments.split_last() else {
                continue;
            };
            for container in
                &rmpath::navigate_matched(&[instance], intermediate, &plan.name_fallback)
            {
                let slot_counts = self.match_closed_children(container, ca, &last.attribute);
                self.check_slot_occurrences(ca, &last.attribute, &slot_counts);
            }
        }
    }

    /// Matches every child under one closed attribute against its fixed
    /// sibling alternatives and its slots, recording an "unexpected node" for
    /// each child neither admits, and returns the per-slot filler counts.
    ///
    /// A child with no `archetype_node_id` is not a LOCATABLE (RM metadata /
    /// PATHABLE value) and is not subject to sibling closure.
    ///
    /// NOTE: an unmatched archetype-rooted child is admitted where the
    /// attribute declares no ARCHETYPE_SLOT, OPT 1.4 flattening not
    /// enumerating the slot-fill universe; slots still gate.
    #[expect(
        clippy::indexing_slicing,
        reason = "`counts` is sized `ca.slots.len()` and the index is a `position` over those same slots, so it is in bounds by construction"
    )]
    fn match_closed_children(
        &mut self,
        container: &Value,
        ca: &crate::flat::webtemplate::model::WebTemplateClosedAttribute,
        attribute: &str,
    ) -> Vec<usize> {
        let mut counts = vec![0usize; ca.slots.len()];
        for child in children_under_attr(container, attribute) {
            let Some(nid) = child
                .get("archetype_node_id")
                .and_then(Value::as_str)
                .filter(|s| !s.is_empty())
            else {
                continue;
            };
            if ca.allowed_ids.iter().any(|a| a == nid) {
                continue; // Matches a fixed sibling alternative.
            }
            if ca.slots.is_empty() && openehr_rm::v1_2::paths::is_archetype_root_node_id(nid) {
                continue;
            }
            let ct = child.get("_type").and_then(Value::as_str).unwrap_or("");
            match ca.slots.iter().position(|s| slot_admits(s, ct, nid)) {
                Some(i) => counts[i] += 1,
                None => self.push(
                    &ca.path,
                    format!(
                        "unexpected node '{nid}' under '{attribute}': no matching archetype \
                         constraint or slot"
                    ),
                    ValidationKind::Unexpected,
                ),
            }
        }
        counts
    }

    /// Checks each slot's occurrences against the fillers matched to it.
    #[expect(
        clippy::indexing_slicing,
        reason = "`counts` is sized `ca.slots.len()` and the index is the `enumerate` index over those same slots, so it is in bounds by construction"
    )]
    fn check_slot_occurrences(
        &mut self,
        ca: &crate::flat::webtemplate::model::WebTemplateClosedAttribute,
        attribute: &str,
        counts: &[usize],
    ) {
        for (i, slot) in ca.slots.iter().enumerate() {
            let count = i32::try_from(counts[i]).unwrap_or(i32::MAX);
            if counts[i] == 0 {
                if slot.min >= 1 {
                    self.push(
                        &ca.path,
                        format!(
                            "mandatory archetype slot (occurrences {}..) under '{attribute}' \
                             has no filler",
                            slot.min
                        ),
                        ValidationKind::Required,
                    );
                }
            } else if slot.max != -1 && count > slot.max {
                self.push(
                    &ca.path,
                    format!(
                        "too many slot fillers under '{attribute}': found {}, expected at most {}",
                        counts[i], slot.max
                    ),
                    ValidationKind::Occurrences,
                );
            }
        }
    }

    /// Type-conformance check for wrapper constraints the compactor hoisted
    /// away (`ITEM_*` / `HISTORY` / single `EVENT` — AOM 1.4 type conformance;
    /// master16 §`ITEM_STRUCTURE`/§EVENT "Class not allowed"): an instance node
    /// matched at a recorded slot path must conform to one of the RM types the
    /// same-path slot alternatives allow. An abstract recorded type
    /// (`ITEM_STRUCTURE`, `EVENT`) admits every concrete subtype via
    /// [`subtype::conforms`].
    fn check_slots(&mut self, instance: &Value, plan: &NodeWalk) {
        for sg in &plan.slot_groups {
            let Some((last, intermediate)) = sg.segments.split_last() else {
                continue;
            };
            let containers =
                rmpath::navigate_matched(&[instance], intermediate, &plan.name_fallback);
            for container in &containers {
                for node in select_group_children(container, last, &plan.slot_names) {
                    let Some(it) = node.get("_type").and_then(Value::as_str) else {
                        continue;
                    };
                    if !sg.allowed.iter().any(|a| subtype::conforms(it, a)) {
                        self.push(
                            &sg.path,
                            format!(
                                "class {it} not allowed: the slot is constrained to [{}]",
                                sg.allowed.join(", ")
                            ),
                            ValidationKind::WrongType,
                        );
                    }
                }
            }
        }
    }

    /// AOM 1.4 `C_ATTRIBUTE.existence` check: for each mandatory plain
    /// RM attribute constrained on this node, verify the attribute *field* is
    /// present on the matched instance. Existence is distinct from occurrences
    /// (archetype-node-identified children) and cardinality (container
    /// membership) — it governs whether the attribute field is there at all — so
    /// this fills the gap for plain structural attributes the occurrence check
    /// deliberately skips. Only the lower bound (mandatory presence) is enforced;
    /// the upper bound is governed by RM single-valuedness / cardinality.
    fn check_existence(&mut self, instance: &Value, wt: &WebTemplateNode, plan: &NodeWalk) {
        // Existence is a lower-bound (mandatory-presence) constraint — relaxed
        // away for a `553|incomplete|` commit (master06 §"Incomplete Content").
        if self.relax_lower_bounds {
            return;
        }
        for (ex, segs) in wt.existence.iter().zip(&plan.existence) {
            if ex.min < 1 {
                continue;
            }
            let Some(segments) = segs else {
                continue;
            };
            let Some((last, intermediate)) = segments.split_last() else {
                continue;
            };
            // An existence constraint on an attribute no RM class declares cannot
            // be satisfied by a conformant instance — enforcing it would demand a
            // member the canonical reader refuses. Skipped here and REPORTED by
            // `unenforceable_existence_constraints`, which shares this predicate;
            // see `UnenforceableReason::AttributeNotInRmModel` for the shapes and
            // their RM citations.
            if !rm_declares_attribute(&last.attribute) {
                continue;
            }
            // Navigate the intermediate segments to the container node(s).
            let containers =
                rmpath::navigate_matched(&[instance], intermediate, &plan.name_fallback);
            for container in &containers {
                if attr_absent(container, &last.attribute) {
                    self.push(
                        ex.path.clone(),
                        format!(
                            "mandatory attribute '{}' is missing (existence {}..)",
                            last.attribute, ex.min
                        ),
                        ValidationKind::Required,
                    );
                }
            }
        }
    }

    /// Resolve a group of `WebTemplate` children sharing an aql path (a single
    /// node, or a set of polymorphic type alternatives) against the instance:
    /// occurrence-check the group, then recurse into each matched instance,
    /// routing it to the conforming alternative.
    #[expect(
        clippy::indexing_slicing,
        reason = "`group.members` holds indices into `wt_parent.children` recorded by `NodeWalk::build` over that same child vector, `members` is non-empty for any recorded group (so `members[0]` exists), and `identity_idx` is an `rposition`/`len() - 1` over `segments` (returned early when empty), so every index and range here is in bounds by construction"
    )]
    fn check_group(
        &mut self,
        parent: &Value,
        wt_parent: &WebTemplateNode,
        group: &ChildGroup,
        names: &SiblingNameIndex,
        name_fallback: &rmpath::NameFallback,
    ) {
        // Segments were parsed once at build time; an empty slice means the child
        // path is not a strict extension of the parent's (or equals it) — the
        // `strip_prefix … else return` / `segments.is_empty()` early-outs, both
        // preserved here.
        let segments = &group.segments;
        if segments.is_empty() {
            return;
        }
        let members: Vec<&WebTemplateNode> = group
            .members
            .iter()
            .map(|&i| &wt_parent.children[i])
            .collect();
        let first = members[0];
        let identity_idx = identity_index(segments);
        // Only the TERMINAL-identity case reads `first.rm_type`: with a trailing
        // plain attribute the identity node is the archetyped `LOCATABLE`
        // intermediate.
        let raw_id_seg = &segments[identity_idx];
        let trailing = &segments[identity_idx + 1..];
        let identity_is_locatable = !trailing.is_empty() || is_locatable(&first.rm_type);
        let structural_id_seg = structural_identity(raw_id_seg, identity_is_locatable);
        let id_seg: &PathSegment = structural_id_seg.as_ref().unwrap_or(raw_id_seg);

        // Navigate the intermediate segments to the container node(s).
        let containers =
            rmpath::navigate_matched(&[parent], &segments[..identity_idx], name_fallback);

        // Occurrences are an *archetype-node* constraint: only checked when the
        // matched node is identified by an archetype-node predicate (at-code /
        // archetype id) on a `LOCATABLE` node that can bear one. Plain RM
        // structural attributes (`context`, `value`, `ism_transition`, …),
        // non-`LOCATABLE` nodes (`EVENT_CONTEXT`, matched structurally above) and
        // `in_context` nodes are governed by RM cardinality/invariants, not
        // archetype occurrences, so they are not occurrence-checked here.
        let occ_applies = identity_is_locatable
            && id_seg.predicate.archetype_node_id.is_some()
            && !members.iter().any(|c| c.in_context == Some(true));
        let (group_min, group_max) = group_occurrences(&members);

        for container in &containers {
            let matched = select_group_children(container, id_seg, names);
            if occ_applies {
                self.emit_occurrences(&first.aql_path, group_min, group_max, matched.len());
            }
            for node in matched {
                self.walk_group_targets(node, trailing, &members, name_fallback);
            }
        }
    }

    /// Walks every target the trailing segments reach under one matched node.
    ///
    /// A single-member group walks its one template node; a choice group is
    /// dispatched to [`Self::visit_choice`].
    fn walk_group_targets(
        &mut self,
        node: &Value,
        trailing: &[PathSegment],
        members: &[&WebTemplateNode],
        name_fallback: &rmpath::NameFallback,
    ) {
        for target in rmpath::navigate_matched(&[node], trailing, name_fallback) {
            match members {
                [only] => self.walk(target, only),
                _ => self.visit_choice(target, members),
            }
        }
    }

    /// Route a matched instance node to the type-choice alternative it conforms
    /// to; if it conforms to none, report a single `WrongType`.
    #[expect(
        clippy::indexing_slicing,
        reason = "`visit_choice` is only reached from `check_group` with `members.len() > 1`, so `group[0]` exists"
    )]
    fn visit_choice(&mut self, target: &Value, group: &[&WebTemplateNode]) {
        match target.get("_type").and_then(Value::as_str) {
            Some(it) => {
                if let Some(alt) = group.iter().find(|c| subtype::conforms(it, &c.rm_type)) {
                    self.walk(target, alt);
                } else {
                    let expected: Vec<&str> = group.iter().map(|c| c.rm_type.as_str()).collect();
                    self.push(
                        &group[0].aql_path,
                        format!(
                            "value type {it} conforms to none of the permitted types [{}]",
                            expected.join(", ")
                        ),
                        ValidationKind::WrongType,
                    );
                }
            }
            // No `_type`: recurse against the first alternative; the RM-invariant
            // pass covers structural issues.
            None => self.walk(target, group[0]),
        }
    }

    /// Emit occurrence violations for a matched-node `count` against `[min, max]`
    /// (`max == -1` is unbounded).
    // NOTE: evaluation uses the WebTemplate's flattened `(min, max)` integers
    // (`-1` unbounded) — equivalent to `Multiplicity_interval.has(count)` (BASE
    // `…foundation_types.multiplicity_interval.adoc`) for OPT 1.4's closed bounds.
    fn emit_occurrences(&mut self, aql_path: &str, min: i32, max: i32, count: usize) {
        let count_i = i32::try_from(count).unwrap_or(i32::MAX);
        // The lower-bound occurrences checks (absent / too-few) are relaxed away
        // for a `553|incomplete|` commit; the upper bound still applies
        // (master06 §"Incomplete Content": lower limits set to zero).
        if !self.relax_lower_bounds {
            if count == 0 {
                if min >= 1 {
                    self.push(
                        aql_path,
                        format!("mandatory node is missing (occurrences {min}..)"),
                        ValidationKind::Required,
                    );
                }
            } else if count_i < min {
                self.push(
                    aql_path,
                    format!("too few occurrences: found {count}, expected at least {min}"),
                    ValidationKind::Occurrences,
                );
            }
        }
        if max != -1 && count_i > max {
            self.push(
                aql_path,
                format!("too many occurrences: found {count}, expected at most {max}"),
                ValidationKind::Occurrences,
            );
        }
    }

    /// Container-cardinality check: for each constraining cardinality on this
    /// node, count the children under the constrained attribute path and compare
    /// to `min`/`max`. Iterates [`WebTemplateNode::card_all`] — the full AOM 1.4
    /// §cardinality set (`0..1`/`1..1`/`1..*` included), a superset of the
    /// serialized `cardinalities` (which the metadata document filters).
    fn check_cardinalities(&mut self, instance: &Value, wt: &WebTemplateNode, plan: &NodeWalk) {
        for (card, segs) in wt.card_all.iter().zip(&plan.cardinalities) {
            let Some(segments) = segs else {
                continue;
            };
            let Some((last, intermediate)) = segments.split_last() else {
                continue;
            };
            // Navigate all but the last segment to the container, then count the
            // last attribute's children (cardinality is over the whole set).
            //
            // AOM 1.4 §cardinality constrains the container's membership only WHEN
            // the attribute is present; absence is C_ATTRIBUTE.existence's business
            // (the vendored Multi_list template pairs `content` cardinality 1..*
            // with existence 0..1), so an absent or null attribute is no cardinality
            // violation — and the RM list invariants forbid a present-empty `[]`.
            let containers =
                rmpath::navigate_matched(&[instance], intermediate, &plan.name_fallback);
            for container in &containers {
                if matches!(container.get(&last.attribute), None | Some(Value::Null)) {
                    continue;
                }
                let count =
                    i32::try_from(openehr_rm::v1_2::paths::select_children(container, last).len())
                        .unwrap_or(i32::MAX);
                let min = card.min.unwrap_or(0).max(0);
                // The cardinality lower bound is relaxed away for a
                // `553|incomplete|` commit (master06 §"Incomplete Content": "all
                // existence and cardinality lower limits set to zero"); the upper
                // bound still applies.
                if count < min && !self.relax_lower_bounds {
                    self.push(
                        card.path.clone(),
                        format!("cardinality violated: {count} children, expected at least {min}"),
                        ValidationKind::Cardinality,
                    );
                }
                if card.max != -1 && count > card.max {
                    self.push(
                        card.path.clone(),
                        format!(
                            "cardinality violated: {count} children, expected at most {}",
                            card.max
                        ),
                        ValidationKind::Cardinality,
                    );
                }
            }
        }
    }
}

// ── path navigation ───────────────────────────────────────────────────────────
//
// RM-path parsing (`[atNNNN]` / `[archetype-id]` / `[atNNNN,'name']` predicates)
// and per-step navigation over the canonical-JSON tree are the single
// implementation in [`openehr_rm::v1_2::paths`], reached here via [`crate::flat::rmpath`]
// (`parse` / `navigate` / `select_children`). Only the checks below —
// attribute-presence for the existence rule and RM instance-path
// normalisation — are validation-specific.

/// Select the instance children a WebTemplate sibling group claims from
/// `container`, applying the name discriminator for same-`archetype_node_id`
/// siblings so an instance is routed to exactly the sibling whose name
/// constraint it satisfies (RM common `master03-archetyped_package.adoc`
/// §"The `LOCATABLE` class": the runtime `name` distinguishes sibling nodes
/// that share an `archetype_node_id`; BASE
/// `architecture_overview/master11-paths.adoc` §"Using a Name-based Predicate").
///
/// `names` is the [`SiblingNameIndex`] computed once per parent from the sibling
/// paths. Three cases on the group's identity segment `id_seg`:
///
/// * **name-qualified, name-differentiated** (`Some` name, identity in `names`):
///   strict `(archetype_node_id, name)` match with the id-only fallback OFF —
///   the sibling set relies on the name, so widening to id-only would claim a
///   sibling's instances.
/// * **unqualified, name-differentiated** (no name, identity in `names`): match
///   by `archetype_node_id` minus the instances the name-qualified siblings
///   claim ([`rmpath::select_children_excluding_names`]) — the residual arm.
/// * **not name-differentiated** (identity absent from `names`): strict match
///   with the id-only fallback ON, tolerating a runtime-renamed instance when
///   the archetype does not constrain the name (master03 §"The `LOCATABLE`
///   class" L35).
fn select_group_children<'a>(
    container: &'a Value,
    id_seg: &PathSegment,
    names: &SiblingNameIndex,
) -> Vec<&'a Value> {
    let claimed = id_seg
        .predicate
        .archetype_node_id
        .as_ref()
        .and_then(|id| names.get(&(id_seg.attribute.clone(), id.clone())));
    match (id_seg.predicate.name_value.as_ref(), claimed) {
        (Some(_), Some(_)) => rmpath::select_children_matched(container, id_seg, false),
        (None, Some(claimed)) => {
            rmpath::select_children_excluding_names(container, id_seg, claimed)
        }
        (_, None) => rmpath::select_children_matched(container, id_seg, true),
    }
}

/// The index of the group's identity segment.
///
/// That is the last segment carrying a predicate; if none carries one, the last
/// segment (a plain single-valued attribute).
fn identity_index(segments: &[PathSegment]) -> usize {
    segments
        .iter()
        .rposition(|s| !s.predicate.is_empty())
        .unwrap_or(segments.len() - 1)
}

/// The predicate-stripped identity segment for a structurally-matched node.
///
/// A node whose RM type does NOT inherit `LOCATABLE` carries no
/// `archetype_node_id` in canonical JSON (only `LOCATABLE` adds it — RM common
/// `UML/classes/org.openehr.rm.common.locatable.adoc`; `EVENT_CONTEXT` inherits
/// `PATHABLE` directly — RM
/// `UML/classes/org.openehr.rm.composition.event_context.adoc` §Inherit), so it
/// is matched by attribute position instead. [`None`] means the raw segment
/// applies unchanged.
fn structural_identity(
    raw_id_seg: &PathSegment,
    identity_is_locatable: bool,
) -> Option<PathSegment> {
    if identity_is_locatable || raw_id_seg.predicate.is_empty() {
        return None;
    }
    let mut stripped = raw_id_seg.clone();
    stripped.predicate = openehr_rm::v1_2::paths::Predicate::default();
    Some(stripped)
}

/// The group's combined occurrence bounds: the loosest of its members'.
fn group_occurrences(members: &[&WebTemplateNode]) -> (i32, i32) {
    let min = members
        .iter()
        .filter_map(|c| c.min)
        .min()
        .unwrap_or(0)
        .max(0);
    let max = if members.iter().any(|c| c.max == -1) {
        -1
    } else {
        members.iter().map(|c| c.max).max().unwrap_or(-1)
    };
    (min, max)
}

/// Whether an RM type inherits `LOCATABLE` and therefore carries an
/// `archetype_node_id` (and `name`) in canonical JSON (RM common
/// `UML/classes/org.openehr.rm.common.locatable.adoc`). Backed by the
/// BMM-generated RM inheritance graph ([`openehr_rm::v1_2::model::is_a`]). A type the
/// model does not recognise is treated as `LOCATABLE` (the historic default —
/// stay strict rather than silently widen matching for an unknown type). Generic
/// arguments are stripped first.
fn is_locatable(rm_type: &str) -> bool {
    let base = rm_type.split('<').next().unwrap_or(rm_type).trim();
    openehr_rm::v1_2::model::class(base).is_none()
        || openehr_rm::v1_2::model::is_a(base, "LOCATABLE")
}

/// The instance child objects directly under `attr` (array elements or a single
/// object), skipping non-object values — the sibling set for closed-world.
fn children_under_attr<'a>(node: &'a Value, attr: &str) -> Vec<&'a Value> {
    match node.get(attr) {
        Some(Value::Array(a)) => a.iter().filter(|v| v.is_object()).collect(),
        Some(v @ Value::Object(_)) => vec![v],
        _ => Vec::new(),
    }
}

/// Whether an open `ARCHETYPE_SLOT` admits a filler of RM type `child_type` and
/// archetype id `archetype_id` (AOM 1.4 `ARCHETYPE_SLOT`): the type must conform
/// to the slot's `rm_type`, the id must match at least one `includes` regex (an
/// empty `includes` = open to the type), and match no `excludes` regex. A blanket
/// match-all (`.*`) exclude is ignored when `includes` is non-empty — the ADL 1.4
/// closed-slot idiom (AOM 1.4 has no `is_closed`; NOTE: includes then win,
/// matching de-facto CDR behaviour).
fn slot_admits(slot: &WebTemplateArchetypeSlot, child_type: &str, archetype_id: &str) -> bool {
    if !slot.rm_type.is_empty() && !subtype::conforms(child_type, &slot.rm_type) {
        return false;
    }
    let include_ok = slot.includes.is_empty()
        || slot
            .includes
            .iter()
            .any(|p| leaf::matches_pattern(p, archetype_id));
    if !include_ok {
        return false;
    }
    for ex in &slot.excludes {
        if !slot.includes.is_empty() && matches!(ex.trim(), ".*" | ".+") {
            continue; // closed-slot idiom: a specific includes list wins.
        }
        if leaf::matches_pattern(ex, archetype_id) {
            return false;
        }
    }
    true
}

/// Whether an RM attribute field is absent (missing, JSON `null`, or an empty
/// array) on a node — the negation of "the attribute is present" for the
/// existence check.
fn attr_absent(node: &Value, attr: &str) -> bool {
    match node.get(attr) {
        None | Some(Value::Null) => true,
        Some(Value::Array(a)) => a.is_empty(),
        Some(_) => false,
    }
}

#[cfg(test)]
mod tests {
    //! Per-rule unit tests for the composition validator, built on hand-shaped
    //! `WebTemplate` nodes + minimal instances (no OPT parsing) so each rule is
    //! exercised in isolation through the private [`Validator`] walk. End-to-end
    //! corpus + public-seam tests live in `tests/validation.rs`.

    use serde_json::{Value, json};

    use super::*;
    use crate::flat::webtemplate::model::{
        WebTemplateCardinality, WebTemplateClosedAttribute, WebTemplateCodeList,
        WebTemplateCodedValue, WebTemplateExistence, WebTemplateInput, WebTemplateInputType,
        WebTemplateRange, WebTemplateSlot, WebTemplateValidation,
    };

    fn node(rm: &str, path: &str) -> WebTemplateNode {
        WebTemplateNode::new(rm.to_owned(), path.to_owned())
    }

    /// Run only the `WebTemplate` (archetype-conformance) pass for a matched root.
    fn walk_only(instance: &Value, root: &WebTemplateNode) -> Vec<ValidationMessage> {
        let mut v = Validator::default();
        v.walk(instance, root);
        v.out
    }

    fn kinds(msgs: &[ValidationMessage]) -> Vec<ValidationKind> {
        msgs.iter().map(|m| m.kind).collect()
    }

    // ── occurrences ──────────────────────────────────────────────────────────

    #[test]
    fn occurrences_too_few() {
        let mut root = node("COMPOSITION", "");
        let mut sec = node("SECTION", "/content[at0001]");
        sec.min = Some(2);
        sec.max = 5;
        root.children = vec![sec];

        let inst = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x",
            "content": [{"_type": "SECTION", "archetype_node_id": "at0001",
                         "name": {"_type": "DV_TEXT", "value": "s"}}]
        });
        let msgs = walk_only(&inst, &root);
        assert!(
            kinds(&msgs).contains(&ValidationKind::Occurrences),
            "expected Occurrences (too few), got {msgs:?}"
        );
    }

    #[test]
    fn occurrences_too_many() {
        let mut root = node("COMPOSITION", "");
        let mut sec = node("SECTION", "/content[at0001]");
        sec.min = Some(0);
        sec.max = 1;
        root.children = vec![sec];

        let one = json!({"_type": "SECTION", "archetype_node_id": "at0001",
                         "name": {"_type": "DV_TEXT", "value": "s"}});
        let inst = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x",
            "content": [one.clone(), one.clone(), one]
        });
        let msgs = walk_only(&inst, &root);
        assert!(
            msgs.iter()
                .any(|m| m.kind == ValidationKind::Occurrences && m.message.contains("too many")),
            "expected Occurrences (too many), got {msgs:?}"
        );
    }

    #[test]
    fn occurrences_required_missing() {
        let mut root = node("COMPOSITION", "");
        let mut sec = node("SECTION", "/content[at0001]");
        sec.min = Some(1);
        sec.max = 1;
        root.children = vec![sec];

        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": []});
        let msgs = walk_only(&inst, &root);
        assert!(
            kinds(&msgs).contains(&ValidationKind::Required),
            "expected Required, got {msgs:?}"
        );
    }

    // ── cardinality ────────────────────────────────────────────────────────────

    #[test]
    fn cardinality_violation() {
        let mut root = node("COMPOSITION", "");
        root.card_all = vec![WebTemplateCardinality {
            min: Some(1),
            max: 2,
            ids: None,
            path: "/content".to_owned(),
        }];
        let entry = json!({"_type": "OBSERVATION", "archetype_node_id": "a"});
        let inst = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x",
            "content": [entry.clone(), entry.clone(), entry]
        });
        let msgs = walk_only(&inst, &root);
        assert!(
            kinds(&msgs).contains(&ValidationKind::Cardinality),
            "expected Cardinality (>max), got {msgs:?}"
        );
    }

    // ── incomplete-lifecycle (553) relaxation ────────────────────────────────
    // RM common master06 §"Incomplete Content": existence/occurrences/cardinality
    // lower limits treated as zero; upper limits and value/type constraints stay.

    /// Run the archetype-conformance pass with the `553|incomplete|` relaxation.
    fn walk_incomplete(instance: &Value, root: &WebTemplateNode) -> Vec<ValidationMessage> {
        let mut v = Validator {
            relax_lower_bounds: true,
            ..Validator::default()
        };
        v.walk(instance, root);
        v.out
    }

    #[test]
    fn incomplete_suppresses_required_missing_occurrences() {
        let mut root = node("COMPOSITION", "");
        let mut sec = node("SECTION", "/content[at0001]");
        sec.min = Some(1);
        sec.max = 1;
        root.children = vec![sec];

        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": []});
        // Strict: a mandatory node is missing.
        assert!(kinds(&walk_only(&inst, &root)).contains(&ValidationKind::Required));
        // Incomplete: the lower bound is zeroed, so nothing is emitted.
        assert!(
            walk_incomplete(&inst, &root).is_empty(),
            "incomplete commit must tolerate a missing mandatory node"
        );
    }

    #[test]
    fn incomplete_suppresses_too_few_but_keeps_too_many() {
        // too few (min 2, one present) → suppressed under incomplete.
        let mut root = node("COMPOSITION", "");
        let mut sec = node("SECTION", "/content[at0001]");
        sec.min = Some(2);
        sec.max = 3;
        root.children = vec![sec];
        let one = json!({"_type": "SECTION", "archetype_node_id": "at0001",
                         "name": {"_type": "DV_TEXT", "value": "s"}});
        let too_few = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x", "content": [one.clone()]
        });
        assert!(
            walk_incomplete(&too_few, &root).is_empty(),
            "incomplete commit must tolerate too-few occurrences"
        );

        // too many (three present, max 3 ok; four exceeds) → still enforced (upper
        // bound is not relaxed: missing is tolerated, wrong is not).
        let too_many = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x",
            "content": [one.clone(), one.clone(), one.clone(), one]
        });
        assert!(
            walk_incomplete(&too_many, &root)
                .iter()
                .any(|m| m.kind == ValidationKind::Occurrences && m.message.contains("too many")),
            "incomplete commit must still reject too-many occurrences"
        );
    }

    #[test]
    fn incomplete_suppresses_cardinality_lower_but_keeps_upper() {
        // Lower-bound cardinality violation → suppressed.
        let mut low = node("COMPOSITION", "");
        low.card_all = vec![WebTemplateCardinality {
            min: Some(2),
            max: -1,
            ids: None,
            path: "/content".to_owned(),
        }];
        let entry = json!({"_type": "OBSERVATION", "archetype_node_id": "a"});
        let one_child = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x", "content": [entry.clone()]
        });
        assert!(
            walk_incomplete(&one_child, &low).is_empty(),
            "incomplete commit must tolerate a below-minimum container"
        );

        // Upper-bound cardinality violation → still enforced.
        let mut high = node("COMPOSITION", "");
        high.card_all = vec![WebTemplateCardinality {
            min: Some(1),
            max: 2,
            ids: None,
            path: "/content".to_owned(),
        }];
        let three = json!({
            "_type": "COMPOSITION", "archetype_node_id": "x",
            "content": [entry.clone(), entry.clone(), entry]
        });
        assert!(
            kinds(&walk_incomplete(&three, &high)).contains(&ValidationKind::Cardinality),
            "incomplete commit must still reject an over-maximum container"
        );
    }

    #[test]
    fn incomplete_suppresses_existence() {
        let mut root = node("COMPOSITION", "");
        root.existence = vec![WebTemplateExistence {
            min: 1,
            max: 1,
            path: "/context".to_owned(),
        }];
        // The mandatory `context` field is absent.
        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x"});
        assert!(kinds(&walk_only(&inst, &root)).contains(&ValidationKind::Required));
        assert!(
            walk_incomplete(&inst, &root).is_empty(),
            "incomplete commit must tolerate a missing mandatory attribute (existence)"
        );
    }

    // ── numeric range ────────────────────────────────────────────────────────

    fn count_node_range(min: i64, max: i64) -> WebTemplateNode {
        let mut n = node("DV_COUNT", "/count");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Integer, None);
        input.validation = Some(WebTemplateValidation {
            range: Some(WebTemplateRange {
                min_op: Some(">=".to_owned()),
                min: Some(Value::from(min)),
                max_op: Some("<=".to_owned()),
                max: Some(Value::from(max)),
            }),
            ..Default::default()
        });
        n.inputs = vec![input];
        n
    }

    #[test]
    fn range_error_out_of_bounds() {
        let n = count_node_range(0, 10);
        let inst = json!({"_type": "DV_COUNT", "magnitude": 42});
        let msgs = walk_only(&inst, &n);
        assert_eq!(kinds(&msgs), vec![ValidationKind::RangeError], "{msgs:?}");
    }

    #[test]
    fn range_ok_in_bounds() {
        let n = count_node_range(0, 10);
        let inst = json!({"_type": "DV_COUNT", "magnitude": 5});
        assert_eq!(walk_only(&inst, &n), Vec::<ValidationMessage>::new());
    }

    // ── DV_MULTIMEDIA.size (C_INTEGER list / range) ──────────────────────────
    // RM `data_types` §DV_MULTIMEDIA (`size: Integer`); AOM 1.4
    // `master04-constraint_model_package.adoc` §C_INTEGER (list + range).

    #[test]
    fn multimedia_size_out_of_range_reported() {
        let mut n = node("DV_MULTIMEDIA", "/media");
        n.inputs = vec![WebTemplateInput::new(WebTemplateInputType::Text, None)];
        n.numeric_ranges = vec![(
            "size".to_owned(),
            WebTemplateRange {
                min_op: Some(">=".to_owned()),
                min: Some(Value::from(200)),
                max_op: Some("<=".to_owned()),
                max: Some(Value::from(1000)),
            },
        )];
        let bad = json!({"_type": "DV_MULTIMEDIA", "uri": "http://x", "size": 123});
        assert_eq!(
            kinds(&walk_only(&bad, &n)),
            vec![ValidationKind::RangeError]
        );
        let good = json!({"_type": "DV_MULTIMEDIA", "uri": "http://x", "size": 500});
        assert_eq!(walk_only(&good, &n), Vec::<ValidationMessage>::new());
    }

    #[test]
    fn multimedia_size_not_in_list_reported() {
        let mut n = node("DV_MULTIMEDIA", "/media");
        n.inputs = vec![WebTemplateInput::new(WebTemplateInputType::Text, None)];
        n.numeric_lists = vec![("size".to_owned(), vec![10.0, 100.0, 1000.0])];
        let bad = json!({"_type": "DV_MULTIMEDIA", "uri": "http://x", "size": 123});
        assert_eq!(
            kinds(&walk_only(&bad, &n)),
            vec![ValidationKind::CodedValue]
        );
        let good = json!({"_type": "DV_MULTIMEDIA", "uri": "http://x", "size": 100});
        assert_eq!(walk_only(&good, &n), Vec::<ValidationMessage>::new());
    }

    // ── DV_IDENTIFIER mandatory (existence 1..1) constrained sub-attribute ────
    // RM `data_types` §DV_IDENTIFIER; AOM 1.4 §existence + §C_STRING. An OPT that
    // constrains and mandates `issuer` rejects a value that omits it (the `id`
    // absence is caught separately by the RM invariant).

    #[test]
    fn dv_identifier_mandatory_issuer_enforced() {
        let mut n = node("DV_IDENTIFIER", "/value");
        let mut issuer = WebTemplateInput::new(WebTemplateInputType::Text, Some("issuer"));
        issuer.list = vec![WebTemplateCodedValue::new("XYZ", Some("XYZ".to_owned()))];
        issuer.list_open = Some(false);
        n.inputs = vec![
            WebTemplateInput::new(WebTemplateInputType::Text, Some("id")),
            issuer,
        ];
        n.existence = vec![WebTemplateExistence {
            min: 1,
            max: 1,
            path: "/value/issuer".to_owned(),
        }];
        // issuer absent → Required.
        let absent = json!({"_type": "DV_IDENTIFIER", "id": "x"});
        assert!(kinds(&walk_only(&absent, &n)).contains(&ValidationKind::Required));
        // issuer present but not in the list → CodedValue.
        let wrong = json!({"_type": "DV_IDENTIFIER", "id": "x", "issuer": "ABC"});
        assert!(kinds(&walk_only(&wrong, &n)).contains(&ValidationKind::CodedValue));
        // issuer present and conforming → clean.
        let ok = json!({"_type": "DV_IDENTIFIER", "id": "x", "issuer": "XYZ"});
        assert_eq!(walk_only(&ok, &n), Vec::<ValidationMessage>::new());
    }

    // ── DV_SCALE generic (C_REAL value list + symbol C_CODE_PHRASE) ───────────
    // AOM 1.4 has no C_DV_SCALE, so DV_SCALE constrains `symbol.defining_code`
    // as a C_CODE_PHRASE code_list (AOM 1.4 §C_CODE_PHRASE; RM §DV_SCALE): a
    // symbol not in the list is rejected, with no (symbol, value) pair check.

    #[test]
    fn dv_scale_generic_symbol_membership_enforced() {
        let mut n = node("DV_SCALE", "/value");
        let mut input = WebTemplateInput::new(WebTemplateInputType::CodedText, None);
        // Generic form: coded symbols carry no scale/ordinal number.
        input.list = vec![
            WebTemplateCodedValue::new("at0005", Some("mild".to_owned())),
            WebTemplateCodedValue::new("at0006", Some("severe".to_owned())),
        ];
        n.inputs = vec![input];
        n.numeric_lists = vec![("value".to_owned(), vec![1.5, 2.4])];
        // Symbol not in the code list (value in the real list) → CodedValue.
        let bad = json!({
            "_type": "DV_SCALE", "value": 1.5,
            "symbol": {"_type": "DV_CODED_TEXT", "value": "?",
                "defining_code": {"_type": "CODE_PHRASE",
                    "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "local"},
                    "code_string": "at0666"}}
        });
        assert!(kinds(&walk_only(&bad, &n)).contains(&ValidationKind::CodedValue));
        // Symbol in the list, value in the list → clean (no spurious pair check).
        let ok = json!({
            "_type": "DV_SCALE", "value": 1.5,
            "symbol": {"_type": "DV_CODED_TEXT", "value": "mild",
                "defining_code": {"_type": "CODE_PHRASE",
                    "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "local"},
                    "code_string": "at0005"}}
        });
        assert!(walk_only(&ok, &n).is_empty(), "{:?}", walk_only(&ok, &n));
    }

    // ── DV_TIME partial value vs C_TIME pattern (over-rejection fix) ──────────
    // ADL 1.4 `master05-cadl.adoc` §"Date, Time and Date/Time" Patterns: `?` =
    // optional field, `X` = disallowed field; an hour-only value "10" satisfies
    // both "HH:??:??" and "HH:XX:XX".

    fn time_node(pattern: &str) -> WebTemplateNode {
        let mut n = node("DV_TIME", "/time");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Time, None);
        input.validation = Some(WebTemplateValidation {
            pattern: Some(pattern.to_owned()),
            ..Default::default()
        });
        n.inputs = vec![input];
        n
    }

    #[test]
    fn dv_time_hour_only_accepts_optional_and_prohibited_patterns() {
        for pattern in ["HH:??:??", "HH:XX:XX"] {
            let n = time_node(pattern);
            let inst = json!({"_type": "DV_TIME", "value": "10"});
            assert!(
                walk_only(&inst, &n).is_empty(),
                "hour-only \"10\" must satisfy pattern {pattern}: {:?}",
                walk_only(&inst, &n)
            );
        }
    }

    // ── string pattern ─────────────────────────────────────────────────────────

    #[test]
    fn pattern_error() {
        let mut n = node("DV_TEXT", "/text");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Text, None);
        input.validation = Some(WebTemplateValidation {
            pattern: Some("[A-Z]+".to_owned()),
            ..Default::default()
        });
        n.inputs = vec![input];

        let bad = json!({"_type": "DV_TEXT", "value": "abc"});
        assert_eq!(
            kinds(&walk_only(&bad, &n)),
            vec![ValidationKind::PatternError]
        );
        let good = json!({"_type": "DV_TEXT", "value": "ABC"});
        assert_eq!(walk_only(&good, &n), Vec::<ValidationMessage>::new());
    }

    // ── coded value membership ─────────────────────────────────────────────────

    fn coded_node(codes: &[&str]) -> WebTemplateNode {
        let mut n = node("DV_CODED_TEXT", "/coded");
        let mut input = WebTemplateInput::new(WebTemplateInputType::CodedText, Some("code"));
        input.list = codes
            .iter()
            .map(|c| WebTemplateCodedValue::new(*c, Some((*c).to_owned())))
            .collect();
        n.inputs = vec![input];
        n
    }

    #[test]
    fn coded_value_not_in_list() {
        let n = coded_node(&["at0001", "at0002"]);
        let bad = json!({
            "_type": "DV_CODED_TEXT", "value": "x",
            "defining_code": {"_type": "CODE_PHRASE",
                "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "local"},
                "code_string": "at0099"}
        });
        assert_eq!(
            kinds(&walk_only(&bad, &n)),
            vec![ValidationKind::CodedValue]
        );

        let good = json!({
            "_type": "DV_CODED_TEXT", "value": "x",
            "defining_code": {"_type": "CODE_PHRASE",
                "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "local"},
                "code_string": "at0001"}
        });
        assert_eq!(walk_only(&good, &n), Vec::<ValidationMessage>::new());
    }

    #[test]
    fn coded_value_external_terminology_is_skipped() {
        // A SNOMED code is not validated against the archetype's internal list.
        let n = coded_node(&["at0001"]);
        let external = json!({
            "_type": "DV_CODED_TEXT", "value": "x",
            "defining_code": {"_type": "CODE_PHRASE",
                "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "SNOMED-CT"},
                "code_string": "999"}
        });
        assert_eq!(walk_only(&external, &n), Vec::<ValidationMessage>::new());
    }

    // ── type conformance ───────────────────────────────────────────────────────

    #[test]
    fn wrong_type_reported() {
        let n = node("DV_QUANTITY", "/q");
        let inst = json!({"_type": "DV_TEXT", "value": "x"});
        assert_eq!(
            kinds(&walk_only(&inst, &n)),
            vec![ValidationKind::WrongType]
        );
    }

    #[test]
    fn coded_text_in_text_slot_conforms() {
        // DV_CODED_TEXT is-a DV_TEXT: no WrongType.
        let n = node("DV_TEXT", "/t");
        let inst = json!({
            "_type": "DV_CODED_TEXT", "value": "x",
            "defining_code": {"_type": "CODE_PHRASE",
                "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "local"},
                "code_string": "at0001"}
        });
        assert!(
            !kinds(&walk_only(&inst, &n)).contains(&ValidationKind::WrongType),
            "DV_CODED_TEXT should conform to a DV_TEXT slot"
        );
    }

    // ── AOM 1.4 C_ATTRIBUTE.existence ────────────────────────────────────────

    #[test]
    fn existence_mandatory_attribute_missing_reported() {
        // A node requiring a mandatory `value` attribute (existence {1..1}); the
        // instance omits it → Required.
        let mut n = node("ELEMENT", "/items[at0001]");
        n.existence = vec![WebTemplateExistence {
            min: 1,
            max: 1,
            path: "/items[at0001]/value".to_owned(),
        }];
        let inst = json!({"_type": "ELEMENT", "archetype_node_id": "at0001",
            "name": {"_type": "DV_TEXT", "value": "e"}});
        let msgs = walk_only(&inst, &n);
        assert!(
            msgs.iter()
                .any(|m| m.kind == ValidationKind::Required && m.path.ends_with("/value")),
            "expected a Required existence violation for the missing value, got {msgs:?}"
        );
    }

    #[test]
    fn existence_present_attribute_is_clean() {
        let mut n = node("ELEMENT", "/items[at0001]");
        n.existence = vec![WebTemplateExistence {
            min: 1,
            max: 1,
            path: "/items[at0001]/value".to_owned(),
        }];
        let inst = json!({"_type": "ELEMENT", "archetype_node_id": "at0001",
            "name": {"_type": "DV_TEXT", "value": "e"},
            "value": {"_type": "DV_TEXT", "value": "v"}});
        assert!(
            walk_only(&inst, &n).is_empty(),
            "a present mandatory value should be clean"
        );
    }

    #[test]
    fn existence_empty_array_counts_as_absent() {
        let mut n = node("COMPOSITION", "");
        n.existence = vec![WebTemplateExistence {
            min: 1,
            max: -1,
            path: "/content".to_owned(),
        }];
        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": []});
        let msgs = walk_only(&inst, &n);
        assert!(
            msgs.iter().any(|m| m.kind == ValidationKind::Required),
            "an empty mandatory container attribute is absent, got {msgs:?}"
        );
    }

    // ── path parsing ─────────────────────────────────────────────────────────

    #[test]
    fn segment_parsing_respects_brackets() {
        // Parsing routes through the single `openehr_rm::v1_2::paths` implementation
        // via `crate::flat::rmpath`; this asserts the validator sees the same segments.
        let segs = rmpath::parse("/content[openEHR-EHR-SECTION.x.v1]/items[at0004,'Sys']/value");
        assert_eq!(segs.len(), 3);
        assert_eq!(segs[0].attribute, "content");
        assert_eq!(
            segs[0].predicate.archetype_node_id.as_deref(),
            Some("openEHR-EHR-SECTION.x.v1")
        );
        assert_eq!(segs[0].predicate.name_value, None);
        assert_eq!(
            segs[1].predicate.archetype_node_id.as_deref(),
            Some("at0004")
        );
        assert_eq!(segs[1].predicate.name_value.as_deref(), Some("Sys"));
        assert!(segs[2].predicate.is_empty());
    }

    // ── CNF-hardening additions (master15/16/17 truth tables) ─────────────────

    /// `1..*` container cardinality with zero members → Cardinality (master15
    /// CONT-COMP-content_card_1plus; AOM 1.4 §cardinality).
    #[test]
    fn cardinality_one_plus_empty_rejected() {
        let mut root = node("COMPOSITION", "");
        root.card_all = vec![WebTemplateCardinality {
            min: Some(1),
            max: -1,
            ids: None,
            path: "/content".to_owned(),
        }];
        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": []});
        let msgs = walk_only(&inst, &root);
        assert!(
            kinds(&msgs).contains(&ValidationKind::Cardinality),
            "expected Cardinality for 1..* with 0 members, got {msgs:?}"
        );
    }

    /// A bare mandatory attribute (existence `1..1`, no value constraint) must be
    /// present (master15 `context_mand`; AOM 1.4 §existence).
    #[test]
    fn bare_mandatory_attribute_absent_rejected() {
        let mut root = node("COMPOSITION", "");
        root.existence = vec![WebTemplateExistence {
            min: 1,
            max: 1,
            path: "/context".to_owned(),
        }];
        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x"});
        let msgs = walk_only(&inst, &root);
        assert!(
            kinds(&msgs).contains(&ValidationKind::Required),
            "expected Required for absent mandatory context, got {msgs:?}"
        );
    }

    /// A hoisted-wrapper slot narrowed to a concrete subtype rejects a sibling
    /// subtype and accepts the narrowed one; an abstract slot accepts any subtype
    /// (master16 §`ITEM_STRUCTURE/§EVENT` "Class not allowed").
    #[test]
    fn slot_narrowing() {
        let mut eval = node("EVALUATION", "/content[at0001]");
        eval.slots = vec![WebTemplateSlot {
            path: "/content[at0001]/data[at0002]".to_owned(),
            rm_type: "ITEM_LIST".to_owned(),
            name: None,
        }];
        let wrong = json!({"_type": "EVALUATION", "archetype_node_id": "at0001",
            "data": {"_type": "ITEM_TREE", "archetype_node_id": "at0002", "items": []}});
        let msgs = walk_only(&wrong, &eval);
        assert!(
            msgs.iter()
                .any(|m| m.kind == ValidationKind::WrongType && m.message.contains("not allowed")),
            "expected WrongType for ITEM_TREE in an ITEM_LIST slot, got {msgs:?}"
        );
        let right = json!({"_type": "EVALUATION", "archetype_node_id": "at0001",
            "data": {"_type": "ITEM_LIST", "archetype_node_id": "at0002", "items": []}});
        assert!(
            walk_only(&right, &eval).is_empty(),
            "narrowed subtype accepted"
        );

        eval.slots[0].rm_type = "ITEM_STRUCTURE".to_owned();
        let any = json!({"_type": "EVALUATION", "archetype_node_id": "at0001",
            "data": {"_type": "ITEM_TABLE", "archetype_node_id": "at0002", "rows": []}});
        assert!(
            walk_only(&any, &eval).is_empty(),
            "abstract ITEM_STRUCTURE slot admits any subtype"
        );
    }

    /// `C_INTEGER.list` on `DV_COUNT.magnitude` (master17.3 CONT-DV_COUNT-validate_list).
    #[test]
    fn numeric_list_membership() {
        let mut count = node("DV_COUNT", "/value");
        count.inputs = vec![WebTemplateInput::new(WebTemplateInputType::Integer, None)];
        count.numeric_lists = vec![("magnitude".to_owned(), vec![3.0])];
        let bad = json!({"_type": "DV_COUNT", "magnitude": 7});
        let msgs = walk_only(&bad, &count);
        assert!(
            kinds(&msgs).contains(&ValidationKind::CodedValue),
            "expected CodedValue for magnitude off the list, got {msgs:?}"
        );
        let good = json!({"_type": "DV_COUNT", "magnitude": 3});
        assert_eq!(walk_only(&good, &count), Vec::<ValidationMessage>::new());
    }

    /// `DV_PROPORTION` `type` kind membership (master17.3 CONT-DV_PROPORTION-*).
    #[test]
    fn proportion_kind_membership() {
        let mut prop = node("DV_PROPORTION", "/value");
        prop.inputs = vec![WebTemplateInput::new(
            WebTemplateInputType::Decimal,
            Some("numerator"),
        )];
        prop.proportion_types = vec!["percent".to_owned()];
        let bad =
            json!({"_type": "DV_PROPORTION", "numerator": 1.0, "denominator": 2.0, "type": 0});
        let msgs = walk_only(&bad, &prop);
        assert!(
            kinds(&msgs).contains(&ValidationKind::CodedValue),
            "expected CodedValue for ratio where percent required, got {msgs:?}"
        );
        let good =
            json!({"_type": "DV_PROPORTION", "numerator": 42.0, "denominator": 100.0, "type": 2});
        assert_eq!(walk_only(&good, &prop), Vec::<ValidationMessage>::new());
    }

    /// `C_QUANTITY_ITEM.precision` (`OpenehrProfile.xsd`, surfaced as
    /// `inputs[].validation.precision`): the instance's `DV_QUANTITY.precision`
    /// must lie in the interval; an absent instance precision violates
    /// nothing (#3027).
    #[test]
    fn quantity_precision_interval() {
        let mut q = node("DV_QUANTITY", "/q");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Decimal, Some("magnitude"));
        input.validation = Some(WebTemplateValidation {
            pattern: None,
            range: None,
            precision: Some(WebTemplateRange {
                min_op: Some(">=".to_owned()),
                min: Some(json!(0)),
                max_op: Some("<=".to_owned()),
                max: Some(json!(1)),
            }),
        });
        q.inputs = vec![input];

        let over =
            json!({"_type": "DV_QUANTITY", "magnitude": 5.25, "units": "kg", "precision": 3});
        assert!(
            kinds(&walk_only(&over, &q)).contains(&ValidationKind::RangeError),
            "precision 3 violates [0, 1]"
        );
        let ok = json!({"_type": "DV_QUANTITY", "magnitude": 5.2, "units": "kg", "precision": 1});
        assert_eq!(walk_only(&ok, &q), Vec::<ValidationMessage>::new());
        let absent = json!({"_type": "DV_QUANTITY", "magnitude": 5.2, "units": "kg"});
        assert!(
            walk_only(&absent, &q).is_empty(),
            "an absent optional precision violates nothing"
        );
    }

    /// `DV_PROPORTION.precision` under the same interval contract as
    /// `DV_QUANTITY` (the `|precision` suffix in the `master05` table; #3027).
    #[test]
    fn proportion_precision_interval() {
        let mut prop = node("DV_PROPORTION", "/p");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Decimal, Some("numerator"));
        input.validation = Some(WebTemplateValidation {
            pattern: None,
            range: None,
            precision: Some(WebTemplateRange {
                min_op: Some(">=".to_owned()),
                min: Some(json!(0)),
                max_op: Some("<=".to_owned()),
                max: Some(json!(0)),
            }),
        });
        prop.inputs = vec![input];
        let bad = json!({"_type": "DV_PROPORTION", "numerator": 42.0, "denominator": 100.0,
            "type": 2, "precision": 2});
        assert!(
            kinds(&walk_only(&bad, &prop)).contains(&ValidationKind::RangeError),
            "precision 2 violates [0, 0]"
        );
        let ok = json!({"_type": "DV_PROPORTION", "numerator": 42.0, "denominator": 100.0,
            "type": 2, "precision": 0});
        assert_eq!(walk_only(&ok, &prop), Vec::<ValidationMessage>::new());
    }

    /// `C_DATE` pattern + range (master17.4 CONT-DV_DATE-validate_constraint/-range).
    #[test]
    fn temporal_pattern_and_range() {
        let mut date = node("DV_DATE", "/value");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Date, None);
        input.validation = Some(WebTemplateValidation {
            pattern: Some("yyyy-mm-dd".to_owned()),
            range: Some(WebTemplateRange {
                min_op: Some(">=".to_owned()),
                min: Some(json!("2021-01-01")),
                max_op: Some("<=".to_owned()),
                max: Some(json!("2021-12-31")),
            }),
            precision: None,
        });
        date.inputs = vec![input];

        let partial = json!({"_type": "DV_DATE", "value": "2021"});
        assert!(
            kinds(&walk_only(&partial, &date)).contains(&ValidationKind::PatternError),
            "partial date violates yyyy-mm-dd"
        );
        let out = json!({"_type": "DV_DATE", "value": "2025-06-01"});
        assert!(
            kinds(&walk_only(&out, &date)).contains(&ValidationKind::RangeError),
            "date outside the range"
        );
        let ok = json!({"_type": "DV_DATE", "value": "2021-10-18"});
        assert_eq!(walk_only(&ok, &date), Vec::<ValidationMessage>::new());
    }

    /// `C_TIME` pattern: a partial time violates HH:MM:SS (master17.4 CONT-DV_TIME).
    #[test]
    fn time_pattern_partial_rejected() {
        let mut time = node("DV_TIME", "/value");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Time, None);
        input.validation = Some(WebTemplateValidation {
            pattern: Some("HH:MM:SS".to_owned()),
            range: None,
            precision: None,
        });
        time.inputs = vec![input];
        assert!(
            kinds(&walk_only(
                &json!({"_type": "DV_TIME", "value": "22"}),
                &time
            ))
            .contains(&ValidationKind::PatternError)
        );
        assert_eq!(
            walk_only(&json!({"_type": "DV_TIME", "value": "22:18:16"}), &time),
            Vec::<ValidationMessage>::new()
        );
    }

    /// `C_DURATION` allowed fields + range (master17.4 CONT-DV_DURATION-*).
    #[test]
    fn duration_fields_and_range() {
        let mut dur = node("DV_DURATION", "/value");
        dur.inputs = vec![
            WebTemplateInput::new(WebTemplateInputType::Integer, Some("hour")),
            WebTemplateInput::new(WebTemplateInputType::Integer, Some("minute")),
        ];
        dur.duration_range = Some(WebTemplateRange {
            min_op: Some(">=".to_owned()),
            min: Some(json!("PT0S")),
            max_op: Some("<=".to_owned()),
            max: Some(json!("PT1H")),
        });
        assert!(
            kinds(&walk_only(
                &json!({"_type": "DV_DURATION", "value": "P1Y"}),
                &dur
            ))
            .contains(&ValidationKind::PatternError),
            "year field forbidden by the pattern"
        );
        assert!(
            kinds(&walk_only(
                &json!({"_type": "DV_DURATION", "value": "PT5H"}),
                &dur
            ))
            .contains(&ValidationKind::RangeError),
            "PT5H outside [PT0S,PT1H]"
        );
        assert_eq!(
            walk_only(&json!({"_type": "DV_DURATION", "value": "PT30M"}), &dur),
            Vec::<ValidationMessage>::new()
        );
    }

    /// An enumerated **external** `C_CODE_PHRASE` list constrains membership
    /// (master17.2 CONT-DV_CODED_TEXT-validate_ext_term; AOM 1.4 §`C_CODE_PHRASE`).
    #[test]
    fn external_code_list_membership() {
        let mut coded = node("DV_CODED_TEXT", "/value");
        let mut input = WebTemplateInput::new(WebTemplateInputType::CodedText, Some("code"));
        input.terminology = Some("SNOMED-CT".to_owned());
        input.list = vec![WebTemplateCodedValue::new("73211009", None)];
        coded.inputs = vec![input];

        let bad = json!({"_type": "DV_CODED_TEXT", "value": "x", "defining_code": {
            "terminology_id": {"value": "SNOMED-CT"}, "code_string": "99999999"}});
        assert!(
            kinds(&walk_only(&bad, &coded)).contains(&ValidationKind::CodedValue),
            "external code off the enumerated list"
        );
        let good = json!({"_type": "DV_CODED_TEXT", "value": "x", "defining_code": {
            "terminology_id": {"value": "SNOMED-CT"}, "code_string": "73211009"}});
        assert_eq!(walk_only(&good, &coded), Vec::<ValidationMessage>::new());
        // A code from a different terminology than the constraint's is not judged.
        let other = json!({"_type": "DV_CODED_TEXT", "value": "x", "defining_code": {
            "terminology_id": {"value": "ICD10"}, "code_string": "A00"}});
        assert_eq!(walk_only(&other, &coded), Vec::<ValidationMessage>::new());
    }

    /// `C_CODE_PHRASE` on a coded attribute outside `defining_code`
    /// (`DV_MULTIMEDIA.media_type` — master17.6 CONT-DV_MULTIMEDIA-validate_media_type).
    #[test]
    fn media_type_code_list() {
        let mut mm = node("DV_MULTIMEDIA", "/value");
        mm.inputs = vec![WebTemplateInput::new(WebTemplateInputType::Text, None)];
        mm.code_lists = vec![WebTemplateCodeList {
            attr: "media_type".to_owned(),
            terminology: Some("IANA_media-types".to_owned()),
            codes: vec!["image/png".to_owned()],
        }];
        let bad = json!({"_type": "DV_MULTIMEDIA", "size": 1, "media_type": {
            "terminology_id": {"value": "IANA_media-types"}, "code_string": "image/gif"}});
        assert!(
            kinds(&walk_only(&bad, &mm)).contains(&ValidationKind::CodedValue),
            "media_type off the enumerated list"
        );
        let good = json!({"_type": "DV_MULTIMEDIA", "size": 1, "media_type": {
            "terminology_id": {"value": "IANA_media-types"}, "code_string": "image/png"}});
        assert_eq!(walk_only(&good, &mm), Vec::<ValidationMessage>::new());
    }

    // ── closed-archetype walk ─────────────────────────────────────────────────

    /// A COMPOSITION whose `content` is closed to `openEHR-EHR-SECTION.x.v1`: the
    /// defined section is accepted, a foreign OBSERVATION is rejected as unexpected
    /// (closed-world rule 1).
    #[test]
    fn closed_world_rejects_foreign_content() {
        let mut root = node("COMPOSITION", "");
        root.closed_attributes = vec![WebTemplateClosedAttribute {
            path: "/content".to_owned(),
            allowed_ids: vec!["openEHR-EHR-SECTION.x.v1".to_owned()],
            slots: vec![],
        }];
        let inst = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": [
            {"_type": "SECTION", "archetype_node_id": "openEHR-EHR-SECTION.x.v1",
             "name": {"_type": "DV_TEXT", "value": "s"}},
            {"_type": "OBSERVATION", "archetype_node_id": "openEHR-EHR-OBSERVATION.foreign.v1",
             "name": {"_type": "DV_TEXT", "value": "o"}}
        ]});
        // The closed-world admission rule: an unmatched *archetype-rooted* child is
        // tolerated (the flat OPT does not enumerate the full slot-fill universe;
        // the CNF corpus itself commits such ENTRYs).
        let msgs = walk_only(&inst, &root);
        assert!(
            msgs.is_empty(),
            "foreign archetype-rooted content is tolerated (the closed-world admission rule), got {msgs:?}"
        );
        // At-coded children remain closed: an at-coded child matching no sibling
        // constraint is rejected (closed-world rule 1).
        let at_foreign = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": [
            {"_type": "SECTION", "archetype_node_id": "at0099",
             "name": {"_type": "DV_TEXT", "value": "s"}}
        ]});
        let msgs = walk_only(&at_foreign, &root);
        assert!(
            msgs.iter()
                .any(|m| m.kind == ValidationKind::Unexpected && m.message.contains("at0099")),
            "expected an Unexpected violation for the foreign at-coded child, got {msgs:?}"
        );
        let ok = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": [
            {"_type": "SECTION", "archetype_node_id": "openEHR-EHR-SECTION.x.v1",
             "name": {"_type": "DV_TEXT", "value": "s"}}]});
        assert!(
            walk_only(&ok, &root).is_empty(),
            "the defined section is admitted"
        );
    }

    /// A metadata value (no `archetype_node_id`, i.e. non-LOCATABLE) under a closed
    /// attribute is never flagged (closed-world rule 2 — the `archetype_node_id`
    /// discriminator).
    #[test]
    fn closed_world_ignores_metadata_values() {
        let mut root = node("ELEMENT", "/items[at0001]");
        root.closed_attributes = vec![WebTemplateClosedAttribute {
            path: "/items[at0001]/value".to_owned(),
            allowed_ids: vec!["at9999".to_owned()],
            slots: vec![],
        }];
        let inst = json!({"_type": "ELEMENT", "archetype_node_id": "at0001",
            "name": {"_type": "DV_TEXT", "value": "e"},
            "value": {"_type": "DV_QUANTITY", "magnitude": 1.0, "units": "kg"}});
        assert!(
            walk_only(&inst, &root).is_empty(),
            "a DATA_VALUE with no archetype_node_id must not be flagged by closure"
        );
    }

    // ── ARCHETYPE_SLOT enforcement ────────────────────────────────────────────

    fn obs_slot(includes: &[&str], excludes: &[&str], min: i32, max: i32) -> WebTemplateNode {
        let mut root = node("COMPOSITION", "");
        root.closed_attributes = vec![WebTemplateClosedAttribute {
            path: "/content".to_owned(),
            allowed_ids: vec![],
            slots: vec![WebTemplateArchetypeSlot {
                rm_type: "OBSERVATION".to_owned(),
                min,
                max,
                includes: includes.iter().map(|s| (*s).to_owned()).collect(),
                excludes: excludes.iter().map(|s| (*s).to_owned()).collect(),
            }],
        }];
        root
    }

    fn content_obs(archetype_id: &str, rm_type: &str) -> Value {
        json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": [
            {"_type": rm_type, "archetype_node_id": archetype_id,
             "name": {"_type": "DV_TEXT", "value": "o"}}]})
    }

    #[test]
    fn slot_admits_include_rejects_others() {
        let root = obs_slot(&[r"openEHR-EHR-OBSERVATION\..*"], &[], 0, -1);
        assert!(
            walk_only(
                &content_obs("openEHR-EHR-OBSERVATION.bp.v1", "OBSERVATION"),
                &root
            )
            .is_empty(),
            "an include-matching filler is admitted"
        );
        let msgs = walk_only(
            &content_obs("openEHR-EHR-EVALUATION.x.v1", "EVALUATION"),
            &root,
        );
        assert!(
            msgs.iter().any(|m| m.kind == ValidationKind::Unexpected),
            "a wrong-rm-type filler is rejected, got {msgs:?}"
        );
    }

    #[test]
    fn slot_exclude_rejects_matching_filler() {
        let root = obs_slot(
            &[r"openEHR-EHR-OBSERVATION\..*"],
            &[r"openEHR-EHR-OBSERVATION\.secret\..*"],
            0,
            -1,
        );
        let msgs = walk_only(
            &content_obs("openEHR-EHR-OBSERVATION.secret.v1", "OBSERVATION"),
            &root,
        );
        assert!(
            msgs.iter().any(|m| m.kind == ValidationKind::Unexpected),
            "an exclude-matching filler is rejected, got {msgs:?}"
        );
    }

    #[test]
    fn slot_blanket_exclude_ignored_when_includes_present() {
        // ADL 1.4 closed-slot idiom: specific include + a blanket `.*` exclude. The
        // specific include wins (AOM 1.4 has no is_closed) — the filler passes.
        let root = obs_slot(&[r"openEHR-EHR-OBSERVATION\.bp\.v1"], &[".*"], 0, -1);
        assert!(
            walk_only(
                &content_obs("openEHR-EHR-OBSERVATION.bp.v1", "OBSERVATION"),
                &root
            )
            .is_empty(),
            "a specific include overrides a blanket `.*` exclude"
        );
    }

    #[test]
    fn slot_occurrences_min_and_max() {
        let root = obs_slot(&[r"openEHR-EHR-OBSERVATION\..*"], &[], 1, 1);
        let empty = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": []});
        assert!(
            walk_only(&empty, &root)
                .iter()
                .any(|m| m.kind == ValidationKind::Required),
            "an unfilled mandatory slot is Required"
        );
        let two = json!({"_type": "COMPOSITION", "archetype_node_id": "x", "content": [
            {"_type": "OBSERVATION", "archetype_node_id": "openEHR-EHR-OBSERVATION.a.v1",
             "name": {"_type": "DV_TEXT", "value": "a"}},
            {"_type": "OBSERVATION", "archetype_node_id": "openEHR-EHR-OBSERVATION.b.v1",
             "name": {"_type": "DV_TEXT", "value": "b"}}]});
        assert!(
            walk_only(&two, &root)
                .iter()
                .any(|m| m.kind == ValidationKind::Occurrences),
            "too many slot fillers is Occurrences"
        );
    }

    // ── DV_ORDINAL / DV_SCALE (symbol, value) pairing ─────────────────────────

    fn ordinal_node(rm: &str, scale: bool) -> WebTemplateNode {
        let mut n = node(rm, "/value");
        let mut input = WebTemplateInput::new(WebTemplateInputType::CodedText, None);
        let mk = |code: &str, v: i32| {
            let mut cv = WebTemplateCodedValue::new(code, None);
            if scale {
                cv.scale = Some(f64::from(v));
            } else {
                cv.ordinal = Some(v);
            }
            cv
        };
        input.list = vec![mk("at0014", 0), mk("at0015", 1)];
        n.inputs = vec![input];
        n
    }

    fn ordinal_value(rm: &str, v: &Value, code: &str) -> Value {
        json!({"_type": rm, "value": v, "symbol": {"_type": "DV_CODED_TEXT",
            "value": "s", "defining_code": {"_type": "CODE_PHRASE",
            "terminology_id": {"_type": "TERMINOLOGY_ID", "value": "local"}, "code_string": code}}})
    }

    #[test]
    fn ordinal_pair_must_match() {
        let n = ordinal_node("DV_ORDINAL", false);
        assert_eq!(
            walk_only(&ordinal_value("DV_ORDINAL", &json!(0), "at0014"), &n),
            Vec::<ValidationMessage>::new()
        );
        assert!(
            kinds(&walk_only(
                &ordinal_value("DV_ORDINAL", &json!(1), "at0014"),
                &n
            ))
            .contains(&ValidationKind::CodedValue),
            "value 1 does not pair with symbol at0014"
        );
        assert!(
            kinds(&walk_only(
                &ordinal_value("DV_ORDINAL", &json!(0), "at0666"),
                &n
            ))
            .contains(&ValidationKind::CodedValue),
            "symbol at0666 is off the list"
        );
    }

    #[test]
    fn scale_pair_must_match() {
        let n = ordinal_node("DV_SCALE", true);
        assert_eq!(
            walk_only(&ordinal_value("DV_SCALE", &json!(0.0), "at0014"), &n),
            Vec::<ValidationMessage>::new()
        );
        assert!(
            kinds(&walk_only(
                &ordinal_value("DV_SCALE", &json!(1.0), "at0014"),
                &n
            ))
            .contains(&ValidationKind::CodedValue),
            "scale 1.0 does not pair with symbol at0014"
        );
    }

    // ── C_STRING fail-closed with fancy-regex fallback ────────────────────────

    #[test]
    fn c_string_backreference_is_enforced() {
        // `(a)\1` uses a backreference the `regex` crate rejects; `fancy-regex`
        // compiles it, so the pattern is enforced instead of silently passing.
        let mut n = node("DV_TEXT", "/text");
        let mut input = WebTemplateInput::new(WebTemplateInputType::Text, None);
        input.validation = Some(WebTemplateValidation {
            pattern: Some(r"(a)\1".to_owned()),
            ..Default::default()
        });
        n.inputs = vec![input];
        assert!(
            kinds(&walk_only(&json!({"_type": "DV_TEXT", "value": "ab"}), &n))
                .contains(&ValidationKind::PatternError),
            "`ab` must fail the backreference pattern rather than silently pass"
        );
        assert_eq!(
            walk_only(&json!({"_type": "DV_TEXT", "value": "aa"}), &n),
            Vec::<ValidationMessage>::new()
        );
    }

    // ── C_TIME / C_DATE_TIME timezone_validity ────────────────────────────────

    #[test]
    fn timezone_validity_mandatory_and_disallowed() {
        let mut n = node("DV_TIME", "/value");
        n.inputs = vec![WebTemplateInput::new(WebTemplateInputType::Time, None)];
        // 1001 = mandatory timezone.
        n.tz_validity = Some(1001);
        assert!(
            kinds(&walk_only(
                &json!({"_type": "DV_TIME", "value": "10:30:00"}),
                &n
            ))
            .contains(&ValidationKind::PatternError),
            "a missing mandatory timezone is rejected"
        );
        assert_eq!(
            walk_only(&json!({"_type": "DV_TIME", "value": "10:30:00Z"}), &n),
            Vec::<ValidationMessage>::new()
        );
        // 1003 = disallowed timezone.
        n.tz_validity = Some(1003);
        assert!(
            kinds(&walk_only(
                &json!({"_type": "DV_TIME", "value": "10:30:00+01:00"}),
                &n
            ))
            .contains(&ValidationKind::PatternError),
            "a present disallowed timezone is rejected"
        );
        assert_eq!(
            walk_only(&json!({"_type": "DV_TIME", "value": "10:30:00"}), &n),
            Vec::<ValidationMessage>::new()
        );
    }

    // ── name-differentiated same-archetype-id siblings ────────────────────────
    //
    // A template may fill the same archetype twice under one container, the fills
    // differentiated by their runtime `name` (RM common
    // `master03-archetyped_package.adoc` §"The `LOCATABLE` class"; AOM 1.4
    // `master04-constraint_model_package.adoc` §`node_id`). Templates fix
    // `name/value` on all-but-one sibling, so the one *unqualified* sibling admits
    // only the instances no name-qualified sibling claims.

    /// Two same-archetype siblings under `items`, one unqualified (name "A", inner
    /// `items` closed to `at0004`) and one name-qualified ('B', inner `items`
    /// closed to `at0013`).
    fn name_diff_parent() -> WebTemplateNode {
        let mut root = node("CLUSTER", "");
        let sib_a = {
            let mut n = node("CLUSTER", "/items[openEHR-EHR-CLUSTER.c.v1]");
            n.name = Some("A".to_owned());
            n.min = Some(0);
            n.max = 1;
            n.closed_attributes = vec![WebTemplateClosedAttribute {
                path: "/items[openEHR-EHR-CLUSTER.c.v1]/items".to_owned(),
                allowed_ids: vec!["at0004".to_owned()],
                slots: vec![],
            }];
            n
        };
        let sib_b = {
            let mut n = node("CLUSTER", "/items[openEHR-EHR-CLUSTER.c.v1,'B']");
            n.name = Some("B".to_owned());
            n.min = Some(0);
            n.max = 1;
            n.closed_attributes = vec![WebTemplateClosedAttribute {
                path: "/items[openEHR-EHR-CLUSTER.c.v1,'B']/items".to_owned(),
                allowed_ids: vec!["at0013".to_owned()],
                slots: vec![],
            }];
            n
        };
        root.children = vec![sib_a, sib_b];
        root
    }

    /// A same-archetype CLUSTER instance with `name` and a single at-coded child.
    fn c_instance(name: &str, child_id: &str) -> Value {
        json!({
            "_type": "CLUSTER", "archetype_node_id": "openEHR-EHR-CLUSTER.c.v1",
            "name": {"_type": "DV_TEXT", "value": name},
            "items": [{"_type": "ELEMENT", "archetype_node_id": child_id,
                       "name": {"_type": "DV_TEXT", "value": "leaf"},
                       "value": {"_type": "DV_TEXT", "value": "v"}}]
        })
    }

    fn unexpected_of(msgs: &[ValidationMessage]) -> Vec<&ValidationMessage> {
        msgs.iter()
            .filter(|m| m.kind == ValidationKind::Unexpected)
            .collect()
    }

    #[test]
    fn name_diff_siblings_route_each_instance_to_its_own_overlay() {
        let root = name_diff_parent();
        // Both instances present, each carrying its own overlay's child.
        let inst = json!({
            "_type": "CLUSTER", "archetype_node_id": "x",
            "name": {"_type": "DV_TEXT", "value": "root"},
            "items": [c_instance("A", "at0004"), c_instance("B", "at0013")]
        });
        let msgs = walk_only(&inst, &root);
        assert!(
            unexpected_of(&msgs).is_empty(),
            "both name-differentiated siblings should validate against their own \
             overlay, got {msgs:?}"
        );
    }

    #[test]
    fn name_qualified_siblings_child_in_unqualified_instance_is_unexpected() {
        let root = name_diff_parent();
        // `at0013` belongs to sibling 'B' only; inside the instance named "A" it
        // must still be Unexpected (true rejections preserved).
        let inst = json!({
            "_type": "CLUSTER", "archetype_node_id": "x",
            "name": {"_type": "DV_TEXT", "value": "root"},
            "items": [c_instance("A", "at0013")]
        });
        let msgs = walk_only(&inst, &root);
        assert!(
            unexpected_of(&msgs)
                .iter()
                .any(|m| m.message.contains("at0013")),
            "at0013 in the unqualified sibling's instance must be Unexpected, got {msgs:?}"
        );
    }

    #[test]
    fn unqualified_sibling_admits_a_runtime_named_residual_instance() {
        let root = name_diff_parent();
        // An instance whose name matches NO name-qualified sibling ("other") routes
        // to the unqualified (residual) sibling — its `name` being unconstrained,
        // master03 §"The `LOCATABLE` class" L35. Its own overlay child (`at0004`)
        // therefore validates clean…
        let ok = json!({
            "_type": "CLUSTER", "archetype_node_id": "x",
            "name": {"_type": "DV_TEXT", "value": "root"},
            "items": [c_instance("other", "at0004")]
        });
        assert!(
            unexpected_of(&walk_only(&ok, &root)).is_empty(),
            "a residual-named instance must validate against the unqualified overlay"
        );
        // …but a child that overlay forbids (`at0013`) is still Unexpected there.
        let bad = json!({
            "_type": "CLUSTER", "archetype_node_id": "x",
            "name": {"_type": "DV_TEXT", "value": "root"},
            "items": [c_instance("other", "at0013")]
        });
        assert!(
            unexpected_of(&walk_only(&bad, &root))
                .iter()
                .any(|m| m.message.contains("at0013")),
            "the residual instance is still closed-world checked against the \
             unqualified overlay"
        );
    }
}