cloudformation-validate-schema-validator 1.12.0

AWS CloudFormation Validate
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
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
//! Runtime compilation and merging of overlay CloudFormation resource provider
//! schemas on top of the bundled compiled schemas.
//!
//! Bundled schemas are compiled at build time from raw CloudFormation registry
//! JSON into the compiled representation baked into the binary. This module runs
//! the *same* transform at engine construction time, so callers can supply
//! additional schemas - for example properties a service has shipped but not yet
//! published to the CloudFormation registry - and have templates using them
//! validate without false findings.
//!
//! # Merge model
//!
//! One model, applied uniformly: an overlay may **add** entries to a collection
//! and **restate** a single-valued constraint or a logical group, and it never
//! silently drops a constraint the bundled schema already carries. Adding an
//! entry to `required` or to a dependency list is stating a constraint, so it can
//! legitimately produce a finding on a template that violates it; what an overlay
//! cannot do is make a bundled constraint disappear. Concretely, for every field
//! of a schema or property:
//!
//! | Field kind | Rule |
//! |------------|------|
//! | Keyed collections - `properties`, `definitions`, `patternProperties` | deep-merged by key: new keys are added, shared keys recurse |
//! | `required` | replaced when the overlay states the keyword (even as `[]` - that is how a requirement is cleared, and a removal is logged); unioned into the base when the keyword is omitted |
//! | Independent-fact collections - the `/properties/...` lifecycle metadata lists, and each key of `dependentRequired`/`dependentExcluded` | unioned, order-preserving, deduplicated |
//! | Single-valued constraints - `type`, `pattern`, `const`, numeric bounds, lengths, item and property counts, `uniqueItems`, `format`, `additionalProperties`, `not.enum` | replaced when the overlay supplies them, inherited otherwise |
//! | Logical groups - `requiredOr`, `requiredXor`, `primaryIdentifier` | replaced as a whole when supplied. Each is *one* group ("at least one of", "exactly one of", "these properties identify the resource"), so unioning two groups would fabricate a third constraint that neither schema states |
//! | Composition - `allOf`/`anyOf`/`oneOf`/`if`-`then`-`else` | replaced when supplied, because a complete overlay restates the whole composition and appending would duplicate branches. `allOf` splits into plain and conditional entries during compilation, so an overlay supplying `allOf` replaces both halves together |
//! | Singleton subschemas - `items` (the schema every array element must satisfy) | deep-merged, like one keyed entry: an overlay stating only `pattern` narrows the element schema without discarding the rest of it |
//! | Schema-level metadata - `replacementStrategy`, `documentationUrl`, `sourceUrl` | replaced when supplied. These enrich reporting and constrain nothing |
//! | Enums | `enum` and `enumCaseInsensitive` are two representations of one field and are never both populated for a property. Supplying either replaces whichever the bundled schema used, keeping the bundled comparison semantics: widening a case-insensitive enum with a plain `enum` stays case-insensitive, so previously accepted casings keep validating |
//!
//! A `$ref` is never folded into the property that points at it. An overlay
//! extending a `$ref` property has its fields merged *beside* the reference, and
//! `PropSchema::resolve` combines the
//! whole chain at validation time - each hop's own constraints applied over its
//! target, nearest hop winning. So a constraint-only overlay (say, just `enum`)
//! takes effect, a chain of references is followed to its end, and a definition
//! changed by a later overlay still reaches every property referencing it. It
//! also means the merge never depends on the order definitions are visited in.
//! Within a chain the same rules decide each field, so a hop that restates a
//! single-valued constraint overrides the one further along, while collections
//! accumulate across every hop.
//!
//! An overlay that supplies a `$ref` for a property updates the reference target
//! (`ref_name`) but preserves the base property's existing inline/additive
//! constraints (e.g. pattern, maxLength). This means an overlay can redirect a
//! property to a different definition without discarding constraints the bundled
//! schema already carries.
//!
//! Overlays for the same type are applied in the order given; a later overlay
//! sees the result of the earlier ones.
//!
//! # Scope limits
//!
//! - An overlay cannot remove an entry from a lifecycle metadata list - those
//!   collections only ever grow. `required` is the one collection with
//!   replacement semantics (below), and any requirement a replacement removes
//!   is logged.
//! - Composition entries (`allOf`/`anyOf`/`oneOf` branches) are full property
//!   schemas: branch `required`, `additionalProperties`, dependency maps, value
//!   constraints, and nested conditionals are all evaluated when the branch is
//!   matched or (for a selected `then`/`else` branch) enforced.
//! - Constructs the compiled model does not represent - a `$ref` outside
//!   `#/definitions/`, tuple-form `items`, an unknown property type, malformed
//!   keyword values, or invalid regular expressions - are rejected.
//! - Validation keywords with no compiled representation (`propertyNames`,
//!   `contains`, and `not` other than `not.enum`) are rejected.
//!   `multipleOf` and `dependencies` (array-form property dependencies) ARE
//!   represented and accepted.
//! - Constraint siblings beside a `$ref` are accepted when they have a compiled
//!   representation (e.g. `pattern`, `maxLength`, `enum`). Only genuinely
//!   unrepresented keywords are rejected. Annotation-only siblings
//!   (`description`, `title`) are always accepted. Bundled schemas are compiled
//!   with draft-07 `$ref` evaluation instead (siblings ignored), so the engine
//!   never enforces more than CloudFormation's own contract on them; an overlay
//!   author states siblings deliberately and gets them enforced.
//! - `enum` and `enumCaseInsensitive` are one field in two comparison modes; an
//!   overlay cannot switch a property the service treats case-insensitively over
//!   to case-sensitive comparison, because doing so would reject casings that
//!   validate today.
//! - `required` has authoritative replacement semantics: an overlay that
//!   explicitly states `required` (even as `required: []`) replaces the prior
//!   required list at that schema level, including clearing it. Omitting
//!   `required` from the overlay preserves the base's list unchanged.
//! - Conditional constraints the build pipeline contributes as extension
//!   fragments are validated from a separate embedded artifact that overlays do
//!   not merge into, so an overlay cannot suppress a finding originating there.
//! - Schema-level metadata (`documentationUrl`, `sourceUrl`, `replacementStrategy`)
//!   alone is not sufficient - the overlay must carry at least one validatable
//!   constraint. Metadata enriches diagnostic context only when combined with
//!   properties, required, or other constraints. Annotations such as
//!   `description` are accepted anywhere and compiled to nothing: they have no
//!   validation meaning and no reader, so an overlay stating only a description
//!   changes nothing.
//! - Overlay-derived resource types, GetAtt attributes and types, Ref return
//!   types, primary identifiers, and schema metadata are propagated to both rule
//!   engines. Region-specific availability and enum snapshots remain bundled.
//!
//! # Catalog vs. config separation
//!
//! The [`SchemaValidator`](crate::SchemaValidator) separates its schema store
//! (the validated/merged catalog of all compiled schemas) from its construction
//! config ([`SchemaValidatorConfig`](crate::SchemaValidatorConfig)) so embedders
//! can serialize/deserialize the config and rebuild the validator without keeping
//! the merged store around. The overlay catalog exposes overlay-aware metadata
//! (type names, GetAtt/Ref types, primary identifiers) without re-merging.

use crate::compiled::{CompiledSchema, ConditionSchema, MAX_REF_CHAIN, PropSchema};
use data_source::compiled_schema::compile_schema;
use data_source::compiled_schema::keywords;
use log::warn;
use serde_json::Value;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::error::Error;
use std::fmt;

/// Keywords that carry no validation meaning, so their presence beside a `$ref`
/// is not worth reporting. Published provider schemas routinely document a
/// referenced property this way.
///
/// Sourced from the shared vocabulary in `data_source::compiled_schema::keywords`.
const REF_ANNOTATION_KEYWORDS: &[&str] = keywords::REF_ANNOTATION_KEYWORDS;

/// Validation keywords the compiled schema model has no field for, so nothing
/// enforces them. An overlay stating one would silently weaken the author's
/// intent - rejected so embedders cannot accidentally rely on a constraint that
/// nothing checks. `not` is handled separately because only its nested `enum` is
/// modelled.
///
/// Sourced from the shared vocabulary in `data_source::compiled_schema::keywords`.
const UNREPRESENTED_CONSTRAINT_KEYWORDS: &[&str] = keywords::UNREPRESENTED_VALIDATION_KEYWORDS;

/// The set of fields the runtime evaluates when matching a composition branch.
/// Since composition branches are now full `PropSchema`s, the runtime evaluates
/// all representable constraint fields. This list gates what the overlay
/// preflight accepts - any field not here is rejected so the overlay author
/// knows it would not fire.
const COMPOSITION_ALLOWED_FIELDS: &[&str] = &[
    keywords::REF,
    keywords::TYPE,
    keywords::ENUM,
    keywords::ENUM_CASE_INSENSITIVE,
    keywords::NOT,
    keywords::CONST,
    keywords::PATTERN,
    keywords::FORMAT,
    keywords::MINIMUM,
    keywords::MAXIMUM,
    keywords::EXCLUSIVE_MINIMUM,
    keywords::EXCLUSIVE_MAXIMUM,
    keywords::MULTIPLE_OF,
    keywords::MIN_LENGTH,
    keywords::MAX_LENGTH,
    keywords::MIN_ITEMS,
    keywords::MAX_ITEMS,
    keywords::UNIQUE_ITEMS,
    keywords::MIN_PROPERTIES,
    keywords::MAX_PROPERTIES,
    keywords::REQUIRED,
    keywords::PROPERTIES,
    keywords::ADDITIONAL_PROPERTIES,
    keywords::PATTERN_PROPERTIES,
    keywords::ITEMS,
    keywords::ALL_OF,
    keywords::ANY_OF,
    keywords::ONE_OF,
    keywords::DEPENDENT_REQUIRED,
    keywords::DEPENDENT_EXCLUDED,
    keywords::REQUIRED_OR,
    keywords::REQUIRED_XOR,
    keywords::IF,
    keywords::THEN,
    keywords::ELSE,
    keywords::DEPENDENCIES,
];

/// Fields the conditional matcher evaluates on the `if` schema itself.
///
/// `type` on the `if` schema constrains the instance type: the evaluation
/// point is always a property object, so `"object"` is a no-op and any other
/// type makes the condition unsatisfiable there.
const CONDITION_ALLOWED_FIELDS: [&str; 3] = [keywords::PROPERTIES, keywords::REQUIRED, keywords::TYPE];

/// Fields `condition_matches` evaluates for a property named by an `if` schema.
/// Every listed field participates in matching: value constraints
/// (`enum`/`not`/`const`/`pattern`/`type`), nested `required`, and the
/// length/count bounds (each scoped to its instance type per draft-07).
const CONDITION_PROPERTY_ALLOWED_FIELDS: [&str; 13] = [
    keywords::REF,
    keywords::TYPE,
    keywords::ENUM,
    keywords::NOT,
    keywords::CONST,
    keywords::PATTERN,
    keywords::REQUIRED,
    keywords::MIN_ITEMS,
    keywords::MAX_ITEMS,
    keywords::MIN_LENGTH,
    keywords::MAX_LENGTH,
    keywords::MIN_PROPERTIES,
    keywords::MAX_PROPERTIES,
];

/// JSON/property type names enforced by `schema-validator`.
const SUPPORTED_PROPERTY_TYPES: [&str; 9] =
    ["string", "integer", "number", "double", "float", "boolean", "array", "object", "null"];

/// Fields allowed inside `then`/`else` of conditional `allOf` entries. Since
/// branches are full `PropSchema`s now, the same set as composition branches
/// (minus the condition keywords themselves) applies.
const CONDITIONAL_THEN_ELSE_ALLOWED_FIELDS: &[&str] = COMPOSITION_ALLOWED_FIELDS;

/// Maximum JSON nesting accepted in an overlay schema.
///
/// Compilation and merging walk the structure recursively, so an unbounded input
/// could exhaust the stack and abort the host process. The bound matches the
/// nesting limit `serde_json` applies when parsing, so a programmatically built
/// `serde_json::Value` is held to the same limit as the same schema supplied as
/// text. Real provider schemas nest far shallower - the deepest published
/// schema measures 18 levels.
pub const MAX_OVERLAY_DEPTH: usize = 128;

/// Why an overlay schema was rejected.
///
/// Every overlay entry point is fallible and validates its input up front: a
/// rejected overlay leaves the schema store untouched rather than registering a
/// half-usable schema.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SchemaOverlayError {
    /// The resource type name was empty.
    MissingTypeName,
    /// The schema was not a JSON object.
    NotAnObject { type_name: String },
    /// The schema nests deeper than [`MAX_OVERLAY_DEPTH`].
    TooDeep { type_name: String, max_depth: usize },
    /// The schema uses a construct the compiled model cannot represent, which
    /// would otherwise compile to a weaker schema than the caller wrote.
    Unsupported { type_name: String, path: String, detail: String },
    /// The schema carried no keyword the compiler understands, so applying it
    /// would change nothing.
    NoEffect { type_name: String },
    /// The definition `$ref` graph contains a cycle, naming the definitions on it.
    CyclicRef { type_name: String, cycle: Vec<String> },
    /// A chain of `$ref`s longer than resolution can follow to its end, which
    /// would leave the constraints at the end of the chain unenforced.
    RefChainTooLong { type_name: String, definition: String, hops: usize },
    /// A property ended up with both enum representations populated, which the
    /// compiled model forbids.
    ConflictingEnums { type_name: String, property: String },
    /// After the whole overlay sequence was applied, a `$ref` still points at a
    /// definition the schema does not contain, so the property it sits on
    /// validates nothing.
    DanglingRef { type_name: String, path: String, target: String },
}

impl fmt::Display for SchemaOverlayError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SchemaOverlayError::MissingTypeName => write!(
                f,
                "Additional schema is missing a resource type name (no explicit type name and none in the schema)"
            ),
            SchemaOverlayError::NotAnObject { type_name } => write!(
                f,
                "Invalid additional schema for '{type_name}': expected a JSON object describing a CloudFormation \
                 resource provider schema"
            ),
            SchemaOverlayError::TooDeep { type_name, max_depth } => write!(
                f,
                "Invalid additional schema for '{type_name}': nested deeper than the supported limit of {max_depth} \
                 levels"
            ),
            SchemaOverlayError::Unsupported { type_name, path, detail } => write!(
                f,
                "Invalid additional schema for '{type_name}': {detail} at '{path}'. Applying it would validate less \
                 than the schema states"
            ),
            SchemaOverlayError::NoEffect { type_name } => write!(
                f,
                "Invalid additional schema for '{type_name}': the schema states no properties, definitions or \
                 constraints, so applying it would have no effect"
            ),
            SchemaOverlayError::CyclicRef { type_name, cycle } => write!(
                f,
                "Invalid additional schema for '{type_name}': definitions reference each other in a cycle ({})",
                cycle.join(" -> ")
            ),
            SchemaOverlayError::RefChainTooLong { type_name, definition, hops } => write!(
                f,
                "Invalid additional schema for '{type_name}': the '$ref' chain starting at definition \
                 '{definition}' is {hops} hops long, more than the {MAX_DEFINITION_REF_CHAIN} that can be resolved, \
                 so the constraints at the end of it would not be enforced"
            ),
            SchemaOverlayError::ConflictingEnums { type_name, property } => write!(
                f,
                "Invalid additional schema for '{type_name}': property '{property}' would carry both a case-sensitive \
                 and a case-insensitive list of allowed values"
            ),
            SchemaOverlayError::DanglingRef { type_name, path, target } => write!(
                f,
                "Invalid additional schema for '{type_name}': after every overlay was applied, '{path}' still \
                 references '#/definitions/{target}', which no overlay defines, so nothing would constrain it"
            ),
        }
    }
}

impl Error for SchemaOverlayError {}

/// Compile a raw CloudFormation resource provider schema (registry JSON) into the
/// runtime [`CompiledSchema`], rejecting input the merge cannot handle safely.
///
/// The raw → compiled transform is single-sourced in `data_source`, the same
/// function the build pipeline uses for bundled schemas; the result is converted
/// into the runtime type field by field. The transform is shared but the input is
/// not enriched the way the build pipeline enriches bundled schemas - see the
/// `data_source::compiled_schema` module docs.
pub(crate) fn compile(type_name: &str, raw: &Value) -> Result<CompiledSchema, SchemaOverlayError> {
    if type_name.trim().is_empty() {
        return Err(SchemaOverlayError::MissingTypeName);
    }
    if type_name != type_name.trim() {
        return Err(SchemaOverlayError::Unsupported {
            type_name: type_name.to_string(),
            path: String::new(),
            detail: "type name has leading or trailing whitespace".to_string(),
        });
    }
    if let Some(declared_type_name) = raw.get(keywords::TYPE_NAME) {
        let in_schema = declared_type_name.as_str().ok_or_else(|| SchemaOverlayError::Unsupported {
            type_name: type_name.to_string(),
            path: keywords::TYPE_NAME.to_string(),
            detail: "'typeName' must be a string".to_string(),
        })?;
        if in_schema != in_schema.trim() {
            return Err(SchemaOverlayError::Unsupported {
                type_name: type_name.to_string(),
                path: keywords::TYPE_NAME.to_string(),
                detail: "type name has leading or trailing whitespace".to_string(),
            });
        }
        if !in_schema.is_empty() && in_schema != type_name {
            return Err(SchemaOverlayError::Unsupported {
                type_name: type_name.to_string(),
                path: keywords::TYPE_NAME.to_string(),
                detail: format!(
                    "the schema declares typeName '{in_schema}' but was submitted as '{type_name}'; remove one or make them match"
                ),
            });
        }
    }
    if !raw.is_object() {
        return Err(SchemaOverlayError::NotAnObject { type_name: type_name.to_string() });
    }
    check_depth(type_name, raw)?;
    check_supported(type_name, raw)?;
    let mut compiled: CompiledSchema = compile_schema(type_name, raw).into();
    // Track whether the overlay explicitly stated `required` (even as `[]`), so
    // merging knows whether to replace the base's required list authoritatively.
    compiled.required_present = raw.get(keywords::REQUIRED).is_some();
    // Propagate required_present into property-level and definition-level schemas
    // where `required` was explicitly stated in their source objects.
    propagate_required_present(raw, &mut compiled);
    // Conditionals an overlay author states are enforced in full - no dedicated
    // rule covers them, unlike bundled conditionals (see
    // `IfThenElse::enforce_full_branch`).
    mark_conditionals_for_full_enforcement(&mut compiled);
    if states_nothing(&compiled) {
        return Err(SchemaOverlayError::NoEffect { type_name: type_name.to_string() });
    }
    Ok(compiled)
}

/// Marks every conditional in the compiled overlay - at the schema root, on
/// properties and definitions at any nesting depth, and inside composition
/// branches - for full branch enforcement.
fn mark_conditionals_for_full_enforcement(schema: &mut CompiledSchema) {
    for ite in &mut schema.if_then_else {
        mark_ite(ite);
    }
    for prop in schema.properties.values_mut().chain(schema.definitions.values_mut()) {
        mark_prop_conditionals(prop);
    }
    for branch in schema.all_of.iter_mut().chain(schema.any_of.iter_mut()).chain(schema.one_of.iter_mut()) {
        mark_prop_conditionals(branch);
    }
}

fn mark_ite(ite: &mut crate::compiled::IfThenElse) {
    ite.enforce_full_branch = true;
    for branch in ite.then_schema.iter_mut().chain(ite.else_schema.iter_mut()) {
        mark_prop_conditionals(branch);
    }
}

fn mark_prop_conditionals(prop: &mut PropSchema) {
    for ite in &mut prop.if_then_else {
        mark_ite(ite);
    }
    for child in prop.properties.values_mut().chain(prop.pattern_properties.values_mut()) {
        mark_prop_conditionals(child);
    }
    if let Some(items) = prop.items.as_mut() {
        mark_prop_conditionals(items);
    }
    for branch in prop.all_of.iter_mut().chain(prop.any_of.iter_mut()).chain(prop.one_of.iter_mut()) {
        mark_prop_conditionals(branch);
    }
}

/// Whether a compiled overlay carries no information that would affect
/// validation - the shape a misspelled or wrong-format JSON object compiles to.
/// Destructured exhaustively so a new field cannot be omitted from the check.
///
/// Schema-level metadata that enriches reporting but constrains nothing
/// (`documentationUrl`, `sourceUrl`, `replacementStrategy`) is not sufficient on
/// its own. These fields enrich diagnostic context when another constraint
/// fires, but alone they state nothing validatable.
fn states_nothing(schema: &CompiledSchema) -> bool {
    let CompiledSchema {
        type_name: _,
        properties,
        definitions,
        required,
        required_present,
        additional_properties,
        read_only_properties,
        write_only_properties,
        create_only_properties,
        deprecated_properties,
        conditional_create_only_properties,
        primary_identifier,
        replacement_strategy: _,
        documentation_url: _,
        source_url: _,
        all_of,
        any_of,
        one_of,
        if_then_else,
        dependent_required,
        dependent_excluded,
        required_or,
        required_xor,
    } = schema;
    properties.is_empty()
        && definitions.is_empty()
        && required.is_empty()
        && !required_present
        && additional_properties.is_none()
        && read_only_properties.is_empty()
        && write_only_properties.is_empty()
        && create_only_properties.is_empty()
        && deprecated_properties.is_empty()
        && conditional_create_only_properties.is_empty()
        && primary_identifier.is_empty()
        && all_of.is_empty()
        && any_of.is_empty()
        && one_of.is_empty()
        && if_then_else.is_empty()
        && dependent_required.is_empty()
        && dependent_excluded.is_empty()
        && required_or.is_empty()
        && required_xor.is_empty()
}

/// Sets `required_present` on each compiled property/definition whose raw source
/// explicitly contained a `required` keyword. Walks the schema maps in lockstep
/// with the raw JSON so presence can be determined per sub-schema.
fn propagate_required_present(raw: &Value, compiled: &mut CompiledSchema) {
    fn mark_prop_map(raw_map: Option<&Value>, compiled_map: &mut HashMap<String, PropSchema>) {
        let Some(raw_obj) = raw_map.and_then(Value::as_object) else {
            return;
        };
        for (name, raw_prop) in raw_obj {
            if let Some(compiled_prop) = compiled_map.get_mut(name) {
                mark_prop(raw_prop, compiled_prop);
            }
        }
    }

    fn mark_prop(raw: &Value, compiled: &mut PropSchema) {
        if raw.get(keywords::REQUIRED).is_some() {
            compiled.required_present = true;
        }
        mark_prop_map(raw.get(keywords::PROPERTIES), &mut compiled.properties);
        mark_prop_map(raw.get(keywords::PATTERN_PROPERTIES), &mut compiled.pattern_properties);
        if let Some(raw_items) = raw.get(keywords::ITEMS)
            && let Some(compiled_items) = compiled.items.as_mut()
        {
            mark_prop(raw_items, compiled_items);
        }
    }

    mark_prop_map(raw.get(keywords::PROPERTIES), &mut compiled.properties);
    mark_prop_map(raw.get(keywords::DEFINITIONS), &mut compiled.definitions);
}

/// Rejects overlay JSON that nests deeper than [`MAX_OVERLAY_DEPTH`].
///
/// The walk is iterative: a recursive check would itself overflow on the input it
/// exists to reject.
fn check_depth(type_name: &str, raw: &Value) -> Result<(), SchemaOverlayError> {
    let mut stack = vec![(raw, 1usize)];
    while let Some((value, depth)) = stack.pop() {
        if depth > MAX_OVERLAY_DEPTH {
            return Err(SchemaOverlayError::TooDeep { type_name: type_name.to_string(), max_depth: MAX_OVERLAY_DEPTH });
        }
        match value {
            Value::Object(members) => stack.extend(members.values().map(|child| (child, depth + 1))),
            Value::Array(items) => stack.extend(items.iter().map(|child| (child, depth + 1))),
            _ => {}
        }
    }
    Ok(())
}

/// Rejects schema constructs the compiled model cannot represent.
///
/// The compiler was written for the curated schemas the build pipeline produces
/// and reduces a shape it does not model to an empty one. That is invisible on
/// curated input and unacceptable on caller input: an overlay that silently
/// validates less than it states is worse than a rejected one. The walk follows
/// schema positions only - `properties`, `definitions`, `patternProperties`,
/// `items`, and composition entries - so a value that merely happens to contain a
/// key like `type` is not mistaken for a schema.
fn check_supported(type_name: &str, raw: &Value) -> Result<(), SchemaOverlayError> {
    let reject = |path: &str, detail: &str| SchemaOverlayError::Unsupported {
        type_name: type_name.to_string(),
        path: path.to_string(),
        detail: detail.to_string(),
    };

    let mut stack: Vec<(String, &Value)> = Vec::new();
    push_schema_children(String::new(), raw, &mut stack);

    // Also validate root-level keywords directly.
    if let Some(root_obj) = raw.as_object() {
        validate_keyword_types(type_name, "", root_obj)?;
        reject_unrepresented_keywords(type_name, "", root_obj)?;
        reject_direct_if_then_else(type_name, "", root_obj)?;
    }

    while let Some((path, value)) = stack.pop() {
        let Some(members) = value.as_object() else {
            continue;
        };
        if let Some(reference) = members.get(keywords::REF) {
            match reference.as_str() {
                Some(target) if target.starts_with(keywords::DEFINITIONS_REF_PREFIX) => {}
                Some(target) => {
                    return Err(reject(&path, &format!("'$ref' target '{target}' is not '#/definitions/<name>'")));
                }
                None => return Err(reject(&path, "'$ref' must be a string")),
            }
            // compile_prop now preserves represented constraint siblings beside
            // a $ref - they are merged at validation time via PropSchema::resolve.
            // Only reject siblings that are genuinely unrepresented (keywords with
            // no compiled field), so the overlay author is warned rather than
            // silently weakened.
            let unrepresented_siblings: Vec<&str> = members
                .keys()
                .map(String::as_str)
                .filter(|key| {
                    *key != keywords::REF
                        && !REF_ANNOTATION_KEYWORDS.contains(key)
                        && UNREPRESENTED_CONSTRAINT_KEYWORDS.contains(key)
                })
                .collect();
            if !unrepresented_siblings.is_empty() {
                return Err(reject(
                    &path,
                    &format!(
                        "'{}' beside a '$ref' has no representation in the compiled schema model",
                        unrepresented_siblings.join("', '")
                    ),
                ));
            }
        }
        if let Some(items) = members.get(keywords::ITEMS)
            && !items.is_object()
        {
            return Err(reject(&path, "'items' must be a single schema object; tuple form is not supported"));
        }
        if !members.contains_key(keywords::REF) {
            reject_unrepresented_keywords(type_name, &path, members)?;
        }
        if let Some(prop_type) = members.get(keywords::TYPE) {
            let type_names: Vec<&str> = match prop_type {
                Value::String(name) => vec![name.as_str()],
                Value::Array(names) if !names.is_empty() => names
                    .iter()
                    .map(|name| name.as_str())
                    .collect::<Option<Vec<_>>>()
                    .ok_or_else(|| reject(&path, "'type' must be a string or an array of strings"))?,
                _ => return Err(reject(&path, "'type' must be a string or a non-empty array of strings")),
            };
            if let Some(unsupported) = type_names.into_iter().find(|name| {
                !(SUPPORTED_PROPERTY_TYPES.contains(name) || (path.is_empty() && ["RESOURCE", "object"].contains(name)))
            }) {
                return Err(reject(&path, &format!("unsupported property type '{unsupported}'")));
            }
        }
        // Validate keyword types for every schema position (not only root).
        validate_keyword_types(type_name, &path, members)?;
        // Reject direct if/then/else at property level.
        reject_direct_if_then_else(type_name, &path, members)?;
        push_schema_children(path, value, &mut stack);
    }
    Ok(())
}

/// Validates the type/domain of every keyword the compiler represents.
///
/// The compiler silently ignores a wrongly-typed keyword (e.g. `required: 42`
/// instead of an array of strings). That is invisible on curated input and
/// dangerous on overlay input: the constraint is silently dropped.
fn validate_keyword_types(
    type_name: &str,
    path: &str,
    members: &serde_json::Map<String, Value>,
) -> Result<(), SchemaOverlayError> {
    let reject = |detail: &str| SchemaOverlayError::Unsupported {
        type_name: type_name.to_string(),
        path: path.to_string(),
        detail: detail.to_string(),
    };

    // String arrays: required, enum, not.enum
    for keyword in keywords::STRING_ARRAY_CONSTRAINTS {
        if let Some(val) = members.get(*keyword) {
            if let Some(arr) = val.as_array() {
                for item in arr {
                    if !item.is_string() {
                        return Err(reject(&format!("'{keyword}' must be an array of strings")));
                    }
                }
            } else {
                return Err(reject(&format!("'{keyword}' must be an array")));
            }
        }
    }

    // Metadata path arrays: these accept arrays of strings.
    for keyword in keywords::METADATA_POINTER_ARRAYS {
        if let Some(value) = members.get(*keyword) {
            let paths = value.as_array().ok_or_else(|| reject(&format!("'{keyword}' must be an array")))?;
            for path_value in paths {
                let property_path =
                    path_value.as_str().ok_or_else(|| reject(&format!("'{keyword}' must be an array of strings")))?;
                if !property_path.starts_with(keywords::PROPERTIES_PATH_PREFIX)
                    || property_path.len() == keywords::PROPERTIES_PATH_PREFIX.len()
                {
                    return Err(reject(&format!("'{keyword}' entries must be JSON pointers below '/properties/'")));
                }
            }
        }
    }

    for keyword in keywords::ENUM_KEYWORDS {
        if let Some(value) = members.get(*keyword) {
            let values = value.as_array().ok_or_else(|| reject(&format!("'{keyword}' must be an array")))?;
            if values.is_empty() {
                return Err(reject(&format!("'{keyword}' must contain at least one value")));
            }
        }
    }
    if let Some(negated) = members.get(keywords::NOT) {
        let negated_members =
            negated.as_object().ok_or_else(|| reject("'not' must be an object containing an enum"))?;
        let unsupported: Vec<&str> = negated_members
            .keys()
            .map(String::as_str)
            .filter(|key| *key != keywords::ENUM && !REF_ANNOTATION_KEYWORDS.contains(key))
            .collect();
        if !unsupported.is_empty() {
            return Err(reject("'not' is only supported when it contains an enum"));
        }
        let values = negated_members
            .get(keywords::ENUM)
            .and_then(Value::as_array)
            .ok_or_else(|| reject("'not.enum' must be an array"))?;
        if values.is_empty() {
            return Err(reject("'not.enum' must contain at least one value"));
        }
    }

    // Dependent maps: { "trigger": ["dep1", "dep2"] }
    for keyword in keywords::DEPENDENCY_MAPS {
        if let Some(val) = members.get(*keyword) {
            if let Some(obj) = val.as_object() {
                for (key, deps) in obj {
                    if let Some(arr) = deps.as_array() {
                        for item in arr {
                            if !item.is_string() {
                                return Err(reject(&format!("'{keyword}.{key}' must be an array of strings")));
                            }
                        }
                    } else {
                        return Err(reject(&format!("'{keyword}.{key}' must be an array")));
                    }
                }
            } else {
                return Err(reject(&format!("'{keyword}' must be an object")));
            }
        }
    }

    // Numeric fields (f64)
    for keyword in keywords::NUMERIC_CONSTRAINTS {
        if let Some(val) = members.get(*keyword)
            && !val.is_number()
        {
            return Err(reject(&format!("'{keyword}' must be a number")));
        }
    }

    // multipleOf: a positive number
    if let Some(val) = members.get(keywords::MULTIPLE_OF) {
        if !val.is_number() {
            return Err(reject("'multipleOf' must be a number"));
        }
        if val.as_f64().is_some_and(|n| n <= 0.0) {
            return Err(reject("'multipleOf' must be a positive number"));
        }
    }

    // dependencies: object where values are arrays of strings (property deps)
    // or schema objects (schema-form, rejected as unrepresented)
    if let Some(val) = members.get(keywords::DEPENDENCIES) {
        let obj = val.as_object().ok_or_else(|| reject("'dependencies' must be an object"))?;
        for (key, dep_value) in obj {
            if dep_value.is_array() {
                // Array-form: property dependencies - accepted
                let arr = dep_value.as_array().expect("confirmed array");
                for item in arr {
                    if !item.is_string() {
                        return Err(reject(&format!("'dependencies.{key}' array items must be strings")));
                    }
                }
            } else if dep_value.is_object() {
                // Schema-form: not represented in the compiled model
                return Err(reject(&format!(
                    "'dependencies.{key}' is a schema-form dependency which has no representation in the compiled model"
                )));
            } else {
                return Err(reject(&format!("'dependencies.{key}' must be an array or object")));
            }
        }
    }

    // u64 fields
    for keyword in keywords::U64_CONSTRAINTS {
        if let Some(value) = members.get(*keyword)
            && value.as_u64().is_none()
        {
            return Err(reject(&format!("'{keyword}' must be a non-negative integer")));
        }
    }

    // Boolean fields
    for keyword in keywords::BOOL_CONSTRAINTS {
        if let Some(val) = members.get(*keyword)
            && !val.is_boolean()
        {
            return Err(reject(&format!("'{keyword}' must be a boolean")));
        }
    }

    // String fields
    for keyword in keywords::STRING_CONSTRAINTS {
        if let Some(val) = members.get(*keyword)
            && !val.is_string()
        {
            return Err(reject(&format!("'{keyword}' must be a string")));
        }
    }

    // Validate regex patterns using the shared template-model compiler
    if let Some(Value::String(pattern)) = members.get(keywords::PATTERN)
        && !template_model::pattern::is_service_valid(pattern)
    {
        return Err(reject(&format!("'pattern' contains an invalid regex: {pattern}")));
    }

    // Properties/definitions/patternProperties must be objects with object values
    for keyword in keywords::SCHEMA_MAPS {
        if let Some(val) = members.get(*keyword) {
            if let Some(obj) = val.as_object() {
                for (name, child) in obj {
                    if !child.is_object() {
                        return Err(reject(&format!("'{keyword}.{name}' must be a JSON object")));
                    }
                }
            } else {
                return Err(reject(&format!("'{keyword}' must be a JSON object")));
            }
        }
    }

    // Validate patternProperties keys are valid regexes
    if let Some(Value::Object(pat_props)) = members.get(keywords::PATTERN_PROPERTIES) {
        for key in pat_props.keys() {
            if !template_model::pattern::is_service_valid(key) {
                return Err(reject(&format!("'patternProperties' key '{key}' is not a valid regex")));
            }
        }
    }

    // Items must be an object (tuple form already checked above)
    if let Some(val) = members.get(keywords::ITEMS)
        && !val.is_object()
    {
        return Err(reject("'items' must be a single schema object; tuple form is not supported"));
    }

    // Composition arrays: entries must be objects
    for keyword in keywords::COMPOSITION {
        if let Some(val) = members.get(*keyword) {
            if let Some(arr) = val.as_array() {
                for (i, entry) in arr.iter().enumerate() {
                    if !entry.is_object() {
                        return Err(reject(&format!("'{keyword}[{i}]' must be a JSON object")));
                    }
                }
                // Validate composition entries contain only enforced fields
                validate_composition_entries(type_name, path, keyword, arr)?;
            } else {
                return Err(reject(&format!("'{keyword}' must be an array")));
            }
        }
    }

    Ok(())
}

/// Rejects composition entries (`allOf`/`anyOf`/`oneOf`) that contain fields the
/// compiled model cannot represent or the runtime does not evaluate.
///
/// With composition branches being full `PropSchema`s, most constraint keywords
/// are allowed. Only genuinely unrepresented keywords are rejected.
fn validate_composition_entries(
    type_name: &str,
    path: &str,
    keyword: &str,
    entries: &[Value],
) -> Result<(), SchemaOverlayError> {
    let reject = |detail: &str| SchemaOverlayError::Unsupported {
        type_name: type_name.to_string(),
        path: path.to_string(),
        detail: detail.to_string(),
    };

    for (index, entry) in entries.iter().enumerate() {
        let Some(entry_members) = entry.as_object() else {
            continue;
        };

        if keyword == keywords::ALL_OF && entry_members.contains_key(keywords::IF) {
            validate_conditional_allof_entry(type_name, path, index, entry_members)?;
            continue;
        }

        let unsupported: Vec<&str> = entry_members
            .keys()
            .map(String::as_str)
            .filter(|key| !COMPOSITION_ALLOWED_FIELDS.contains(key) && !REF_ANNOTATION_KEYWORDS.contains(key))
            .collect();
        if !unsupported.is_empty() {
            return Err(reject(&format!(
                "'{keyword}[{index}]' contains fields not supported in composition branches: '{}'",
                unsupported.join("', '")
            )));
        }
    }
    Ok(())
}

/// Validates a conditional `allOf` entry (`if`/`then`/`else`).
///
/// The condition (`if`) may use `properties` and `required`; property conditions
/// are limited to fields `condition_matches` evaluates. The `then`/`else`
/// branches are restricted to `dependentRequired`/`dependentExcluded`, the only
/// fields conditional branch evaluation enforces.
fn validate_conditional_allof_entry(
    type_name: &str,
    path: &str,
    index: usize,
    entry_members: &serde_json::Map<String, Value>,
) -> Result<(), SchemaOverlayError> {
    let reject = |detail: &str| SchemaOverlayError::Unsupported {
        type_name: type_name.to_string(),
        path: path.to_string(),
        detail: detail.to_string(),
    };

    let unsupported_entry_fields: Vec<&str> = entry_members
        .keys()
        .map(String::as_str)
        .filter(|key| !keywords::CONDITIONALS.contains(key) && !REF_ANNOTATION_KEYWORDS.contains(key))
        .collect();
    if !unsupported_entry_fields.is_empty() {
        return Err(reject(&format!(
            "'allOf[{index}]' contains unsupported conditional fields: '{}'",
            unsupported_entry_fields.join("', '")
        )));
    }

    let condition = entry_members
        .get(keywords::IF)
        .and_then(Value::as_object)
        .ok_or_else(|| reject(&format!("'allOf[{index}].if' must be a JSON object")))?;
    let unsupported_condition_fields: Vec<&str> = condition
        .keys()
        .map(String::as_str)
        .filter(|key| !CONDITION_ALLOWED_FIELDS.contains(key) && !REF_ANNOTATION_KEYWORDS.contains(key))
        .collect();
    if !unsupported_condition_fields.is_empty() {
        return Err(reject(&format!(
            "'allOf[{index}].if' contains fields the conditional matcher does not enforce: '{}'",
            unsupported_condition_fields.join("', '")
        )));
    }
    if let Some(properties) = condition.get(keywords::PROPERTIES).and_then(Value::as_object) {
        for (property_name, property_schema) in properties {
            let property_members = property_schema.as_object().ok_or_else(|| {
                reject(&format!("'allOf[{index}].if.properties.{property_name}' must be a JSON object"))
            })?;
            let unsupported_property_fields: Vec<&str> = property_members
                .keys()
                .map(String::as_str)
                .filter(|key| {
                    !CONDITION_PROPERTY_ALLOWED_FIELDS.contains(key) && !REF_ANNOTATION_KEYWORDS.contains(key)
                })
                .collect();
            if !unsupported_property_fields.is_empty() {
                return Err(reject(&format!(
                    "'allOf[{index}].if.properties.{property_name}' contains fields the conditional matcher does not enforce: '{}'",
                    unsupported_property_fields.join("', '")
                )));
            }
        }
    }

    if !entry_members.contains_key(keywords::THEN) && !entry_members.contains_key(keywords::ELSE) {
        return Err(reject(&format!("'allOf[{index}]' must contain 'then' or 'else'")));
    }
    for branch_name in [keywords::THEN, keywords::ELSE] {
        let Some(branch_value) = entry_members.get(branch_name) else {
            continue;
        };
        let branch = branch_value
            .as_object()
            .ok_or_else(|| reject(&format!("'allOf[{index}].{branch_name}' must be a JSON object")))?;
        let unsupported: Vec<&str> = branch
            .keys()
            .map(String::as_str)
            .filter(|key| !CONDITIONAL_THEN_ELSE_ALLOWED_FIELDS.contains(key) && !REF_ANNOTATION_KEYWORDS.contains(key))
            .collect();
        if !unsupported.is_empty() {
            return Err(reject(&format!(
                "'allOf[{index}].{branch_name}' contains unsupported fields: '{}'",
                unsupported.join("', '")
            )));
        }
    }
    Ok(())
}

/// Rejects direct `if`/`then`/`else` at root or property level.
///
/// Standalone `if`/`then`/`else` (outside `allOf`) is not supported in the
/// compiled model - it is only recognized inside `allOf` entries. Accepting it
/// silently would drop the constraint.
fn reject_direct_if_then_else(
    type_name: &str,
    path: &str,
    members: &serde_json::Map<String, Value>,
) -> Result<(), SchemaOverlayError> {
    for keyword in keywords::CONDITIONALS {
        if members.contains_key(*keyword) && !members.contains_key(keywords::REF) {
            // Direct if/then/else is supported at property level (compiled into
            // if_then_else) and inside allOf entries. At the schema root the
            // compiler only reads conditionals from inside `allOf`, so a
            // root-level standalone conditional would be silently dropped - a
            // sibling `allOf` key does not move the root conditional into that
            // array.
            if path.is_empty() {
                return Err(SchemaOverlayError::Unsupported {
                    type_name: type_name.to_string(),
                    path: path.to_string(),
                    detail: format!(
                        "standalone '{keyword}' is not supported at the root level; place conditional logic \
                         inside an allOf array or at property level"
                    ),
                });
            }
        }
    }
    Ok(())
}

/// Rejects validation keywords the compiled model has no field for.
///
/// Promoted from warning-only to an error so embedders cannot silently weaken
/// schemas by stating constraints nothing enforces.
fn reject_unrepresented_keywords(
    type_name: &str,
    path: &str,
    members: &serde_json::Map<String, Value>,
) -> Result<(), SchemaOverlayError> {
    let unrepresented: Vec<&str> =
        members.keys().map(String::as_str).filter(|key| UNREPRESENTED_CONSTRAINT_KEYWORDS.contains(key)).collect();
    if !unrepresented.is_empty() {
        return Err(SchemaOverlayError::Unsupported {
            type_name: type_name.to_string(),
            path: path.to_string(),
            detail: format!(
                "'{}' has no representation in the compiled schema model and would silently weaken the overlay",
                unrepresented.join("', '")
            ),
        });
    }
    if members.get(keywords::NOT).is_some_and(|negated| negated.get(keywords::ENUM).is_none()) {
        return Err(SchemaOverlayError::Unsupported {
            type_name: type_name.to_string(),
            path: path.to_string(),
            detail: "'not' is only enforced when it contains an 'enum'; this form would silently \
                     weaken the overlay"
                .to_string(),
        });
    }
    Ok(())
}

/// Pushes every child of `value` that is itself in a schema position.
fn push_schema_children<'a>(path: String, value: &'a Value, stack: &mut Vec<(String, &'a Value)>) {
    let Some(members) = value.as_object() else {
        return;
    };
    let child_path = |suffix: &str| if path.is_empty() { suffix.to_string() } else { format!("{path}.{suffix}") };

    for keyword in keywords::SCHEMA_MAPS {
        if let Some(map) = members.get(*keyword).and_then(Value::as_object) {
            for (name, child) in map {
                stack.push((child_path(&format!("{keyword}.{name}")), child));
            }
        }
    }
    if let Some(items) = members.get(keywords::ITEMS) {
        stack.push((child_path("items"), items));
    }
    for keyword in keywords::COMPOSITION {
        if let Some(entries) = members.get(*keyword).and_then(Value::as_array) {
            for (index, entry) in entries.iter().enumerate() {
                stack.push((child_path(&format!("{keyword}[{index}]")), entry));
            }
        }
    }
    for keyword in keywords::CONDITIONALS {
        if let Some(branch) = members.get(*keyword) {
            stack.push((child_path(keyword), branch));
        }
    }
}

/// Checks the invariants the compiled model relies on: an acyclic definition
/// `$ref` graph, and at most one enum representation per property.
///
/// Runs on the schema as it will be stored, so it covers both a freshly inserted
/// overlay and the result of merging one into a bundled schema.
pub(crate) fn validate_schema(schema: &CompiledSchema) -> Result<(), SchemaOverlayError> {
    match find_ref_chain_defect(&schema.definitions) {
        Some(RefChainDefect::Cycle(cycle)) => {
            return Err(SchemaOverlayError::CyclicRef { type_name: schema.type_name.clone(), cycle });
        }
        Some(RefChainDefect::TooLong { start, hops }) => {
            return Err(SchemaOverlayError::RefChainTooLong {
                type_name: schema.type_name.clone(),
                definition: start,
                hops,
            });
        }
        None => {}
    }
    // Collect all conflicting-enum paths, including from composition property
    // maps, then sort deterministically before reporting the first one.
    let mut conflicts: Vec<String> = Vec::new();
    for (name, prop) in schema.properties.iter().chain(schema.definitions.iter()) {
        collect_conflicting_enums(name, prop, &mut conflicts);
    }
    // Traverse composition/condition property maps too.
    for sub in schema.all_of.iter().chain(schema.any_of.iter()).chain(schema.one_of.iter()) {
        for (name, prop) in &sub.properties {
            collect_conflicting_enums(&format!("allOf.{name}"), prop, &mut conflicts);
        }
    }
    for ite in &schema.if_then_else {
        collect_conflicting_enums_in_condition("if", &ite.condition, &mut conflicts);
        if let Some(then_sub) = &ite.then_schema {
            for (name, prop) in &then_sub.properties {
                collect_conflicting_enums(&format!("then.{name}"), prop, &mut conflicts);
            }
        }
        if let Some(else_sub) = &ite.else_schema {
            for (name, prop) in &else_sub.properties {
                collect_conflicting_enums(&format!("else.{name}"), prop, &mut conflicts);
            }
        }
    }
    conflicts.sort();
    if let Some(first) = conflicts.into_iter().next() {
        return Err(SchemaOverlayError::ConflictingEnums { type_name: schema.type_name.clone(), property: first });
    }
    Ok(())
}

/// Maximum `$ref` hops in a chain of definitions. One shorter than
/// [`MAX_REF_CHAIN`], because resolution starts at the property referencing the
/// first definition - so a chain of this length still resolves in full instead of
/// being cut short.
const MAX_DEFINITION_REF_CHAIN: usize = MAX_REF_CHAIN - 1;

/// A definition `$ref` graph resolution could not follow faithfully.
enum RefChainDefect {
    /// The definitions on a cycle, in the order they were walked.
    Cycle(Vec<String>),
    /// A chain resolution would cut short before reaching its end.
    TooLong { start: String, hops: usize },
}

/// Returns the first defect in the definition `$ref` graph, or `None` when every
/// chain is acyclic and short enough to resolve in full.
fn find_ref_chain_defect(defs: &HashMap<String, PropSchema>) -> Option<RefChainDefect> {
    let mut names: Vec<&String> = defs.keys().collect();
    names.sort();
    for start in names {
        let mut chain: Vec<String> = Vec::new();
        let mut current = start.clone();
        loop {
            if chain.contains(&current) {
                chain.push(current);
                return Some(RefChainDefect::Cycle(chain));
            }
            chain.push(current.clone());
            match defs.get(&current).and_then(|def| def.ref_name.clone()) {
                Some(next) => current = next,
                None => break,
            }
            if chain.len() > MAX_DEFINITION_REF_CHAIN {
                return Some(RefChainDefect::TooLong { start: start.clone(), hops: chain.len() });
            }
        }
    }
    None
}

/// Logs every requirement that merging `merged` over `base` removed.
///
/// An overlay that states `required` replaces the base's list at that schema
/// level; a complete provider schema does this deliberately, while a partial
/// overlay that restates `required` does it by accident. Either way a bundled
/// constraint disappearing is worth saying out loud - silently weakening the
/// schema is the failure mode this module exists to prevent.
pub(crate) fn warn_removed_required(base: &CompiledSchema, merged: &CompiledSchema) {
    for (path, removed) in removed_required(base, merged) {
        let location = if path.is_empty() { "the resource".to_string() } else { format!("'{path}'") };
        warn!(
            "Additional schema for '{}': the stated 'required' list removes {} from {location}. An overlay that \
             states 'required' replaces the previous list; omit the keyword to keep it.",
            merged.type_name,
            removed.iter().map(|name| format!("'{name}'")).collect::<Vec<_>>().join(", "),
        );
    }
}

/// Every `(path, removed names)` pair where `merged` requires less than `base`
/// at the same schema position. Paths are sorted so repeated runs report
/// identically.
pub(crate) fn removed_required(base: &CompiledSchema, merged: &CompiledSchema) -> Vec<(String, Vec<String>)> {
    let mut removals: Vec<(String, Vec<String>)> = Vec::new();
    collect_removed(String::new(), &base.required, &merged.required, &mut removals);

    let mut stack: Vec<(String, &PropSchema, &PropSchema)> = Vec::new();
    push_shared_children(String::new(), &base.properties, &merged.properties, &mut stack);
    push_shared_children("definitions".to_string(), &base.definitions, &merged.definitions, &mut stack);
    while let Some((path, before, after)) = stack.pop() {
        collect_removed(path.clone(), &before.required, &after.required, &mut removals);
        push_shared_children(path.clone(), &before.properties, &after.properties, &mut stack);
        push_shared_children(
            format!("{path}<patternProperties>"),
            &before.pattern_properties,
            &after.pattern_properties,
            &mut stack,
        );
        if let (Some(before_items), Some(after_items)) = (&before.items, &after.items) {
            stack.push((format!("{path}[]"), before_items, after_items));
        }
    }

    removals.sort();
    removals
}

/// Records the names present in `before` but missing from `after`.
fn collect_removed(path: String, before: &[String], after: &[String], out: &mut Vec<(String, Vec<String>)>) {
    let removed: Vec<String> = before.iter().filter(|name| !after.contains(name)).cloned().collect();
    if !removed.is_empty() {
        out.push((path, removed));
    }
}

/// Pushes every key the base and merged maps share, pairing the two sides.
fn push_shared_children<'a>(
    path: String,
    before: &'a HashMap<String, PropSchema>,
    after: &'a HashMap<String, PropSchema>,
    stack: &mut Vec<(String, &'a PropSchema, &'a PropSchema)>,
) {
    for (name, before_prop) in before {
        if let Some(after_prop) = after.get(name) {
            let child_path = if path.is_empty() { name.clone() } else { format!("{path}.{name}") };
            stack.push((child_path, before_prop, after_prop));
        }
    }
}

/// Logs every `$ref` pointing at a definition the schema does not contain.
///
/// Such a property carries no constraints at all, so a mistyped definition name
/// would otherwise quietly stop validating anything. It is a warning rather than a
/// rejection because overlays apply in sequence, and an earlier one may reference a
/// definition a later one supplies.
pub(crate) fn warn_dangling_refs(schema: &CompiledSchema) {
    for (path, target) in find_dangling_refs(schema) {
        warn!(
            "Additional schema for '{}': '{path}' references '#/definitions/{target}', which the schema does \
             not define, so nothing constrains it (a later overlay may still supply the definition).",
            schema.type_name
        );
    }
}

/// Every `(path, target)` pair where a `$ref` points at a definition the schema
/// does not contain, sorted for deterministic reporting.
///
/// Walked per overlay for a warning (a later overlay in the sequence may supply
/// the definition) and again after the whole sequence, where a survivor is
/// rejected - a property referencing nothing validates nothing, which is the
/// silent weakening this module exists to prevent.
pub(crate) fn find_dangling_refs(schema: &CompiledSchema) -> Vec<(String, String)> {
    let mut dangling: Vec<(String, String)> = Vec::new();
    let mut stack: Vec<(String, &PropSchema)> =
        schema.properties.iter().chain(schema.definitions.iter()).map(|(name, prop)| (name.clone(), prop)).collect();
    // Also traverse composition/condition property maps.
    for sub in schema.all_of.iter().chain(schema.any_of.iter()).chain(schema.one_of.iter()) {
        for (name, prop) in &sub.properties {
            stack.push((format!("allOf.{name}"), prop));
        }
    }
    for ite in &schema.if_then_else {
        push_condition_props("if", &ite.condition, &mut stack);
        if let Some(then_sub) = &ite.then_schema {
            for (name, prop) in &then_sub.properties {
                stack.push((format!("then.{name}"), prop));
            }
        }
        if let Some(else_sub) = &ite.else_schema {
            for (name, prop) in &else_sub.properties {
                stack.push((format!("else.{name}"), prop));
            }
        }
    }
    while let Some((path, current)) = stack.pop() {
        if let Some(target) = &current.ref_name
            && !schema.definitions.contains_key(target)
        {
            dangling.push((path.clone(), target.clone()));
        }
        for (name, child) in current.properties.iter().chain(current.pattern_properties.iter()) {
            stack.push((format!("{path}.{name}"), child));
        }
        if let Some(items) = &current.items {
            stack.push((format!("{path}[]"), items));
        }
    }
    dangling.sort();
    dangling
}

/// Helper: push condition schema properties onto the traversal stack.
fn push_condition_props<'a>(prefix: &str, cond: &'a ConditionSchema, stack: &mut Vec<(String, &'a PropSchema)>) {
    for (name, prop) in &cond.properties {
        stack.push((format!("{prefix}.{name}"), prop));
    }
    for (i, sub_cond) in cond.any_of.iter().enumerate() {
        push_condition_props(&format!("{prefix}.anyOf[{i}]"), sub_cond, stack);
    }
}

/// Collect all conflicting-enum paths into the `out` vec (does not short-circuit).
fn collect_conflicting_enums(name: &str, prop: &PropSchema, out: &mut Vec<String>) {
    let mut stack = vec![(name.to_string(), prop)];
    while let Some((path, current)) = stack.pop() {
        if !current.enum_values.is_empty() && !current.enum_case_insensitive.is_empty() {
            out.push(path.clone());
        }
        for (child_name, child) in current.properties.iter().chain(current.pattern_properties.iter()) {
            stack.push((format!("{path}.{child_name}"), child));
        }
        if let Some(items) = current.items.as_deref() {
            stack.push((format!("{path}[]"), items));
        }
    }
}

/// Collect conflicting enums from a condition schema.
fn collect_conflicting_enums_in_condition(prefix: &str, cond: &ConditionSchema, out: &mut Vec<String>) {
    for (name, prop) in &cond.properties {
        collect_conflicting_enums(&format!("{prefix}.{name}"), prop, out);
    }
    for (i, sub_cond) in cond.any_of.iter().enumerate() {
        collect_conflicting_enums_in_condition(&format!("{prefix}.anyOf[{i}]"), sub_cond, out);
    }
}

/// Deep-merge an overlay [`CompiledSchema`] into an existing bundled schema in
/// place, following the model documented at the module level.
pub(crate) fn merge_into(base: &mut CompiledSchema, overlay: CompiledSchema) {
    merge_definitions(&mut base.definitions, overlay.definitions);
    merge_prop_map(&mut base.properties, overlay.properties);

    // An overlay that explicitly states `required` (even as `[]`) authoritatively
    // replaces the prior required list - this is how an overlay clears required
    // properties. Omitting `required` from the overlay preserves the base.
    // `apply_overlay` reports any requirement a replacement removes, so a
    // partial overlay that restates `required` carelessly is called out.
    if overlay.required_present {
        base.required = overlay.required;
    } else {
        union_extend(&mut base.required, overlay.required);
    }

    replace_if_some(&mut base.additional_properties, overlay.additional_properties);
    replace_if_some(&mut base.replacement_strategy, overlay.replacement_strategy);
    replace_if_some(&mut base.documentation_url, overlay.documentation_url);
    replace_if_some(&mut base.source_url, overlay.source_url);

    // Property-path metadata lists only ever grow: an overlay that names one
    // more deprecated property must not delete the bundled deprecations.
    // `write_only`/`create_only`/`deprecated`/`conditional_create_only` drive
    // their own diagnostics; `read_only` and the primary identifier feed the
    // overlay catalog (GetAtt and Ref metadata for the rule engines).
    union_extend(&mut base.read_only_properties, overlay.read_only_properties);
    union_extend(&mut base.write_only_properties, overlay.write_only_properties);
    union_extend(&mut base.create_only_properties, overlay.create_only_properties);
    union_extend(&mut base.deprecated_properties, overlay.deprecated_properties);
    union_extend(&mut base.conditional_create_only_properties, overlay.conditional_create_only_properties);
    // The primary identifier is one identity tuple, not a set of independent
    // facts, so a supplied one replaces the bundled tuple.
    replace_if_present(&mut base.primary_identifier, overlay.primary_identifier);

    // `allOf` is split during compilation into plain subschemas and conditional
    // `if`/`then`/`else` entries. They come from one keyword, so an overlay that
    // supplies it replaces both halves together - replacing only the half it
    // happens to populate would leave a composition matching neither schema.
    //
    // This replacement does NOT suppress extension-origin findings: conditional
    // constraints from the build pipeline's extension fragments are stored in a
    // separately embedded ExtensionStore that overlays do not merge into. An
    // overlay replacing `allOf` here only affects the main schema's composition
    // - the extension-contributed conditionals remain validated independently.
    if !overlay.all_of.is_empty() || !overlay.if_then_else.is_empty() {
        base.all_of = overlay.all_of;
        base.if_then_else = overlay.if_then_else;
    }
    replace_if_present(&mut base.any_of, overlay.any_of);
    replace_if_present(&mut base.one_of, overlay.one_of);

    merge_dependency_map(&mut base.dependent_required, overlay.dependent_required);
    merge_dependency_map(&mut base.dependent_excluded, overlay.dependent_excluded);

    // Each of these is a single logical group, so a supplied group replaces the
    // bundled one; unioning would state a constraint neither schema makes.
    replace_if_present(&mut base.required_or, overlay.required_or);
    replace_if_present(&mut base.required_xor, overlay.required_xor);
}

/// Merges overlay definitions into `base`.
///
/// New definitions are inserted and shared ones are merged in place. Nothing here
/// depends on the order definitions are visited, because a `$ref` is never folded
/// into the property or definition that points at it - resolution happens at
/// validation time against the final definition set (see
/// `PropSchema::resolve`).
fn merge_definitions(base: &mut HashMap<String, PropSchema>, overlay: HashMap<String, PropSchema>) {
    for (name, def) in overlay {
        match base.entry(name) {
            Entry::Occupied(mut existing) => merge_prop(existing.get_mut(), def),
            Entry::Vacant(slot) => {
                slot.insert(def);
            }
        }
    }
}

/// Deep-merge an overlay property schema into an existing one in place.
pub(crate) fn merge_prop(base: &mut PropSchema, overlay: PropSchema) {
    let mut overlay = overlay;
    // An overlay that supplies a `$ref` updates the reference target, and its
    // remaining fields merge normally below - constraint siblings the overlay
    // states beside its reference are preserved, not discarded. The base's
    // existing inline constraints also survive, so redirecting a reference
    // never silently weakens the property.
    if overlay.ref_name.is_some() {
        base.ref_name = overlay.ref_name.take();
    }
    if is_no_op(&overlay) {
        return;
    }

    // A `$ref` on the base is deliberately left in place. The overlay's fields are
    // merged beside it and combined with the referenced definition when validation
    // resolves the property, so a definition changed by a later overlay still
    // reaches every property that points at it.
    replace_if_some(&mut base.prop_type, overlay.prop_type);
    merge_enums(base, overlay.enum_values, overlay.enum_case_insensitive);
    replace_if_present(&mut base.not_enum, overlay.not_enum);
    replace_if_some(&mut base.const_value, overlay.const_value);
    replace_if_some(&mut base.pattern, overlay.pattern);
    replace_if_some(&mut base.minimum, overlay.minimum);
    replace_if_some(&mut base.maximum, overlay.maximum);
    replace_if_some(&mut base.exclusive_minimum, overlay.exclusive_minimum);
    replace_if_some(&mut base.exclusive_maximum, overlay.exclusive_maximum);
    replace_if_some(&mut base.multiple_of, overlay.multiple_of);
    replace_if_some(&mut base.min_length, overlay.min_length);
    replace_if_some(&mut base.max_length, overlay.max_length);
    replace_if_some(&mut base.min_items, overlay.min_items);
    replace_if_some(&mut base.max_items, overlay.max_items);
    replace_if_some(&mut base.unique_items, overlay.unique_items);
    replace_if_some(&mut base.min_properties, overlay.min_properties);
    replace_if_some(&mut base.max_properties, overlay.max_properties);
    replace_if_some(&mut base.format, overlay.format);
    replace_if_some(&mut base.additional_properties, overlay.additional_properties);

    merge_prop_map(&mut base.properties, overlay.properties);
    merge_prop_map(&mut base.pattern_properties, overlay.pattern_properties);
    if overlay.required_present {
        base.required = overlay.required;
    } else {
        union_extend(&mut base.required, overlay.required);
    }

    if let Some(overlay_items) = overlay.items {
        match base.items.as_mut() {
            Some(base_items) => merge_prop(base_items, *overlay_items),
            None => base.items = Some(overlay_items),
        }
    }
    replace_if_present(&mut base.all_of, overlay.all_of);
    replace_if_present(&mut base.any_of, overlay.any_of);
    replace_if_present(&mut base.one_of, overlay.one_of);
    replace_if_present(&mut base.if_then_else, overlay.if_then_else);
    merge_dependency_map(&mut base.dependent_required, overlay.dependent_required);
    merge_dependency_map(&mut base.dependent_excluded, overlay.dependent_excluded);
    // Each is a single logical group, so a supplied group replaces the base;
    // unioning would fabricate a constraint neither schema states.
    replace_if_present(&mut base.required_or, overlay.required_or);
    replace_if_present(&mut base.required_xor, overlay.required_xor);
}

/// Applies the overlay's allowed-value list, keeping the two enum
/// representations mutually exclusive.
///
/// `enum` and `enumCaseInsensitive` are the same field in two comparison modes,
/// evaluated independently by the validator, so leaving both populated would
/// report a value twice with contradictory allowed sets. When the bundled schema
/// compares case-insensitively - because the service accepts any casing - a
/// plain `enum` overlay keeps that mode: the overlay author is widening the value
/// list, not tightening the comparison, and reinterpreting it as case-sensitive
/// would reject casings that validate today.
fn merge_enums(base: &mut PropSchema, overlay_exact: Vec<Value>, overlay_insensitive: Vec<Value>) {
    if !overlay_insensitive.is_empty() {
        base.enum_case_insensitive = overlay_insensitive;
        base.enum_values.clear();
    } else if !overlay_exact.is_empty() {
        if base.enum_case_insensitive.is_empty() {
            base.enum_values = overlay_exact;
        } else {
            base.enum_case_insensitive = overlay_exact;
            base.enum_values.clear();
        }
    }
}

fn merge_prop_map(base: &mut HashMap<String, PropSchema>, overlay: HashMap<String, PropSchema>) {
    for (name, prop) in overlay {
        match base.get_mut(&name) {
            Some(existing) => merge_prop(existing, prop),
            None => {
                base.insert(name, prop);
            }
        }
    }
}

/// Unions each trigger's dependency list rather than replacing it, so an overlay
/// naming one dependency of an existing trigger keeps the bundled ones.
fn merge_dependency_map(base: &mut HashMap<String, Vec<String>>, overlay: HashMap<String, Vec<String>>) {
    for (trigger, dependencies) in overlay {
        union_extend(base.entry(trigger).or_default(), dependencies);
    }
}

/// Whether merging this overlay property would change nothing.
///
/// Destructured exhaustively so a new field cannot be forgotten here and make an
/// overlay silently look empty.
fn is_no_op(prop: &PropSchema) -> bool {
    let PropSchema {
        ref_name,
        prop_type,
        enum_values,
        enum_case_insensitive,
        not_enum,
        const_value,
        pattern,
        minimum,
        maximum,
        exclusive_minimum,
        exclusive_maximum,
        multiple_of,
        min_length,
        max_length,
        min_items,
        max_items,
        unique_items,
        min_properties,
        max_properties,
        format,
        properties,
        required,
        required_present,
        additional_properties,
        pattern_properties,
        items,
        all_of,
        any_of,
        one_of,
        if_then_else,
        dependent_required,
        dependent_excluded,
        required_or,
        required_xor,
    } = prop;
    ref_name.is_none()
        && prop_type.is_none()
        && enum_values.is_empty()
        && enum_case_insensitive.is_empty()
        && not_enum.is_empty()
        && const_value.is_none()
        && pattern.is_none()
        && minimum.is_none()
        && maximum.is_none()
        && exclusive_minimum.is_none()
        && exclusive_maximum.is_none()
        && multiple_of.is_none()
        && min_length.is_none()
        && max_length.is_none()
        && min_items.is_none()
        && max_items.is_none()
        && unique_items.is_none()
        && min_properties.is_none()
        && max_properties.is_none()
        && format.is_none()
        && properties.is_empty()
        && required.is_empty()
        && !required_present
        && additional_properties.is_none()
        && pattern_properties.is_empty()
        && items.is_none()
        && all_of.is_empty()
        && any_of.is_empty()
        && one_of.is_empty()
        && if_then_else.is_empty()
        && dependent_required.is_empty()
        && dependent_excluded.is_empty()
        && required_or.is_empty()
        && required_xor.is_empty()
}

/// Append items from `extra` not already present in `base`.
fn union_extend(base: &mut Vec<String>, extra: Vec<String>) {
    for item in extra {
        if !base.contains(&item) {
            base.push(item);
        }
    }
}

/// Replace `base` with `overlay` only when the overlay is non-empty.
fn replace_if_present<T>(base: &mut Vec<T>, overlay: Vec<T>) {
    if !overlay.is_empty() {
        *base = overlay;
    }
}

/// Replace `base` with `overlay` only when the overlay supplies a value.
fn replace_if_some<T>(base: &mut Option<T>, overlay: Option<T>) {
    if overlay.is_some() {
        *base = overlay;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn compiled(raw: Value) -> CompiledSchema {
        compile("AWS::Test::T", &raw).expect("test schema must compile")
    }

    #[test]
    fn compile_basic_schema() {
        let c = compiled(json!({
            "properties": {
                "Name": { "type": "string" },
                "Size": { "type": "integer", "enum": [1, 2, 3] }
            },
            "required": ["Name"],
            "additionalProperties": false
        }));
        assert_eq!(c.type_name, "AWS::Test::T");
        assert!(c.properties.contains_key("Name"));
        assert_eq!(c.additional_properties, Some(false));
        assert_eq!(c.required, vec!["Name".to_string()]);
        assert_eq!(c.properties["Size"].enum_values, vec![json!(1), json!(2), json!(3)]);
    }

    #[test]
    fn compile_ref_and_property_paths() {
        let c = compiled(json!({
            "properties": { "Cfg": { "$ref": "#/definitions/Config" } },
            "definitions": { "Config": { "type": "object" } },
            "readOnlyProperties": ["/properties/Arn", "/properties/Nested/Id"],
            "primaryIdentifier": ["/properties/Id"]
        }));
        assert_eq!(c.properties["Cfg"].ref_name.as_deref(), Some("Config"));
        assert!(c.definitions.contains_key("Config"));
        assert_eq!(c.read_only_properties, vec!["Arn".to_string(), "Nested.Id".to_string()]);
        assert_eq!(c.primary_identifier, vec!["Id".to_string()]);
    }

    #[test]
    fn compile_rejects_empty_type_name() {
        let error = compile("", &json!({})).expect_err("an empty type name must be rejected");
        assert_eq!(error, SchemaOverlayError::MissingTypeName);
    }

    #[test]
    fn compile_rejects_non_object() {
        let error = compile("AWS::Test::T", &json!(42)).expect_err("a non-object schema must be rejected");
        assert!(matches!(error, SchemaOverlayError::NotAnObject { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_overlay_nested_past_the_depth_limit() {
        let mut node = json!({ "type": "string" });
        for _ in 0..MAX_OVERLAY_DEPTH {
            node = json!({ "type": "object", "properties": { "N": node } });
        }
        let error = compile("AWS::Test::Deep", &json!({ "properties": { "Top": node } }))
            .expect_err("an over-deep overlay must be rejected, not overflow the stack");
        assert!(matches!(error, SchemaOverlayError::TooDeep { .. }), "got {error:?}");
    }

    #[test]
    fn compile_accepts_overlay_within_the_depth_limit() {
        // Each `properties` wrapper contributes two levels (the map, then the
        // schema inside it), on top of the three the outer schema already uses, so
        // this is the deepest nesting the limit admits.
        let deepest_accepted_wrappers = (MAX_OVERLAY_DEPTH - 4) / 2;
        let mut node = json!({ "type": "string" });
        for _ in 0..deepest_accepted_wrappers {
            node = json!({ "type": "object", "properties": { "N": node } });
        }
        compile("AWS::Test::Deep", &json!({ "properties": { "Top": node } })).expect("ordinary nesting is accepted");
    }

    /// Builds a value whose deepest member sits exactly `depth` levels down, so the
    /// accept/reject boundary can be stated to the level rather than to the two
    /// levels a `properties` wrapper adds at a time.
    fn value_nested_to_depth(depth: usize) -> Value {
        let mut value = json!("leaf");
        for _ in 1..depth {
            value = json!({ "N": value });
        }
        value
    }

    #[test]
    fn depth_check_accepts_nesting_exactly_at_the_limit() {
        check_depth("AWS::Test::Deep", &value_nested_to_depth(MAX_OVERLAY_DEPTH))
            .expect("nesting exactly at the limit must be accepted");
    }

    #[test]
    fn depth_check_rejects_nesting_one_level_past_the_limit() {
        let error = check_depth("AWS::Test::Deep", &value_nested_to_depth(MAX_OVERLAY_DEPTH + 1))
            .expect_err("one level past the limit must be rejected");
        assert!(matches!(error, SchemaOverlayError::TooDeep { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_a_ref_outside_definitions() {
        let error = compile("AWS::Test::T", &json!({ "properties": { "P": { "$ref": "other.json#/Thing" } } }))
            .expect_err("a non-local $ref cannot be represented and must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_tuple_form_items() {
        let error = compile(
            "AWS::Test::T",
            &json!({ "properties": { "P": { "type": "array", "items": [{ "type": "string" }] } } }),
        )
        .expect_err("tuple-form items cannot be represented and must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_a_non_string_type() {
        let error = compile("AWS::Test::T", &json!({ "properties": { "P": { "type": { "nested": true } } } }))
            .expect_err("an object-valued 'type' cannot be represented and must be rejected");
        match error {
            SchemaOverlayError::Unsupported { path, .. } => {
                assert_eq!(path, "properties.P", "the error must name the offending path")
            }
            other => panic!("expected an unsupported-construct error, got {other:?}"),
        }
    }

    #[test]
    fn compile_rejects_unsupported_constructs_nested_in_definitions_and_composition() {
        for raw in [
            json!({ "definitions": { "D": { "properties": { "P": { "items": [{ "type": "string" }] } } } } }),
            json!({ "oneOf": [{ "properties": { "P": { "$ref": "https://example.com/schema" } } }] }),
            json!({
                "allOf": [{
                    "if": { "properties": { "A": { "enum": ["x"] } } },
                    "then": { "properties": { "B": { "type": [1, 2] } } }
                }]
            }),
        ] {
            let error = compile("AWS::Test::T", &raw).expect_err("nested unsupported constructs must be rejected");
            assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?} for {raw}");
        }
    }

    #[test]
    fn compile_accepts_a_property_named_like_a_keyword() {
        // A property literally named `type` or `items` is ordinary schema content
        // and must not be mistaken for the keyword.
        compile(
            "AWS::Test::T",
            &json!({
                "properties": {
                    "type": { "type": "string" },
                    "items": { "type": "array", "items": { "type": "string" } }
                }
            }),
        )
        .expect("keyword-named properties are ordinary content");
    }

    #[test]
    fn compile_accepts_an_enum_value_that_contains_schema_like_keys() {
        // Enum values are data, not schemas, so a key like `type` inside one must
        // not be inspected as a schema keyword.
        compile("AWS::Test::T", &json!({ "properties": { "P": { "enum": [{ "type": [1, 2] }] } } }))
            .expect("enum values are data and are not inspected as schemas");
    }

    #[test]
    fn validate_schema_rejects_a_property_carrying_both_enum_representations() {
        // A schema stating both forms for one property is reachable from caller
        // input: the compiler parses both keywords, and the validator evaluates
        // them independently, which would report one value against two different
        // allowed-value lists.
        let schema = compiled(json!({
            "properties": { "P": { "type": "string", "enum": ["A"], "enumCaseInsensitive": ["a"] } }
        }));
        let error = validate_schema(&schema).expect_err("both enum representations must be rejected");
        match error {
            SchemaOverlayError::ConflictingEnums { property, .. } => assert_eq!(property, "P"),
            other => panic!("expected a conflicting-enum error, got {other:?}"),
        }
    }

    #[test]
    fn validate_schema_finds_a_conflicting_enum_nested_in_a_definition() {
        let schema = compiled(json!({
            "definitions": {
                "D": { "properties": { "Inner": { "enum": ["A"], "enumCaseInsensitive": ["a"] } } }
            }
        }));
        let error = validate_schema(&schema).expect_err("a nested conflict must be rejected");
        match error {
            SchemaOverlayError::ConflictingEnums { property, .. } => {
                assert_eq!(property, "D.Inner", "the error must name the nested path")
            }
            other => panic!("expected a conflicting-enum error, got {other:?}"),
        }
    }

    #[test]
    fn validate_schema_rejects_a_self_referential_definition() {
        let schema = compiled(json!({
            "properties": { "P": { "$ref": "#/definitions/D" } },
            "definitions": { "D": { "$ref": "#/definitions/D" } }
        }));
        let error = validate_schema(&schema).expect_err("a self-referential definition must be rejected");
        match error {
            SchemaOverlayError::CyclicRef { cycle, .. } => assert!(cycle.contains(&"D".to_string()), "got {cycle:?}"),
            other => panic!("expected a cycle error, got {other:?}"),
        }
    }

    #[test]
    fn validate_schema_rejects_a_multi_node_definition_cycle() {
        let schema = compiled(json!({
            "definitions": {
                "A": { "$ref": "#/definitions/B" },
                "B": { "$ref": "#/definitions/C" },
                "C": { "$ref": "#/definitions/A" }
            }
        }));
        validate_schema(&schema).expect_err("a multi-node definition cycle must be rejected");
    }

    #[test]
    fn validate_schema_accepts_an_acyclic_ref_chain() {
        let schema = compiled(json!({
            "properties": { "P": { "$ref": "#/definitions/A" } },
            "definitions": {
                "A": { "$ref": "#/definitions/B" },
                "B": { "type": "object" }
            }
        }));
        validate_schema(&schema).expect("an acyclic chain is valid");
    }

    #[test]
    fn merge_adds_new_property_and_keeps_base() {
        let mut base =
            compiled(json!({ "properties": { "Handler": { "type": "string" } }, "additionalProperties": false }));
        merge_into(&mut base, compiled(json!({ "properties": { "NewThing": { "type": "object" } } })));
        assert!(base.properties.contains_key("Handler"), "bundled property must be retained");
        assert!(base.properties.contains_key("NewThing"), "overlay property must be added");
        assert_eq!(base.additional_properties, Some(false), "bundled additionalProperties must be retained");
    }

    #[test]
    fn merge_replaces_enum_for_property() {
        let mut base = compiled(json!({ "properties": { "Mode": { "type": "string", "enum": ["A", "B"] } } }));
        merge_into(&mut base, compiled(json!({ "properties": { "Mode": { "enum": ["A", "B", "C"] } } })));
        let values: Vec<&str> = base.properties["Mode"].enum_values.iter().filter_map(|v| v.as_str()).collect();
        assert_eq!(values, vec!["A", "B", "C"], "overlay enum must replace the bundled enum");
    }

    #[test]
    fn merge_keeps_case_insensitive_comparison_when_widening_with_a_plain_enum() {
        let mut base =
            compiled(json!({ "properties": { "Mode": { "type": "string", "enumCaseInsensitive": ["a", "b"] } } }));
        merge_into(&mut base, compiled(json!({ "properties": { "Mode": { "enum": ["a", "b", "c"] } } })));
        let prop = &base.properties["Mode"];
        assert!(prop.enum_values.is_empty(), "the exact list must stay empty so the value is not checked twice");
        let values: Vec<&str> = prop.enum_case_insensitive.iter().filter_map(|v| v.as_str()).collect();
        assert_eq!(values, vec!["a", "b", "c"], "widening must land on the case-insensitive list");
    }

    #[test]
    fn merge_case_insensitive_overlay_clears_a_bundled_exact_enum() {
        let mut base = compiled(json!({ "properties": { "Mode": { "type": "string", "enum": ["A", "B"] } } }));
        merge_into(&mut base, compiled(json!({ "properties": { "Mode": { "enumCaseInsensitive": ["a", "b"] } } })));
        let prop = &base.properties["Mode"];
        assert!(prop.enum_values.is_empty(), "the exact list must be cleared");
        assert_eq!(prop.enum_case_insensitive.len(), 2);
        validate_schema(&base).expect("the two enum representations must stay mutually exclusive");
    }

    #[test]
    fn merge_unions_required() {
        let mut base = compiled(json!({ "required": ["A"] }));
        merge_into(&mut base, compiled(json!({ "required": ["A", "B"] })));
        assert_eq!(
            base.required,
            vec!["A".to_string(), "B".to_string()],
            "required must be unioned without duplicates"
        );
    }

    #[test]
    fn merge_unions_property_path_metadata() {
        let mut base = compiled(json!({ "deprecatedProperties": ["/properties/Old"] }));
        merge_into(&mut base, compiled(json!({ "deprecatedProperties": ["/properties/New"] })));
        assert_eq!(
            base.deprecated_properties,
            vec!["Old".to_string(), "New".to_string()],
            "an additive metadata overlay must keep the bundled entries"
        );
    }

    #[test]
    fn merge_replaces_the_primary_identifier_tuple() {
        let mut base = compiled(json!({ "primaryIdentifier": ["/properties/Name"] }));
        merge_into(&mut base, compiled(json!({ "primaryIdentifier": ["/properties/Region", "/properties/Name"] })));
        assert_eq!(
            base.primary_identifier,
            vec!["Region".to_string(), "Name".to_string()],
            "the identifier tuple is replaced, not unioned into a different tuple"
        );
    }

    #[test]
    fn merge_replaces_logical_groups_instead_of_unioning_them() {
        let mut base = compiled(json!({ "requiredXor": ["A", "B"], "requiredOr": ["C", "D"] }));
        merge_into(&mut base, compiled(json!({ "requiredXor": ["A", "B", "E"], "requiredOr": ["C", "D", "F"] })));
        assert_eq!(base.required_xor, vec!["A".to_string(), "B".to_string(), "E".to_string()]);
        assert_eq!(base.required_or, vec!["C".to_string(), "D".to_string(), "F".to_string()]);
    }

    #[test]
    fn merge_keeps_logical_groups_when_the_overlay_omits_them() {
        let mut base = compiled(json!({ "requiredXor": ["A", "B"] }));
        merge_into(&mut base, compiled(json!({ "properties": { "P": { "type": "string" } } })));
        assert_eq!(base.required_xor, vec!["A".to_string(), "B".to_string()]);
    }

    #[test]
    fn merge_unions_dependency_lists_per_trigger() {
        let mut base = compiled(json!({ "dependentRequired": { "A": ["B", "C"] } }));
        merge_into(
            &mut base,
            compiled(json!({ "dependentRequired": { "A": ["D"] }, "dependentExcluded": { "E": ["F"] } })),
        );
        assert_eq!(
            base.dependent_required.get("A"),
            Some(&vec!["B".to_string(), "C".to_string(), "D".to_string()]),
            "an overlay entry for an existing trigger must extend, not replace"
        );
        assert_eq!(base.dependent_excluded.get("E"), Some(&vec!["F".to_string()]));
    }

    #[test]
    fn merge_deep_merges_nested_properties() {
        let mut base = compiled(json!({
            "properties": {
                "Cfg": {
                    "type": "object",
                    "properties": { "X": { "type": "string" } },
                    "additionalProperties": false
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({ "properties": { "Cfg": { "properties": { "Y": { "type": "integer" } } } } })),
        );
        let cfg = &base.properties["Cfg"];
        assert!(cfg.properties.contains_key("X"), "nested bundled property must be retained");
        assert!(cfg.properties.contains_key("Y"), "nested overlay property must be added");
        assert_eq!(cfg.additional_properties, Some(false), "nested additionalProperties must be retained");
    }

    #[test]
    fn merge_deep_merges_pattern_property_values() {
        let mut base = compiled(json!({
            "properties": {
                "Map": {
                    "type": "object",
                    "patternProperties": {
                        "^k$": { "type": "object", "properties": { "X": { "type": "string" } } }
                    }
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "Map": { "patternProperties": { "^k$": { "properties": { "Y": { "type": "string" } } } } }
                }
            })),
        );
        let value = &base.properties["Map"].pattern_properties["^k$"];
        assert!(value.properties.contains_key("X"), "the bundled pattern value schema must be retained");
        assert!(value.properties.contains_key("Y"), "the overlay pattern value schema must be merged in");
    }

    #[test]
    fn merge_prop_overrides_all_scalar_constraints() {
        let mut base = compiled(json!({ "properties": { "P": { "type": "string" } } }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "P": {
                        "type": "string",
                        "pattern": "^a+$",
                        "minLength": 1, "maxLength": 10,
                        "minimum": 0.0, "maximum": 100.0,
                        "exclusiveMinimum": 1.0, "exclusiveMaximum": 99.0,
                        "minItems": 1, "maxItems": 5,
                        "uniqueItems": true,
                        "minProperties": 1, "maxProperties": 3,
                        "format": "uri",
                        "const": "a",
                        "not": { "enum": ["bad"] },
                        "additionalProperties": false
                    }
                }
            })),
        );
        let p = &base.properties["P"];
        assert_eq!(p.pattern.as_deref(), Some("^a+$"));
        assert_eq!(p.min_length, Some(1));
        assert_eq!(p.max_length, Some(10));
        assert_eq!(p.minimum, Some(0.0));
        assert_eq!(p.maximum, Some(100.0));
        assert_eq!(p.exclusive_minimum, Some(1.0));
        assert_eq!(p.exclusive_maximum, Some(99.0));
        assert_eq!(p.min_items, Some(1));
        assert_eq!(p.max_items, Some(5));
        assert_eq!(p.unique_items, Some(true));
        assert_eq!(p.min_properties, Some(1));
        assert_eq!(p.max_properties, Some(3));
        assert_eq!(p.format.as_deref(), Some("uri"));
        assert_eq!(p.const_value, Some(json!("a")));
        assert_eq!(p.not_enum, vec![json!("bad")]);
        assert_eq!(p.additional_properties, Some(false));
    }

    #[test]
    fn merge_prop_explicit_unique_items_false_clears_bundled_true() {
        let mut base = compiled(json!({ "properties": { "Arr": { "type": "array", "uniqueItems": true } } }));
        merge_into(&mut base, compiled(json!({ "properties": { "Arr": { "uniqueItems": false } } })));
        assert_eq!(
            base.properties["Arr"].unique_items,
            Some(false),
            "an explicitly relaxed uniqueItems must override the bundled true"
        );
    }

    #[test]
    fn merge_prop_omitted_unique_items_keeps_bundled_true() {
        let mut base = compiled(json!({ "properties": { "Arr": { "type": "array", "uniqueItems": true } } }));
        merge_into(&mut base, compiled(json!({ "properties": { "Arr": { "minItems": 1 } } })));
        assert_eq!(base.properties["Arr"].unique_items, Some(true), "an omitted uniqueItems must inherit");
    }

    #[test]
    fn merge_prop_merges_items() {
        let mut base = compiled(json!({
            "properties": {
                "Arr": { "type": "array", "items": { "type": "string" } },
                "Empty": { "type": "array" }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "Arr": { "items": { "pattern": "^x$" } },
                    "Empty": { "items": { "type": "string" } }
                }
            })),
        );
        let items = base.properties["Arr"].items.as_ref().expect("items retained");
        assert_eq!(items.pattern.as_deref(), Some("^x$"), "nested items must be merged");
        assert!(base.properties["Empty"].items.is_some(), "items must be inserted when the base has none");
    }

    #[test]
    fn merge_prop_ref_overlay_updates_ref_preserving_base_constraints() {
        let mut base = compiled(json!({
            "properties": { "P": { "type": "string", "pattern": "^a", "maxLength": 10 } },
            "definitions": { "D": { "type": "object" } }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": { "P": { "$ref": "#/definitions/D" } },
                "definitions": { "D": { "type": "object" } }
            })),
        );
        assert_eq!(base.properties["P"].ref_name.as_deref(), Some("D"), "a $ref overlay must update the reference");
        assert_eq!(
            base.properties["P"].pattern.as_deref(),
            Some("^a"),
            "the base pattern must be preserved when a $ref overlay is applied"
        );
        assert_eq!(
            base.properties["P"].max_length,
            Some(10),
            "the base maxLength must be preserved when a $ref overlay is applied"
        );
    }

    /// The schema that actually applies to a top-level property, i.e. what
    /// validation sees: the `$ref` chain resolved and any fields stated beside the
    /// reference merged on top.
    fn effective(schema: &CompiledSchema, property: &str) -> PropSchema {
        schema.properties[property].resolve(&schema.definitions).into_owned()
    }

    #[test]
    fn constraint_only_overlay_on_a_ref_base_takes_effect() {
        let mut base = compiled(json!({
            "properties": { "P": { "$ref": "#/definitions/D" } },
            "definitions": { "D": { "type": "string", "enum": ["A", "B"] } }
        }));
        merge_into(&mut base, compiled(json!({ "properties": { "P": { "enum": ["A", "B", "C"] } } })));
        assert_eq!(
            base.properties["P"].ref_name.as_deref(),
            Some("D"),
            "the reference stays live so a later change to the definition still reaches the property"
        );
        let p = effective(&base, "P");
        assert_eq!(p.enum_values.len(), 3, "the overlay enum must take effect");
        assert_eq!(
            p.prop_type.as_ref().and_then(crate::compiled::PropType::primary),
            Some("string"),
            "the referenced definition's own constraints must be preserved"
        );
    }

    #[test]
    fn overlay_on_a_ref_base_follows_the_whole_chain() {
        let mut base = compiled(json!({
            "properties": { "P": { "$ref": "#/definitions/Level1" } },
            "definitions": {
                "Level1": { "$ref": "#/definitions/Level2" },
                "Level2": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": { "Old": { "type": "string" } }
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({ "properties": { "P": { "properties": { "New": { "type": "integer" } } } } })),
        );
        let p = effective(&base, "P");
        assert!(p.properties.contains_key("Old"), "the terminal definition's properties must be preserved");
        assert!(p.properties.contains_key("New"), "the overlay property must be merged in");
        assert_eq!(p.additional_properties, Some(false), "the terminal definition's constraints must be preserved");
    }

    #[test]
    fn merge_no_op_overlay_leaves_a_ref_base_untouched() {
        let mut base = compiled(json!({
            "properties": { "P": { "$ref": "#/definitions/D" } },
            "definitions": { "D": { "type": "object" } }
        }));
        merge_into(&mut base, compiled(json!({ "properties": { "P": {} } })));
        assert_eq!(
            base.properties["P"].ref_name.as_deref(),
            Some("D"),
            "an overlay with nothing to say must not rewrite the property"
        );
    }

    #[test]
    fn a_ref_to_an_overlay_only_definition_resolves() {
        // The bundled definition `Config` references `Common`, which only the
        // overlay supplies.
        let mut base = compiled(json!({
            "properties": { "Cfg": { "$ref": "#/definitions/Config" } },
            "definitions": { "Config": { "$ref": "#/definitions/Common" } }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "definitions": {
                    "Common": { "type": "object", "required": ["FromCommon"] },
                    "Config": { "properties": { "Extra": { "type": "string" } } }
                }
            })),
        );
        let cfg = effective(&base, "Cfg");
        assert_eq!(
            cfg.required,
            vec!["FromCommon".to_string()],
            "the overlay-only definition's constraints must reach the property"
        );
        assert!(cfg.properties.contains_key("Extra"), "the overlay definition content must be merged in");
    }

    #[test]
    fn a_definition_updated_by_a_later_merge_reaches_an_already_extended_property() {
        // The regression that merge-time inlining caused: extending a `$ref`
        // property and then widening the definition it points at.
        let mut base = compiled(json!({
            "properties": { "P": { "$ref": "#/definitions/D" } },
            "definitions": { "D": { "type": "string", "enum": ["alpha", "beta"] } }
        }));
        merge_into(&mut base, compiled(json!({ "properties": { "P": { "maxLength": 16 } } })));
        merge_into(&mut base, compiled(json!({ "definitions": { "D": { "enum": ["alpha", "beta", "gamma"] } } })));
        let p = effective(&base, "P");
        assert_eq!(
            p.enum_values.len(),
            3,
            "the later definition update must reach the property, got {:?}",
            p.enum_values
        );
        assert_eq!(p.max_length, Some(16), "the earlier property extension must survive");
    }

    #[test]
    fn merge_into_overrides_schema_metadata() {
        let mut base = compiled(json!({ "additionalProperties": true }));
        merge_into(
            &mut base,
            compiled(json!({
                "additionalProperties": false,
                "replacementStrategy": "delete",
                "documentationUrl": "http://docs",
                "sourceUrl": "http://src"
            })),
        );
        assert_eq!(base.additional_properties, Some(false));
        assert_eq!(base.replacement_strategy.as_deref(), Some("delete"));
        assert_eq!(base.documentation_url.as_deref(), Some("http://docs"));
        assert_eq!(base.source_url.as_deref(), Some("http://src"));
    }

    #[test]
    fn merge_into_merges_existing_definition_and_inserts_new() {
        let mut base = compiled(json!({
            "definitions": {
                "D": { "type": "object", "properties": { "X": { "type": "string" } }, "additionalProperties": false }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "definitions": {
                    "D": { "properties": { "Y": { "type": "integer" } } },
                    "E": { "type": "string" }
                }
            })),
        );
        let d = &base.definitions["D"];
        assert!(d.properties.contains_key("X"), "existing definition property must be retained");
        assert!(d.properties.contains_key("Y"), "overlay definition property must be merged in");
        assert_eq!(d.additional_properties, Some(false), "existing definition metadata must be retained");
        assert!(base.definitions.contains_key("E"), "a new definition must be inserted");
    }

    #[test]
    fn merge_replaces_composition_keywords_instead_of_appending() {
        let mut base = compiled(json!({ "oneOf": [{ "required": ["A"] }, { "required": ["B"] }] }));
        assert_eq!(base.one_of.len(), 2);
        merge_into(&mut base, compiled(json!({ "oneOf": [{ "required": ["C"] }] })));
        assert_eq!(base.one_of.len(), 1, "overlay oneOf must replace the bundled oneOf");
        assert_eq!(base.one_of[0].required, vec!["C".to_string()]);
    }

    #[test]
    fn merge_replaces_both_halves_of_a_split_all_of() {
        // `allOf` compiles into plain entries plus conditional ones. An overlay
        // supplying only conditional entries must still clear the bundled plain
        // entries, or the merged composition matches neither schema.
        let mut base = compiled(json!({
            "allOf": [
                { "required": ["Plain"] },
                { "if": { "properties": { "A": { "enum": ["x"] } } }, "then": { "dependentRequired": { "A": ["B"] } } }
            ]
        }));
        assert_eq!(base.all_of.len(), 1);
        assert_eq!(base.if_then_else.len(), 1);
        merge_into(
            &mut base,
            compiled(json!({
                "allOf": [{ "if": { "properties": { "A": { "enum": ["y"] } } }, "then": { "dependentRequired": { "A": ["C"] } } }]
            })),
        );
        assert!(base.all_of.is_empty(), "the bundled plain allOf entry must not survive a replacing overlay");
        assert_eq!(base.if_then_else.len(), 1);
    }

    #[test]
    fn merge_keeps_composition_when_overlay_omits_it() {
        let mut base = compiled(json!({ "oneOf": [{ "required": ["A"] }] }));
        merge_into(&mut base, compiled(json!({ "properties": { "P": { "type": "string" } } })));
        assert_eq!(base.one_of.len(), 1, "bundled oneOf must be kept when the overlay omits it");
    }

    #[test]
    fn merge_prop_recurses_into_shared_nested_property() {
        let mut base = compiled(json!({
            "properties": {
                "Cfg": {
                    "type": "object",
                    "properties": { "Inner": { "type": "object", "properties": { "A": { "type": "string" } } } }
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "Cfg": { "properties": { "Inner": { "properties": { "B": { "type": "integer" } } } } }
                }
            })),
        );
        let inner = &base.properties["Cfg"].properties["Inner"].properties;
        assert!(inner.contains_key("A"), "deep bundled sub-property must be retained");
        assert!(inner.contains_key("B"), "deep overlay sub-property must be merged in");
    }

    #[test]
    fn merge_result_is_independent_of_map_iteration_order() {
        // Many definitions merged at once, one of them referenced by another: the
        // effective schema must not depend on which order the map happens to yield.
        let mut base_raw = serde_json::Map::new();
        for i in 0..32 {
            base_raw.insert(format!("D{i}"), json!({ "type": "object" }));
        }
        base_raw.insert("Holder".into(), json!({ "$ref": "#/definitions/Target" }));
        base_raw.insert("Target".into(), json!({ "type": "object" }));
        let mut overlay_raw = serde_json::Map::new();
        for i in 0..32 {
            overlay_raw.insert(format!("D{i}"), json!({ "maxProperties": 64 }));
        }
        overlay_raw.insert("Target".into(), json!({ "required": ["Late"] }));
        overlay_raw.insert("Holder".into(), json!({ "properties": { "X": { "type": "string" } } }));

        for _ in 0..8 {
            let mut base = compiled(json!({
                "properties": { "P": { "$ref": "#/definitions/Holder" } },
                "definitions": base_raw
            }));
            merge_into(&mut base, compiled(json!({ "definitions": overlay_raw })));
            let p = effective(&base, "P");
            assert_eq!(p.required, vec!["Late".to_string()], "the referenced definition's update must be visible");
            assert!(p.properties.contains_key("X"), "the referencing definition's own content must be visible");
        }
    }

    // -------------------------------------------------- keyword type validation

    #[test]
    fn compile_rejects_malformed_required_array() {
        let error = compile("AWS::Test::T", &json!({ "properties": { "P": { "type": "string" } }, "required": 42 }))
            .expect_err("non-array required must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_required_containing_non_strings() {
        let error =
            compile("AWS::Test::T", &json!({ "properties": { "P": { "type": "string" } }, "required": ["A", 42] }))
                .expect_err("required with non-string entries must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_invalid_property_pattern() {
        let error =
            compile("AWS::Test::T", &json!({ "properties": { "P": { "type": "string", "pattern": "^(unbalanced" } } }))
                .expect_err("an invalid regex in pattern must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_invalid_pattern_properties_regex() {
        let error = compile(
            "AWS::Test::T",
            &json!({ "properties": { "Map": { "type": "object", "patternProperties": { "^(bad": { "type": "string" } } } } }),
        )
        .expect_err("an invalid regex in patternProperties key must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_root_not_without_enum() {
        let error = compile(
            "AWS::Test::T",
            &json!({ "properties": { "P": { "type": "string", "not": { "pattern": "^x" } } } }),
        )
        .expect_err("'not' without 'enum' must be rejected as unsupported");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    // ------------------------------------------ composition rejection

    #[test]
    fn compile_rejects_composition_entry_with_ref() {
        compile(
            "AWS::Test::T",
            &json!({
                "definitions": { "D": { "type": "object" } },
                "allOf": [{ "$ref": "#/definitions/D" }]
            }),
        )
        .expect("a $ref inside a composition entry is accepted and resolved at validation time");
    }

    #[test]
    fn compile_accepts_composition_entry_with_scalar_constraint() {
        compile(
            "AWS::Test::T",
            &json!({
                "oneOf": [{ "required": ["A"], "minLength": 5 }]
            }),
        )
        .expect("scalar constraints inside composition entries are accepted and evaluated");
    }

    #[test]
    fn compile_accepts_conditional_allof_then_with_required() {
        compile(
            "AWS::Test::T",
            &json!({
                "allOf": [{
                    "if": { "properties": { "A": { "enum": ["x"] } } },
                    "then": { "required": ["B"] }
                }]
            }),
        )
        .expect("conditional then with 'required' is now supported");
    }

    #[test]
    fn compile_accepts_conditional_allof_then_with_dependent_required() {
        compile(
            "AWS::Test::T",
            &json!({
                "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                "allOf": [{
                    "if": { "properties": { "A": { "enum": ["x"] } } },
                    "then": { "dependentRequired": { "A": ["B"] } }
                }]
            }),
        )
        .expect("a conditional then with only dependentRequired must be accepted");
    }

    #[test]
    fn compile_rejects_direct_root_if_then_else() {
        let error = compile(
            "AWS::Test::T",
            &json!({
                "properties": { "A": { "type": "string" } },
                "if": { "properties": { "A": { "enum": ["x"] } } },
                "then": { "required": ["B"] }
            }),
        )
        .expect_err("direct root if/then/else must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_accepts_plain_composition_with_allowed_fields() {
        compile(
            "AWS::Test::T",
            &json!({
                "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                "oneOf": [{ "required": ["A"] }, { "required": ["B"] }]
            }),
        )
        .expect("composition with only required must be accepted");
    }

    // ------------------------------------------ deterministic conflict reporting

    #[test]
    fn conflicting_enum_check_reports_alphabetically_first_path() {
        let schema = compiled(json!({
            "properties": {
                "Zeta": { "type": "string", "enum": ["A"], "enumCaseInsensitive": ["a"] },
                "Alpha": { "type": "string", "enum": ["B"], "enumCaseInsensitive": ["b"] }
            }
        }));
        let error = validate_schema(&schema).expect_err("both conflicts must be detected");
        match error {
            SchemaOverlayError::ConflictingEnums { property, .. } => {
                assert_eq!(property, "Alpha", "the alphabetically first path must be reported");
            }
            other => panic!("expected conflicting-enum error, got {other:?}"),
        }
    }

    // ------------------------------------------ low-level compile whitespace/mismatch

    #[test]
    fn compile_rejects_leading_whitespace_in_type_name() {
        let error = compile(" AWS::Test::T", &json!({ "properties": { "P": { "type": "string" } } }))
            .expect_err("leading whitespace must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_trailing_whitespace_in_type_name() {
        let error = compile("AWS::Test::T ", &json!({ "properties": { "P": { "type": "string" } } }))
            .expect_err("trailing whitespace must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_type_name_mismatch_with_schema_typename() {
        let error = compile(
            "AWS::Test::A",
            &json!({ "typeName": "AWS::Test::B", "properties": { "P": { "type": "string" } } }),
        )
        .expect_err("a typeName mismatch must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    // ------------------------------------------ unrepresented keywords now error

    #[test]
    fn compile_rejects_unrepresented_constraint_keywords() {
        for keyword in ["propertyNames", "contains"] {
            let schema = json!({ "properties": { "P": { "type": "string", keyword: 42 } } });
            let error =
                compile("AWS::Test::T", &schema).expect_err(&format!("'{keyword}' must be rejected, not warned"));
            assert!(
                matches!(error, SchemaOverlayError::Unsupported { .. }),
                "expected Unsupported for '{keyword}', got {error:?}"
            );
        }
    }

    #[test]
    fn compile_accepts_multiple_of() {
        compile("AWS::Test::T", &json!({ "properties": { "P": { "type": "number", "multipleOf": 5 } } }))
            .expect("multipleOf is represented and enforced");
    }

    // ------------------------------------------ $ref preserves base constraints

    #[test]
    fn ref_overlay_preserves_base_pattern_and_max_length() {
        let mut base = compiled(json!({
            "properties": { "P": { "type": "string", "pattern": "^[a-z]+$", "maxLength": 50 } },
            "definitions": { "D": { "type": "string" } }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": { "P": { "$ref": "#/definitions/D" } },
                "definitions": { "D": { "type": "string" } }
            })),
        );
        let p = &base.properties["P"];
        assert_eq!(p.ref_name.as_deref(), Some("D"), "the reference must be set");
        assert_eq!(p.pattern.as_deref(), Some("^[a-z]+$"), "base pattern must be preserved");
        assert_eq!(p.max_length, Some(50), "base maxLength must be preserved");
    }

    #[test]
    fn compile_accepts_composition_properties() {
        compile(
            "AWS::Test::T",
            &json!({
                "oneOf": [{
                    "properties": { "A": { "enum": ["x"] } },
                    "required": ["A"],
                    "requiredOr": ["A", "B"],
                    "requiredXor": ["A", "C"]
                }]
            }),
        )
        .expect("composition branches with property constraints are now accepted and evaluated");
    }

    #[test]
    fn compile_rejects_conditional_fields_the_matcher_does_not_evaluate() {
        // anyOf inside a condition `if` block is still not supported (nested
        // condition schemas are not reachable from caller-supplied overlays).
        let error = compile(
            "AWS::Test::T",
            &json!({ "allOf": [{ "if": { "anyOf": [{ "required": ["A"] }] }, "then": { "dependentRequired": { "A": ["B"] } } }] }),
        )
        .expect_err("unsupported conditional fields must be rejected");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_accepts_conditional_with_maxlength_in_property() {
        compile(
            "AWS::Test::T",
            &json!({
                "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                "allOf": [{
                    "if": { "properties": { "A": { "maxLength": 3 } } },
                    "then": { "dependentRequired": { "A": ["B"] } }
                }]
            }),
        )
        .expect("maxLength in condition property is now accepted");
    }

    #[test]
    fn compile_accepts_condition_fields_the_matcher_evaluates() {
        compile(
            "AWS::Test::T",
            &json!({
                "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                "allOf": [{
                    "if": { "properties": { "A": { "type": "string", "enum": ["x"] } }, "required": ["A"] },
                    "then": { "dependentRequired": { "A": ["B"] } }
                }]
            }),
        )
        .expect("fully enforced conditional fields must remain supported");
    }

    #[test]
    fn compile_rejects_unknown_or_empty_property_types() {
        for prop_type in [json!("strng"), json!([])] {
            let error = compile("AWS::Test::T", &json!({ "properties": { "P": { "type": prop_type } } }))
                .expect_err("a type the validator cannot enforce must be rejected");
            assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
        }
    }

    #[test]
    fn compile_rejects_non_string_schema_type_name() {
        let error = compile("AWS::Test::T", &json!({ "typeName": 42, "properties": { "P": { "type": "string" } } }))
            .expect_err("typeName must be a string");
        assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
    }

    #[test]
    fn compile_rejects_malformed_property_metadata_paths() {
        for pointer in [json!("P"), json!("/properties/"), json!(42)] {
            let error = compile(
                "AWS::Test::T",
                &json!({
                    "properties": { "P": { "type": "string" } },
                    "readOnlyProperties": [pointer]
                }),
            )
            .expect_err("metadata paths must be valid property pointers");
            assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
        }
    }

    #[test]
    fn compile_rejects_malformed_not_enum() {
        for negated in [json!({ "enum": "x" }), json!({ "enum": [] }), json!({ "type": "string" })] {
            let error =
                compile("AWS::Test::T", &json!({ "properties": { "P": { "type": "string", "not": negated } } }))
                    .expect_err("only a non-empty not.enum array is enforceable");
            assert!(matches!(error, SchemaOverlayError::Unsupported { .. }), "got {error:?}");
        }
    }

    #[test]
    fn compile_rejects_constraints_beside_a_ref() {
        compile(
            "AWS::Test::T",
            &json!({
                "properties": { "P": { "$ref": "#/definitions/D", "maxLength": 3 } },
                "definitions": { "D": { "type": "string" } }
            }),
        )
        .expect("represented constraints beside a $ref are accepted and merged at validation time");
    }

    #[test]
    fn overlay_with_explicit_required_replaces_base_required() {
        let mut base = compiled(json!({
            "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
            "required": ["A", "B"]
        }));
        let overlay = compiled(json!({
            "properties": { "C": { "type": "string" } },
            "required": ["C"]
        }));
        merge_into(&mut base, overlay);
        assert_eq!(base.required, vec!["C".to_string()], "explicit required replaces, not unions");
    }

    #[test]
    fn overlay_with_empty_required_clears_base_required() {
        let mut base = compiled(json!({
            "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
            "required": ["A", "B"]
        }));
        let overlay = compiled(json!({
            "properties": { "C": { "type": "string" } },
            "required": []
        }));
        merge_into(&mut base, overlay);
        assert!(base.required.is_empty(), "explicit empty required clears the base's required list");
    }

    #[test]
    fn overlay_without_required_preserves_base_required() {
        let mut base = compiled(json!({
            "properties": { "A": { "type": "string" } },
            "required": ["A"]
        }));
        let overlay = compiled(json!({
            "properties": { "B": { "type": "string" } }
        }));
        merge_into(&mut base, overlay);
        assert_eq!(base.required, vec!["A".to_string()], "omitting required from overlay preserves base");
    }

    #[test]
    fn property_level_required_present_replaces() {
        let mut base = compiled(json!({
            "properties": {
                "Nested": { "type": "object", "properties": { "X": { "type": "string" } }, "required": ["X"] }
            }
        }));
        let overlay = compiled(json!({
            "properties": {
                "Nested": { "type": "object", "properties": { "Y": { "type": "string" } }, "required": ["Y"] }
            }
        }));
        merge_into(&mut base, overlay);
        assert_eq!(
            base.properties["Nested"].required,
            vec!["Y".to_string()],
            "property-level explicit required replaces base"
        );
    }

    #[test]
    fn states_nothing_rejects_description_only_overlay() {
        let error = compile("AWS::Test::DescOnly", &json!({ "description": "just a note" }))
            .expect_err("a description-only overlay carries no validatable constraint");
        assert!(matches!(error, SchemaOverlayError::NoEffect { .. }), "got {error:?}");
    }

    #[test]
    fn states_nothing_accepts_documentation_url_with_property() {
        compile(
            "AWS::Test::DocUrl",
            &json!({
                "properties": { "P": { "type": "string" } },
                "documentationUrl": "https://example.com"
            }),
        )
        .expect("documentation URL with a property is valid");
    }

    #[test]
    fn states_nothing_rejects_documentation_url_only() {
        let error = compile("AWS::Test::DocOnly", &json!({ "documentationUrl": "https://example.com" }))
            .expect_err("documentation URL alone has no validatable constraint");
        assert!(matches!(error, SchemaOverlayError::NoEffect { .. }), "got {error:?}");
    }

    #[test]
    fn required_present_set_when_raw_has_required_keyword() {
        let overlay = compiled(json!({
            "properties": { "A": { "type": "string" } },
            "required": ["A"]
        }));
        assert!(overlay.required_present, "required_present must be set when source has 'required'");
    }

    #[test]
    fn required_present_not_set_when_raw_omits_required() {
        let overlay = compiled(json!({
            "properties": { "A": { "type": "string" } }
        }));
        assert!(!overlay.required_present, "required_present must not be set when source omits 'required'");
    }

    #[test]
    fn required_present_propagates_into_pattern_properties() {
        // `required` stated inside a patternProperties value schema must carry
        // the same replacement semantics as every other schema position.
        let mut base = compiled(json!({
            "properties": {
                "Map": {
                    "type": "object",
                    "patternProperties": {
                        "^k$": { "type": "object", "required": ["A", "B"] }
                    }
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "Map": { "patternProperties": { "^k$": { "required": ["A"] } } }
                }
            })),
        );
        assert_eq!(
            base.properties["Map"].pattern_properties["^k$"].required,
            vec!["A".to_string()],
            "a stated required inside patternProperties must replace, not union"
        );
    }

    #[test]
    fn removed_required_reports_every_position_deterministically() {
        let base = compiled(json!({
            "properties": {
                "Cfg": { "type": "object", "required": ["Inner", "Kept"] },
                "List": { "type": "array", "items": { "type": "object", "required": ["Elem"] } },
                "Map": { "type": "object", "patternProperties": { "^k$": { "required": ["P"] } } }
            },
            "required": ["Cfg", "List"]
        }));
        let mut merged = base.clone();
        merge_into(
            &mut merged,
            compiled(json!({
                "properties": {
                    "Cfg": { "required": ["Kept"] },
                    "List": { "items": { "required": [] } },
                    "Map": { "patternProperties": { "^k$": { "required": [] } } }
                },
                "required": ["Cfg"]
            })),
        );
        let removals = removed_required(&base, &merged);
        assert_eq!(
            removals,
            vec![
                (String::new(), vec!["List".to_string()]),
                ("Cfg".to_string(), vec!["Inner".to_string()]),
                ("List[]".to_string(), vec!["Elem".to_string()]),
                ("Map<patternProperties>.^k$".to_string(), vec!["P".to_string()]),
            ],
            "every removal must be reported with its schema position, in sorted order"
        );
    }

    #[test]
    fn removed_required_is_empty_when_nothing_is_removed() {
        let base = compiled(json!({ "properties": { "A": { "type": "string" } }, "required": ["A"] }));
        let mut merged = base.clone();
        merge_into(
            &mut merged,
            compiled(json!({ "properties": { "B": { "type": "string" } }, "required": ["A", "B"] })),
        );
        assert!(removed_required(&base, &merged).is_empty(), "adding a requirement is not a removal");

        let mut unioned = base.clone();
        merge_into(&mut unioned, compiled(json!({ "properties": { "B": { "type": "string" } } })));
        assert!(removed_required(&base, &unioned).is_empty(), "an omitted 'required' keeps the base list");
    }

    #[test]
    fn nested_prop_required_or_makes_merge_non_no_op() {
        let overlay_prop = PropSchema { required_or: vec!["X".into(), "Y".into()], ..Default::default() };
        assert!(!is_no_op(&overlay_prop), "a PropSchema with required_or must not be a no-op");
    }

    #[test]
    fn nested_prop_required_xor_makes_merge_non_no_op() {
        let overlay_prop = PropSchema { required_xor: vec!["X".into(), "Y".into()], ..Default::default() };
        assert!(!is_no_op(&overlay_prop), "a PropSchema with required_xor must not be a no-op");
    }

    #[test]
    fn nested_required_or_replaces_on_merge() {
        let mut base = compiled(json!({
            "properties": {
                "Cfg": {
                    "type": "object",
                    "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                    "requiredOr": ["A", "B"]
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "Cfg": { "requiredOr": ["A", "B", "C"] }
                }
            })),
        );
        assert_eq!(
            base.properties["Cfg"].required_or,
            vec!["A".to_string(), "B".to_string(), "C".to_string()],
            "nested requiredOr must be replaced by overlay"
        );
    }

    #[test]
    fn nested_required_xor_replaces_on_merge() {
        let mut base = compiled(json!({
            "properties": {
                "Cfg": {
                    "type": "object",
                    "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                    "requiredXor": ["A", "B"]
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": {
                    "Cfg": { "requiredXor": ["A", "B", "C"] }
                }
            })),
        );
        assert_eq!(
            base.properties["Cfg"].required_xor,
            vec!["A".to_string(), "B".to_string(), "C".to_string()],
            "nested requiredXor must be replaced by overlay"
        );
    }

    #[test]
    fn nested_required_groups_preserved_when_overlay_omits_them() {
        let mut base = compiled(json!({
            "properties": {
                "Cfg": {
                    "type": "object",
                    "properties": { "A": { "type": "string" }, "B": { "type": "string" } },
                    "requiredOr": ["A", "B"],
                    "requiredXor": ["A", "B"]
                }
            }
        }));
        merge_into(
            &mut base,
            compiled(json!({
                "properties": { "Cfg": { "properties": { "C": { "type": "string" } } } }
            })),
        );
        assert_eq!(
            base.properties["Cfg"].required_or,
            vec!["A".to_string(), "B".to_string()],
            "nested requiredOr must be preserved when overlay omits it"
        );
        assert_eq!(
            base.properties["Cfg"].required_xor,
            vec!["A".to_string(), "B".to_string()],
            "nested requiredXor must be preserved when overlay omits it"
        );
    }
}