shape-schema 0.2.1

Schema-by-example validation: your schema looks like your data. The Rust port of shape, held to the canonical TypeScript by a shared corpus.
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
//! The walk: a node against a value, producing in place.

use crate::context::{Context, PathPart, State, Update, UpdateErr};
use crate::error::*;
use crate::node::{Kind, ListMode, Node, Validator};
use crate::stringify::stringify_node;
use crate::value::{is_integer, Map, Value};
use std::collections::HashMap;
use std::sync::Arc;

/// A cursor onto the value under validation: mutable when producing, shared
/// when only a verdict is wanted. Writes through a shared cursor are dropped,
/// so one walk serves both. A container's cursor is typed by its map or
/// vector once its kind is known.
pub(crate) enum Cur<'a, T = Value> {
    Mut(&'a mut T),
    Ref(&'a T),
}

impl<'a, T> Cur<'a, T> {
    fn get(&self) -> &T {
        match self {
            Cur::Mut(v) => v,
            Cur::Ref(v) => v,
        }
    }

    fn set(&mut self, nv: T) {
        if let Cur::Mut(v) = self {
            **v = nv;
        }
    }

    fn reborrow(&mut self) -> Cur<'_, T> {
        match self {
            Cur::Mut(v) => Cur::Mut(v),
            Cur::Ref(v) => Cur::Ref(v),
        }
    }
}

/// What a walk carries besides the value.
pub(crate) struct Walk<'c> {
    pub ctx: &'c mut Context,
    /// The schema's own definitions, borrowed for the walk.
    pub defs: &'c HashMap<String, Arc<Node>>,
    pub is_match: bool,
    /// The path from the root, the current key last.
    pub path: Vec<PathPart>,
    /// Whether the path is kept: a terse walk of a schema with no
    /// validators has nothing that reads it.
    pub paths: bool,
}

impl<'c> Walk<'c> {
    #[inline]
    fn push(&mut self, part: PathPart) {
        if self.paths {
            self.path.push(part);
        }
    }

    #[inline]
    fn pop(&mut self) {
        if self.paths {
            self.path.pop();
        }
    }
}

/// The digits of an index, written into a stack buffer: an element's key,
/// without an allocation per element.
fn index_key(i: usize, buf: &mut [u8; 20]) -> &str {
    let mut n = i;
    let mut pos = buf.len();
    loop {
        pos -= 1;
        buf[pos] = b'0' + (n % 10) as u8;
        n /= 10;
        if n == 0 {
            break;
        }
    }
    // SAFETY: the buffer holds ASCII digits only.
    unsafe { std::str::from_utf8_unchecked(&buf[pos..]) }
}

pub(crate) fn required_mark_for(k: Kind) -> i64 {
    match k {
        Kind::Object => MARK_OBJECT_REQUIRED,
        Kind::Array => MARK_ARRAY_REQUIRED,
        _ => MARK_SCALAR_REQUIRED,
    }
}

pub(crate) fn type_mark_for(k: Kind) -> i64 {
    match k {
        Kind::Object => MARK_OBJECT_TYPE,
        Kind::Array => MARK_ARRAY_TYPE,
        Kind::Check => MARK_CHECK_TYPE,
        _ => MARK_SCALAR_TYPE,
    }
}

/// Validate a node against the value at `cur`, writing the produced value
/// through it. Returns false when the slot should be left out of the
/// produced container: nothing was there and nothing was injected.
pub(crate) fn validate_node(
    n: &Node,
    cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    w: &mut Walk<'_>,
    verr: &mut ValidationError,
) -> bool {
    validate_node_with(
        n,
        &n.befores,
        &n.afters,
        n.silent,
        cur,
        key,
        parent_is_array,
        w,
        verr,
    )
}

/// `validate_node` with the checks and the silence given apart from the
/// node: an Ignore lifts the silence of the node it probes, and a Catch or
/// Transform runs the node with the checks it took inside.
#[allow(clippy::too_many_arguments)]
pub(crate) fn validate_node_with(
    n: &Node,
    befores: &[Validator],
    afters: &[Validator],
    silent: bool,
    mut cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    w: &mut Walk<'_>,
    verr: &mut ValidationError,
) -> bool {
    let absent = cur.get().is_undefined();
    // A before may replace the value or the node; a replacement value under a
    // shared cursor lives here.
    let mut over: Option<Value> = None;
    let mut node_over: Option<Arc<Node>> = None;
    let mut done = false;

    for b in befores {
        let cur_node: &Node = node_over.as_deref().unwrap_or(n);
        let mut update = Update::default();
        let ok = {
            let value: &Value = over.as_ref().unwrap_or_else(|| cur.get());
            let mut state = State {
                path_arr: &w.path,
                key,
                value,
                node: cur_node,
                parent_is_array,
                is_match: w.is_match,
                ctx: w.ctx,
                absent,
                check_name: &b.name,
                defs: w.defs,
            };
            (b.func)(&mut state, &mut update)
        };
        if let Some(v) = update.val.take() {
            match &mut cur {
                Cur::Mut(_) => cur.set(v),
                Cur::Ref(_) => over = Some(v),
            }
        }
        if let Some(nn) = update.node.take() {
            node_over = Some(nn);
        }
        let cur_node: &Node = node_over.as_deref().unwrap_or(n);
        let cur_silent = if node_over.is_some() {
            cur_node.silent
        } else {
            silent
        };
        let value_now: &Value = over.as_ref().unwrap_or_else(|| cur.get());
        if !ok && absent_skips(cur_node, value_now, absent, &update) {
            continue;
        }
        if !ok {
            let at = at_of(w, key, cur_node, value_now, parent_is_array, &b.name);
            emit_update_errors(&at, cur_node, cur_silent, &update, verr);
        }
        if !ok || update.done {
            done = true;
        }
    }

    let n: &Node = node_over.as_deref().unwrap_or(n);
    let silent = if node_over.is_some() {
        n.silent
    } else {
        silent
    };
    let mut kept = true;
    if !done {
        let inner = match over.as_mut() {
            Some(v) => Cur::Mut(v),
            None => cur.reborrow(),
        };
        kept = validate_structure(n, silent, inner, key, parent_is_array, absent, w, verr);
    }

    // Afters run whatever the structural check reported.
    for a in afters {
        let mut update = Update::default();
        let ok = {
            let value: &Value = over.as_ref().unwrap_or_else(|| cur.get());
            let mut state = State {
                path_arr: &w.path,
                key,
                value,
                node: n,
                parent_is_array,
                is_match: w.is_match,
                ctx: w.ctx,
                absent,
                check_name: &a.name,
                defs: w.defs,
            };
            (a.func)(&mut state, &mut update)
        };
        if let Some(v) = update.val.take() {
            kept = true;
            match &mut cur {
                Cur::Mut(_) => cur.set(v),
                Cur::Ref(_) => over = Some(v),
            }
        }
        let value_now: &Value = over.as_ref().unwrap_or_else(|| cur.get());
        if !ok && absent_skips(n, value_now, absent, &update) {
            continue;
        }
        if !ok {
            let at = at_of(w, key, n, value_now, parent_is_array, &a.name);
            emit_update_errors(&at, n, silent, &update, verr);
        }
    }

    kept
}

/// A failed check against an absent value on a node that does not require
/// one raises nothing, unless the check insisted with `done`.
fn absent_skips(n: &Node, value: &Value, absent: bool, update: &Update) -> bool {
    absent && value.is_undefined() && (n.skippable || !n.required) && !update.done
}

/// The location of an error. A value renders as "undefined" when it is
/// undefined at the time of the error, as the canonical `s.val` does: an
/// injected default is the value a later check reports.
pub(crate) fn at_of<'a>(
    w: &'a Walk<'_>,
    key: &'a str,
    n: &Node,
    value: &'a Value,
    parent_is_array: bool,
    check: &'a str,
) -> At<'a> {
    At {
        path: &w.path,
        key,
        kind: n.kind,
        value,
        parent_arr: parent_is_array,
        absent: value.is_undefined(),
        check,
        // Rendered only when the error will be: a terse one is counted.
        regexp_src: if w.ctx.terse {
            None
        } else {
            n.regexp.as_ref().map(|_| format!("/{}/", n.regexp_src))
        },
        terse: w.ctx.terse,
    }
}

fn emit_update_errors(
    at: &At<'_>,
    n: &Node,
    silent: bool,
    update: &Update,
    verr: &mut ValidationError,
) {
    if silent {
        return;
    }
    let why = update.why.as_deref().unwrap_or("");
    match &update.err {
        None => {
            let why = if why.is_empty() { WHY_CHECK } else { why };
            let mark = if update.mark == 0 {
                MARK_CUSTOM_CHECK_ERR
            } else {
                update.mark
            };
            let mut err = make_err(at, why, mark, "");
            if let Some(f) = &n.fault_msg {
                if !err.terse {
                    err.text = expand_err_text(f, &err.path, at.value, at.absent);
                }
            }
            verr.add(err);
        }
        Some(UpdateErr::Text(t)) => {
            let why = if why.is_empty() { WHY_CHECK } else { why };
            let mark = if update.mark == 0 {
                MARK_CUSTOM_CHECK_TEXT
            } else {
                update.mark
            };
            verr.add(make_err(at, why, mark, t));
        }
        Some(UpdateErr::Field(e)) => {
            let mut e = (**e).clone();
            fill_field(&mut e, at);
            verr.add(e);
        }
        Some(UpdateErr::Fields(es)) => {
            for e in es {
                let mut e = e.clone();
                fill_field(&mut e, at);
                verr.add(e);
            }
        }
    }
}

fn fill_field(e: &mut FieldError, at: &At<'_>) {
    if e.path.is_empty() {
        e.path = crate::context::join_path(at.path);
    }
    if e.mark == 0 {
        e.mark = MARK_CUSTOM_CHECK_TEXT;
    }
}

fn add_structural(
    at: &At<'_>,
    n: &Node,
    silent: bool,
    why: &str,
    mark: i64,
    verr: &mut ValidationError,
) {
    let mut err = make_err(at, why, mark, "");
    if let Some(f) = &n.fault_msg {
        if !err.terse {
            err.text = expand_err_text(f, &err.path, at.value, at.absent);
        }
    }
    if !silent {
        verr.add(err);
    }
}

pub(crate) fn emit_type_err(
    w: &Walk<'_>,
    key: &str,
    n: &Node,
    silent: bool,
    value: &Value,
    parent_is_array: bool,
    verr: &mut ValidationError,
) {
    let at = at_of(w, key, n, value, parent_is_array, "");
    add_structural(&at, n, silent, WHY_TYPE, type_mark_for(n.kind), verr);
}

/// The structural checks: composition, nullable, never, the missing-value
/// rules and the kind check.
#[allow(clippy::too_many_arguments)]
fn validate_structure(
    n: &Node,
    silent: bool,
    mut cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    absent: bool,
    w: &mut Walk<'_>,
    verr: &mut ValidationError,
) -> bool {
    if n.kind == Kind::List && n.list_mode != ListMode::None && (n.required || !absent) {
        let before = verr.count();
        let kept = evaluate_list(
            n,
            silent,
            cur.reborrow(),
            key,
            parent_is_array,
            absent,
            w,
            verr,
        );
        // A required composition that passed but left an absent subject
        // absent (All produces nothing from it) reports the missing value,
        // as TypeScript's required check does after the validators.
        if absent && n.required && verr.count() == before && cur.get().is_undefined() {
            let at = at_of(w, key, n, cur.get(), parent_is_array, "");
            add_structural(
                &at,
                n,
                silent,
                WHY_REQUIRED,
                required_mark_for(n.kind),
                verr,
            );
            return false;
        }
        return kept;
    }

    // Nullable: an explicit null is the value.
    if cur.get().is_null() && n.nullable {
        return true;
    }

    // Never rejects a value present or absent.
    if n.kind == Kind::Never {
        let at = at_of(w, key, n, cur.get(), parent_is_array, "");
        add_structural(&at, n, silent, WHY_NEVER, MARK_NEVER, verr);
        return !absent;
    }

    // A regexp never reports "required": an absent value is a non-string.
    if n.kind == Kind::Regexp && absent && !n.required {
        return false;
    }

    // Missing: required error, skip, inject the default, or descend into an
    // empty container so that required descendants still raise.
    if absent && n.kind != Kind::Regexp {
        if n.required {
            let at = at_of(w, key, n, cur.get(), parent_is_array, "");
            add_structural(
                &at,
                n,
                silent,
                WHY_REQUIRED,
                required_mark_for(n.kind),
                verr,
            );
            return false;
        }
        if n.skippable {
            return false;
        }
        if !n.has_default && (n.kind == Kind::Object || n.kind == Kind::Array) {
            cur.set(if n.kind == Kind::Array {
                Value::Arr(Vec::new())
            } else {
                Value::Obj(Default::default())
            });
            if matches!(cur, Cur::Ref(_)) {
                // Nothing to write into: probe a scratch container so that
                // the descendants are still judged.
                let mut scratch = if n.kind == Kind::Array {
                    Value::Arr(Vec::new())
                } else {
                    Value::Obj(Default::default())
                };
                return validate_structure(
                    n,
                    silent,
                    Cur::Mut(&mut scratch),
                    key,
                    parent_is_array,
                    false,
                    w,
                    verr,
                );
            }
        } else {
            if n.default.is_undefined() {
                // Nothing to inject: the slot stays empty.
                return false;
            }
            cur.set(n.default.clone());
            return true;
        }
    }

    let value = cur.get();
    match n.kind {
        Kind::Any | Kind::Check => {}
        Kind::Regexp => {
            let Some(s) = value.as_str() else {
                let at = at_of(w, key, n, value, parent_is_array, "");
                let mut err = make_err(&at, WHY_TYPE, MARK_SCALAR_TYPE, "");
                err.kind = Kind::String;
                if !err.terse {
                    err.text = default_err_text(&err);
                    if let Some(f) = &n.fault_msg {
                        err.text = expand_err_text(f, &err.path, value, absent);
                    }
                }
                if !silent {
                    verr.add(err);
                }
                return true;
            };
            let matched = n.regexp.as_ref().is_some_and(|re| re.is_match(s));
            if !matched {
                let at = at_of(w, key, n, value, parent_is_array, "");
                add_structural(&at, n, silent, WHY_REGEXP, MARK_REGEXP, verr);
            }
        }
        Kind::String => match value {
            Value::Str(s) => {
                if s.is_empty() && !n.empty {
                    let at = at_of(w, key, n, value, parent_is_array, "");
                    add_structural(&at, n, silent, WHY_REQUIRED, MARK_UNDEF_REQUIRED, verr);
                }
            }
            _ => emit_type_err(w, key, n, silent, value, parent_is_array, verr),
        },
        Kind::Number => match value {
            Value::Num(x) if !x.is_nan() => {}
            _ => emit_type_err(w, key, n, silent, value, parent_is_array, verr),
        },
        Kind::Integer => match value {
            Value::Num(x) if is_integer(*x) => {}
            _ => emit_type_err(w, key, n, silent, value, parent_is_array, verr),
        },
        Kind::Boolean => {
            if !matches!(value, Value::Bool(_)) {
                emit_type_err(w, key, n, silent, value, parent_is_array, verr);
            }
        }
        Kind::Date => {
            if !matches!(value, Value::Date(_)) {
                emit_type_err(w, key, n, silent, value, parent_is_array, verr);
            }
        }
        Kind::BigInt => {
            if !matches!(value, Value::BigInt(_)) {
                emit_type_err(w, key, n, silent, value, parent_is_array, verr);
            }
        }
        Kind::NaN => match value {
            Value::Num(x) if x.is_nan() => {}
            _ => emit_type_err(w, key, n, silent, value, parent_is_array, verr),
        },
        Kind::Null => {
            if !value.is_null() {
                emit_type_err(w, key, n, silent, value, parent_is_array, verr);
            }
        }
        Kind::Function => {
            if !matches!(value, Value::Func(_)) {
                emit_type_err(w, key, n, silent, value, parent_is_array, verr);
            }
        }
        Kind::Array => return validate_array(n, silent, cur, key, parent_is_array, w, verr),
        Kind::Object => return validate_object(n, silent, cur, key, parent_is_array, w, verr),
        Kind::Never | Kind::List => {}
    }
    true
}

/// Whether a present value passes a plain scalar node with nothing left to
/// do: the structural check alone, with no error to make. Anything else
/// takes the full walk, which reports it.
#[inline]
fn plain_scalar_passes(n: &Node, v: &Value) -> bool {
    match (n.kind, v) {
        (Kind::String, Value::Str(s)) => n.empty || !s.is_empty(),
        (Kind::Number, Value::Num(x)) => !x.is_nan(),
        (Kind::Integer, Value::Num(x)) => is_integer(*x),
        (Kind::Boolean, Value::Bool(_)) => true,
        _ => false,
    }
}

/// The key of the child at `idx`, shared with the node once it is prepared;
/// a node that never was allocates it.
fn child_key(n: &Node, idx: usize, k: &str) -> Arc<str> {
    match n.obj_keys.get(idx) {
        Some(key) => Arc::clone(key),
        None => Arc::from(k),
    }
}

/// Whether the input's keys are the declared keys in declaration order, or
/// a prefix of them: then none is unknown, and each child sits at its own
/// index. The usual case for JSON written from the same shape, and a
/// string comparison per key rather than a hash lookup.
fn aligned(n: &Node, map: &Map) -> bool {
    map.len() <= n.obj_keys.len()
        && map
            .keys()
            .zip(&n.obj_keys)
            .all(|(k, dk)| k.as_str() == &**dk)
}

/// The child at `k`, guessed at its declared position first: the input
/// written from the same shape has it there, and the guess is a short
/// comparison rather than a hash lookup. Any other order looks it up.
fn guess<'m>(map: &'m Map, idx: usize, k: &str) -> Option<&'m Value> {
    match map.get_index(idx) {
        Some((mk, v)) if mk == k => Some(v),
        _ => map.get(k),
    }
}

/// The index of the child at `k`, guessed as `guess` does.
fn guess_index(map: &Map, idx: usize, k: &str) -> Option<usize> {
    match map.get_index(idx) {
        Some((mk, _)) if mk == k => Some(idx),
        _ => map.get_index_of(k),
    }
}

/// The keys of a closed object it does not consume, in the input's order.
fn unknown_keys(n: &Node, map: &Map) -> Vec<String> {
    map.iter()
        .filter(|(k, v)| !v.is_undefined() && !n.consumed.contains(*k))
        .map(|(k, _)| k.clone())
        .collect()
}

fn validate_object(
    n: &Node,
    silent: bool,
    cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    w: &mut Walk<'_>,
    verr: &mut ValidationError,
) -> bool {
    // Unknown keys are reported before descending, in one message, in the
    // input's order.
    let aligned = cur.get().as_obj().is_some_and(|map| aligned(n, map));
    if let Some(map) = cur.get().as_obj() {
        if !n.is_open() && !aligned {
            let unknown = unknown_keys(n, map);
            if !unknown.is_empty() {
                let value = cur.get();
                let joined = unknown.join(", ");
                let at = at_of(w, &joined, n, value, parent_is_array, "");
                let mut err = make_err(&at, WHY_CLOSED, MARK_OBJECT_CLOSED, "");
                err.plural = unknown.len() > 1;
                if !err.terse {
                    err.text = default_err_text(&err);
                    if let Some(f) = &n.fault_msg {
                        err.text = expand_err_text(f, &err.path, value, false);
                    }
                }
                if !silent {
                    verr.add(err);
                }
            }
        }
    }

    // The container is the wrong type: its declared keys are meaningless,
    // so no descent.
    let mut oc: Cur<'_, Map> = match cur {
        Cur::Mut(Value::Obj(m)) => Cur::Mut(m),
        Cur::Ref(Value::Obj(m)) => Cur::Ref(m),
        other => {
            emit_type_err(w, key, n, silent, other.get(), parent_is_array, verr);
            return true;
        }
    };

    for (idx, (k, cn)) in n.obj_children.iter().enumerate() {
        // A plain child with a present value of its kind: a scalar is judged
        // here, and a read-only walk descends into a container directly,
        // without the general walk's bookkeeping.
        if cn.plain {
            let found = match &oc {
                Cur::Ref(map) if aligned => map.get_index(idx).map(|(_, v)| v),
                Cur::Ref(map) => guess(map, idx, k),
                Cur::Mut(map) => guess(map, idx, k),
            };
            if let Some(v) = found {
                if plain_scalar_passes(cn, v) {
                    continue;
                }
                if let Cur::Ref(_) = &oc {
                    let container = matches!(
                        (cn.kind, v),
                        (Kind::Object, Value::Obj(_)) | (Kind::Array, Value::Arr(_))
                    );
                    if container {
                        if w.paths {
                            w.push(PathPart::Key(child_key(n, idx, k)));
                        }
                        if cn.kind == Kind::Object {
                            validate_object(cn, cn.silent, Cur::Ref(v), k, false, w, verr);
                        } else {
                            validate_array(cn, cn.silent, Cur::Ref(v), k, false, w, verr);
                        }
                        w.pop();
                        continue;
                    }
                }
            }
        }

        // A rename's claim: the value is missing and a claimed source has
        // it, so it is picked up from there.
        let mut claimed: Option<&Value> = None;
        if cn.rename_to.is_some() && !cn.rename_claim.is_empty() {
            let present = oc.get().get(k).map(|v| !v.is_undefined()).unwrap_or(false);
            if !present {
                let src = cn
                    .rename_claim
                    .iter()
                    .find(|src| oc.get().contains_key(*src))
                    .cloned();
                if let Some(src) = src {
                    match &mut oc {
                        Cur::Mut(map) => {
                            let v = if cn.rename_keep {
                                map.get(&src).cloned().unwrap()
                            } else {
                                map.shift_remove(&src).unwrap()
                            };
                            map.insert(k.clone(), v);
                        }
                        Cur::Ref(map) => claimed = map.get(&src),
                    }
                }
            }
        }

        // The shared key is only taken when the path is kept.
        if w.paths {
            w.push(PathPart::Key(child_key(n, idx, k)));
        }
        // The scratch slot of an absent child on a read-only walk, made
        // only on that path.
        let mut scratch: Value;
        let (child, was_absent) = match &mut oc {
            Cur::Mut(map) => match guess_index(map, idx, k) {
                // The key is cloned only when it is absent and must be made.
                Some(pos) => (Cur::Mut(&mut map[pos]), false),
                None => (
                    Cur::Mut(map.entry(k.clone()).or_insert(Value::Undefined)),
                    true,
                ),
            },
            Cur::Ref(map) => {
                // A read-only walk of an aligned input finds the child by
                // index; the map cannot have changed under it.
                let found = if aligned {
                    map.get_index(idx).map(|(_, v)| v)
                } else {
                    claimed.or_else(|| guess(map, idx, k))
                };
                match found {
                    Some(child) if !child.is_undefined() => (Cur::Ref(child), false),
                    _ => {
                        scratch = Value::Undefined;
                        (Cur::Mut(&mut scratch), true)
                    }
                }
            }
        };
        let keep = if cn.is_ignore() {
            validate_ignored(cn, child, k, false, w, verr)
        } else {
            validate_node(cn, child, k, false, w, verr)
        };
        w.pop();
        if let Cur::Mut(m) = &mut oc {
            if !keep {
                let empty = m.get(k).map(|x| x.is_undefined()).unwrap_or(false);
                if was_absent || empty || cn.is_ignore() {
                    m.shift_remove(k);
                }
            } else if let Some(to) = &cn.rename_to {
                // The produced value moves under the new name.
                if to != k {
                    let v = if cn.rename_keep {
                        m.get(k).cloned()
                    } else {
                        m.shift_remove(k)
                    };
                    if let Some(v) = v {
                        m.insert(to.clone(), v);
                    }
                }
            }
        }
    }

    // An aligned read-only walk has nothing left over.
    let rest_keys = !(aligned && matches!(oc, Cur::Ref(_)));
    if let Some(rest) = n.obj_rest.as_deref().filter(|_| rest_keys) {
        let extra: Vec<String> = oc
            .get()
            .iter()
            .filter(|(k, v)| !v.is_undefined() && !n.obj_children.contains_key(*k))
            .map(|(k, _)| k.clone())
            .collect();
        for k in extra {
            w.push(PathPart::Key(Arc::from(k.as_str())));
            let child = match &mut oc {
                Cur::Mut(map) => Cur::Mut(map.get_mut(&k).unwrap()),
                Cur::Ref(map) => Cur::Ref(map.get(&k).unwrap()),
            };
            let keep = if rest.is_ignore() {
                validate_ignored(rest, child, &k, false, w, verr)
            } else {
                validate_node(rest, child, &k, false, w, verr)
            };
            w.pop();
            if !keep {
                if let Cur::Mut(m) = &mut oc {
                    m.shift_remove(&k);
                }
            }
        }
    }
    true
}

fn validate_array(
    n: &Node,
    silent: bool,
    cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    w: &mut Walk<'_>,
    verr: &mut ValidationError,
) -> bool {
    let fixed = n.arr_children.len();
    let has_child = n.arr_child.is_some();

    // A closed tuple with extra elements: one error naming the first index
    // beyond the tuple, and no element is validated.
    if let Some(arr) = cur.get().as_arr() {
        if fixed > 0 && arr.len() > fixed && !has_child && n.arr_rest.is_none() {
            let value = cur.get();
            let k = fixed.to_string();
            let at = at_of(w, &k, n, value, parent_is_array, "");
            let mut err = make_err(&at, WHY_CLOSED, MARK_ARRAY_CLOSED, "");
            if !err.terse {
                if let Some(f) = &n.fault_msg {
                    err.text = expand_err_text(f, &err.path, value, false);
                }
            }
            if !silent {
                verr.add(err);
            }
            return true;
        }
    }

    let mut ac: Cur<'_, Vec<Value>> = match cur {
        Cur::Mut(Value::Arr(a)) => Cur::Mut(a),
        Cur::Ref(Value::Arr(a)) => Cur::Ref(a),
        other => {
            emit_type_err(w, key, n, silent, other.get(), parent_is_array, verr);
            return true;
        }
    };
    let len = ac.get().len();

    let mut buf = [0u8; 20];
    let mut i = 0usize;
    for cn in &n.arr_children {
        let k = index_key(i, &mut buf);
        w.push(PathPart::Index(i));
        let mut scratch = Value::Undefined;
        let (child, appended) = match &mut ac {
            Cur::Mut(a) => {
                let appended = i >= a.len();
                if appended {
                    a.push(Value::Undefined);
                }
                (Cur::Mut(&mut a[i]), appended)
            }
            Cur::Ref(a) => match a.get(i) {
                Some(e) if !e.is_undefined() => (Cur::Ref(e), false),
                _ => (Cur::Mut(&mut scratch), true),
            },
        };
        let keep = if cn.is_ignore() {
            validate_ignored(cn, child, k, true, w, verr)
        } else {
            validate_node(cn, child, k, true, w, verr)
        };
        w.pop();
        if !keep && !appended && cn.is_ignore() {
            if let Cur::Mut(a) = &mut ac {
                a[i] = Value::Undefined;
            }
        }
        i += 1;
    }
    // Positions appended for missing tuple slots that produced nothing are
    // holes at the end; drop them.
    if let Cur::Mut(a) = &mut ac {
        while a.len() > len && a.last().map(|x| x.is_undefined()).unwrap_or(false) {
            a.pop();
        }
    }

    if let Some(child_shape) = n.arr_child.as_deref().or(n.arr_rest.as_deref()) {
        let mut drop_at: Vec<usize> = Vec::new();
        while i < len {
            // A plain element shape: a scalar element of its kind is judged
            // here on either walk (it is its own produced value), and a
            // read-only walk descends into an object directly.
            if child_shape.plain {
                if plain_scalar_passes(child_shape, &ac.get()[i]) {
                    i += 1;
                    continue;
                }
                if let Cur::Ref(a) = &ac {
                    let v = &a[i];
                    if child_shape.kind == Kind::Object && matches!(v, Value::Obj(_)) {
                        let k = if w.paths { index_key(i, &mut buf) } else { "" };
                        w.push(PathPart::Index(i));
                        validate_object(
                            child_shape,
                            child_shape.silent,
                            Cur::Ref(v),
                            k,
                            true,
                            w,
                            verr,
                        );
                        w.pop();
                        i += 1;
                        continue;
                    }
                }
            }
            let k = index_key(i, &mut buf);
            w.push(PathPart::Index(i));
            let child = match &mut ac {
                Cur::Mut(a) => Cur::Mut(&mut a[i]),
                Cur::Ref(a) => Cur::Ref(&a[i]),
            };
            let keep = if child_shape.is_ignore() {
                validate_ignored(child_shape, child, k, true, w, verr)
            } else {
                validate_node(child_shape, child, k, true, w, verr)
            };
            w.pop();
            if !keep && child_shape.is_ignore() {
                drop_at.push(i);
            }
            i += 1;
        }
        if let Cur::Mut(a) = &mut ac {
            for idx in drop_at {
                a[idx] = Value::Undefined;
            }
        }
    }
    true
}

/// An `Ignore` node: run it with its silence lifted into a collector of its
/// own, and drop the value when anything failed.
pub(crate) fn validate_ignored(
    n: &Node,
    cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    w: &mut Walk<'_>,
    _verr: &mut ValidationError,
) -> bool {
    let mut sub = ValidationError::default();
    let kept = validate_node_with(
        n,
        &n.befores,
        &n.afters,
        false,
        cur,
        key,
        parent_is_array,
        w,
        &mut sub,
    );
    !sub.has_any() && kept
}

/// Whether a branch accepts the value, without producing or rendering.
fn branch_passes(
    sn: &Node,
    value: &Value,
    key: &str,
    parent_is_array: bool,
    w: &mut Walk<'_>,
) -> bool {
    let saved = (w.is_match, w.ctx.terse);
    w.is_match = true;
    w.ctx.terse = true;
    let mut sub = ValidationError {
        terse: true,
        ..Default::default()
    };
    validate_node(sn, Cur::Ref(value), key, parent_is_array, w, &mut sub);
    w.is_match = saved.0;
    w.ctx.terse = saved.1;
    !sub.has_any()
}

#[allow(clippy::too_many_arguments)]
fn list_error(
    n: &Node,
    silent: bool,
    value: &Value,
    key: &str,
    parent_is_array: bool,
    w: &Walk<'_>,
    verr: &mut ValidationError,
    why: &str,
    mark: i64,
    words: &str,
    fault_applies: bool,
) {
    let names: Vec<String> = n.list.iter().map(|sn| stringify_node(sn, true)).collect();
    let at = at_of(w, key, n, value, parent_is_array, "");
    let text = format!(
        "Value \"$VALUE\" for property \"$PATH\" does not satisfy {}: {}",
        words,
        names.join(", ")
    );
    let mut err = make_err(&at, why, mark, &text);
    if fault_applies && !err.terse {
        if let Some(f) = &n.fault_msg {
            err.text = expand_err_text(f, &err.path, value, at.absent);
        }
    }
    if !silent {
        verr.add(err);
    }
}

/// One, Some and All. The branches see the value as the parent saw it: an
/// absent value stays absent, so a branch that does not require one can
/// still match and supply its default.
#[allow(clippy::too_many_arguments)]
fn evaluate_list(
    n: &Node,
    silent: bool,
    mut cur: Cur<'_>,
    key: &str,
    parent_is_array: bool,
    _absent: bool,
    w: &mut Walk<'_>,
    verr: &mut ValidationError,
) -> bool {
    match n.list_mode {
        ListMode::One => {
            let winner = n
                .list
                .iter()
                .position(|sn| branch_passes(sn, cur.get(), key, parent_is_array, w));
            match winner {
                Some(i) => {
                    // The value is produced by the branch that took it.
                    let mut sub = ValidationError::default();
                    validate_node(
                        &n.list[i],
                        cur.reborrow(),
                        key,
                        parent_is_array,
                        w,
                        &mut sub,
                    )
                }
                None => {
                    list_error(
                        n,
                        silent,
                        cur.get(),
                        key,
                        parent_is_array,
                        w,
                        verr,
                        "One",
                        4030,
                        "one of",
                        true,
                    );
                    true
                }
            }
        }
        ListMode::Some => {
            // Every branch sees the value as it was given, never one another
            // branch changed: each matching branch produces from its own copy
            // of the original, and the last one's result stands.
            let original = cur.get().clone();
            let mut matched = false;
            let mut kept = true;
            for sn in &n.list {
                if !branch_passes(sn, &original, key, parent_is_array, w) {
                    continue;
                }
                matched = true;
                cur.set(original.clone());
                let mut sub = ValidationError::default();
                kept = validate_node(sn, cur.reborrow(), key, parent_is_array, w, &mut sub);
            }
            if !matched {
                list_error(
                    n,
                    silent,
                    &original,
                    key,
                    parent_is_array,
                    w,
                    verr,
                    "Some",
                    4031,
                    "any of",
                    true,
                );
                return true;
            }
            kept
        }
        _ => {
            // All threads the value through its branches; a branch that
            // fails leaves the value as the one before it produced.
            let original = cur.get().clone();
            let mut pass_all = true;
            let mut kept = true;
            for sn in &n.list {
                let before = cur.get().clone();
                let mut sub = ValidationError::default();
                let k = validate_node(sn, cur.reborrow(), key, parent_is_array, w, &mut sub);
                if sub.has_any() {
                    pass_all = false;
                    cur.set(before);
                } else {
                    kept = k;
                }
            }
            if !pass_all {
                cur.set(original.clone());
                list_error(
                    n,
                    silent,
                    &original,
                    key,
                    parent_is_array,
                    w,
                    verr,
                    "All",
                    4032,
                    "all of",
                    false,
                );
                return true;
            }
            // An absent subject stays absent, for the required check to
            // see, as in TypeScript.
            if original.is_undefined() {
                cur.set(original);
            }
            kept
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::builders::*;
    use crate::node::{Token, Validator};
    use crate::spec::{arr, obj, Spec};
    use crate::Schema;
    use regex::Regex;
    use std::sync::Arc;

    fn chk(
        name: &str,
        f: impl Fn(&mut State<'_>, &mut Update) -> bool + Send + Sync + 'static,
    ) -> Validator {
        Validator {
            name: name.into(),
            func: Arc::new(f),
            args: vec![],
            suffix: None,
            inner: None,
        }
    }

    fn j(s: &str) -> Value {
        Value::from(serde_json::from_str::<serde_json::Value>(s).unwrap())
    }

    fn run(s: &Schema, input: &str) -> String {
        match s.validate(j(input)) {
            Ok(v) => serde_json::Value::from(v).to_string(),
            Err(e) => format!("ERR {}", e),
        }
    }

    fn before(mut n: Node, v: Validator) -> Node {
        n.befores.push(v);
        n
    }

    fn after(mut n: Node, v: Validator) -> Node {
        n.afters.push(v);
        n
    }

    fn at_a(n: impl Into<Spec>) -> Schema {
        Schema::new(obj([("a", n.into())]))
    }

    fn fail() -> Validator {
        chk("Odd", |_, _| false)
    }

    #[test]
    fn index_keys_need_no_allocation() {
        let mut buf = [0u8; 20];
        assert_eq!(index_key(0, &mut buf), "0");
        assert_eq!(index_key(7, &mut buf), "7");
        assert_eq!(index_key(1234567890123, &mut buf), "1234567890123");
    }

    #[test]
    fn plain_nodes_take_the_direct_path() {
        // Scalars, nested objects and arrays of objects with no validators
        // are judged in place on a read-only walk, with the paths intact.
        let s = Schema::new(obj([
            ("a", obj([("b", Spec::from(Token::Number))])),
            ("c", arr([obj([("d", Spec::from(Token::String))])])),
            ("e", arr([Spec::from(Token::Integer)])),
            ("f", Spec::from(Token::Boolean)),
        ]));
        assert!(s.valid(&j(
            r#"{"a":{"b":1},"c":[{"d":"x"},{"d":"y"}],"e":[1,2],"f":true}"#
        )));
        let bad = j(r#"{"a":{"b":"x"},"c":[{"d":"x"},{"d":2}],"e":[1,2.5],"f":1}"#);
        assert!(!s.valid(&bad));
        let paths: Vec<String> = s.error(&bad).iter().map(|e| e.path.clone()).collect();
        assert_eq!(paths, vec!["a.b", "c.1.d", "e.1", "f"]);
        // A produced walk takes the same shortcut for a passing scalar.
        assert_eq!(
            run(&s, r#"{"a":{"b":1},"c":[],"e":[],"f":false}"#),
            r#"{"a":{"b":1},"c":[],"e":[],"f":false}"#
        );
        // An empty string is judged by the node's Empty flag.
        let e = Schema::new(obj([("s", Spec::from(Token::String))]));
        assert!(!e.valid(&j(r#"{"s":""}"#)));
        assert!(
            Schema::new(obj([("s", Spec::from(empty(Token::String)))])).valid(&j(r#"{"s":""}"#))
        );
    }

    #[test]
    fn unprepared_object_keys_are_allocated() {
        // A node that never went through prepare has no shared keys, so the
        // walk allocates them; the path still renders.
        let n = crate::normalize::normalize(obj([("a", Token::Number)]));
        assert!(n.obj_keys.is_empty());
        let mut ctx = crate::Context::new();
        let defs = HashMap::new();
        let mut w = Walk {
            ctx: &mut ctx,
            defs: &defs,
            is_match: false,
            path: Vec::new(),
            paths: true,
        };
        let mut verr = ValidationError::default();
        let mut val = j(r#"{"a":"x"}"#);
        validate_node(&n, Cur::Mut(&mut val), "", false, &mut w, &mut verr);
        // Nothing is consumed either, so the key is also reported unknown.
        assert_eq!(
            verr.to_string(),
            "Validation failed for object \"{a:x}\" because the property \"a\" is not allowed.\n\
             Validation failed for property \"a\" with string \"x\" because the string is not of type number."
        );
    }

    #[test]
    fn marks() {
        assert_eq!(required_mark_for(Kind::Object), MARK_OBJECT_REQUIRED);
        assert_eq!(required_mark_for(Kind::Array), MARK_ARRAY_REQUIRED);
        assert_eq!(required_mark_for(Kind::String), MARK_SCALAR_REQUIRED);
        assert_eq!(type_mark_for(Kind::Object), MARK_OBJECT_TYPE);
        assert_eq!(type_mark_for(Kind::Array), MARK_ARRAY_TYPE);
        assert_eq!(type_mark_for(Kind::Check), MARK_CHECK_TYPE);
        assert_eq!(type_mark_for(Kind::String), MARK_SCALAR_TYPE);
    }

    #[test]
    fn custom_check_errors() {
        let s = at_a(before(buildize(Token::Number), fail()));
        assert_eq!(
            run(&s, r#"{"a":1}"#),
            "ERR Validation failed for property \"a\" with number \"1\" because check \"Odd\" failed."
        );
        assert!(!s.valid(&j(r#"{"a":1}"#)));
        assert_eq!(s.error(&j(r#"{"a":1}"#))[0].mark, MARK_CUSTOM_CHECK_ERR);

        let s = at_a(before(fault("bad $PATH $VALUE", Token::Number), fail()));
        assert_eq!(run(&s, r#"{"a":1}"#), "ERR bad a 1");
        assert!(!s.valid(&j(r#"{"a":1}"#)));

        let mut quiet = buildize(Token::Number);
        quiet.silent = true;
        let s = at_a(before(quiet, fail()));
        assert_eq!(run(&s, r#"{"a":1}"#), r#"{"a":1}"#);

        let text = chk("T", |_, u| {
            u.err = Some(UpdateErr::Text("no $PATH=$VALUE".into()));
            false
        });
        let s = at_a(before(buildize(Token::Number), text));
        assert_eq!(run(&s, r#"{"a":1}"#), "ERR no a=1");
        let e = &s.error(&j(r#"{"a":1}"#))[0];
        assert_eq!(
            (e.mark, e.why.as_str()),
            (MARK_CUSTOM_CHECK_TEXT, WHY_CHECK)
        );

        let marked = chk("M", |_, u| {
            u.err = Some(UpdateErr::Text("m".into()));
            u.why = Some("odd".into());
            u.mark = 99;
            false
        });
        let e = &at_a(before(buildize(Token::Number), marked)).error(&j(r#"{"a":1}"#))[0];
        assert_eq!((e.mark, e.why.as_str(), e.text.as_str()), (99, "odd", "m"));
        let marked = chk("M", |_, u| {
            u.why = Some("odd".into());
            u.mark = 98;
            false
        });
        let e = &at_a(before(buildize(Token::Number), marked)).error(&j(r#"{"a":1}"#))[0];
        assert_eq!((e.mark, e.why.as_str()), (98, "odd"));

        let field = chk("F", |_, u| {
            u.err = Some(UpdateErr::Field(Box::new(FieldError {
                text: "f".into(),
                ..Default::default()
            })));
            false
        });
        let s = at_a(before(buildize(Token::Number), field));
        assert_eq!(run(&s, r#"{"a":1}"#), "ERR f");
        let e = &s.error(&j(r#"{"a":1}"#))[0];
        assert_eq!((e.path.as_str(), e.mark), ("a", MARK_CUSTOM_CHECK_TEXT));

        let fields = chk("F", |_, u| {
            u.err = Some(UpdateErr::Fields(vec![
                FieldError {
                    text: "f1".into(),
                    ..Default::default()
                },
                FieldError {
                    text: "f2".into(),
                    path: "z".into(),
                    mark: 7,
                    ..Default::default()
                },
            ]));
            false
        });
        let s = at_a(before(buildize(Token::Number), fields));
        assert_eq!(run(&s, r#"{"a":1}"#), "ERR f1\nf2");
        let es = s.error(&j(r#"{"a":1}"#));
        assert_eq!((es[1].path.as_str(), es[1].mark), ("z", 7));
    }

    #[test]
    fn befores_replace_value_or_node_and_may_stop() {
        let dbl = chk("Dbl", |s, u| {
            if let Value::Num(x) = s.value {
                u.val = Some(Value::Num(x * 2.0));
            }
            true
        });
        let s = at_a(before(buildize(Token::Number), dbl));
        assert_eq!(run(&s, r#"{"a":2}"#), r#"{"a":4}"#);
        assert!(s.valid(&j(r#"{"a":2}"#)));

        let to_str = chk("Str", |_, u| {
            u.val = Some(Value::Str("x".into()));
            true
        });
        let s = at_a(before(buildize(Token::Number), to_str));
        assert_eq!(
            run(&s, r#"{"a":2}"#),
            "ERR Validation failed for property \"a\" with string \"x\" because the string is not of type number."
        );
        assert!(!s.valid(&j(r#"{"a":2}"#)));

        let swap = chk("Swap", |_, u| {
            u.node = Some(Arc::new(buildize(Token::String)));
            true
        });
        let s = at_a(before(buildize(Token::Number), swap));
        assert_eq!(run(&s, r#"{"a":"x"}"#), r#"{"a":"x"}"#);

        let stop = chk("Stop", |_, u| {
            u.done = true;
            true
        });
        let s = at_a(before(buildize(Token::Number), stop));
        assert_eq!(run(&s, r#"{"a":"x"}"#), r#"{"a":"x"}"#);

        // A failing check against an absent optional value is not an error.
        let s = at_a(before(optional(Token::Number), fail()));
        assert_eq!(run(&s, "{}"), r#"{"a":0}"#);
        // Unless the check insisted.
        let insist = chk("I", |_, u| {
            u.done = true;
            false
        });
        let s = at_a(before(optional(Token::Number), insist));
        assert_eq!(s.error(&j("{}")).len(), 1);
        // A required node's absence is the check's to report too, once.
        let s = at_a(before(required(Token::Number), fail()));
        assert_eq!(s.error(&j("{}")).len(), 1);
        // The check sees the state it is given.
        let peek = chk("Peek", |s, _| {
            s.absent && s.key == "a" && s.path_str() == "a" && !s.is_match && !s.parent_is_array
        });
        let s = at_a(before(optional(Token::Number), peek));
        assert_eq!(run(&s, "{}"), r#"{"a":0}"#);
    }

    #[test]
    fn afters_run_after_structure() {
        let s = at_a(after(buildize(Token::Number), fail()));
        assert_eq!(s.error(&j(r#"{"a":1}"#))[0].check, "Odd");

        let inc = chk("Inc", |s, u| {
            if let Value::Num(x) = s.value {
                u.val = Some(Value::Num(x + 1.0));
            }
            true
        });
        let s = at_a(after(buildize(Token::Number), inc));
        assert_eq!(run(&s, r#"{"a":1}"#), r#"{"a":2}"#);
        assert!(s.valid(&j(r#"{"a":1}"#)));

        let fill = chk("Fill", |_, u| {
            u.val = Some(Value::Num(9.0));
            true
        });
        let s = at_a(after(skip(Token::Number), fill));
        assert_eq!(run(&s, "{}"), r#"{"a":9}"#);

        // An injected default is the value a failing after reports.
        let s = at_a(after(optional(Token::Number), fail()));
        assert_eq!(
            run(&s, "{}"),
            "ERR Validation failed for property \"a\" with number \"0\" because check \"Odd\" failed."
        );
        let s = at_a(after(skip(Token::Number), fail()));
        assert_eq!(run(&s, "{}"), "{}");
    }

    #[test]
    fn structural_rules() {
        assert_eq!(
            run(&Schema::new(one([Spec::from(Token::Number)])), "1"),
            "1"
        );
        assert_eq!(
            run(&Schema::new(Spec::from(Node::of(Kind::List))), "1"),
            "1"
        );

        assert_eq!(
            run(&at_a(nullable(Token::String)), r#"{"a":null}"#),
            r#"{"a":null}"#
        );

        let s = at_a(never(1));
        assert_eq!(s.error(&j(r#"{"a":1}"#))[0].why, WHY_NEVER);
        assert_eq!(s.error(&j("{}"))[0].why, WHY_NEVER);
        assert!(!s.valid(&j("{}")));
        assert_eq!(run(&at_a(never(1).fault("nope")), r#"{"a":1}"#), "ERR nope");

        let re = || Regex::new("^a+$").unwrap();
        let s = at_a(buildize(re()));
        assert_eq!(
            run(&s, r#"{"a":"b"}"#),
            "ERR Validation failed for property \"a\" with string \"b\" because the string did not match /^a+$/."
        );
        assert_eq!(
            run(&s, r#"{"a":1}"#),
            "ERR Validation failed for property \"a\" with number \"1\" because the number is not of type string."
        );
        // A regexp never reports "required": an absent value is a non-string.
        assert_eq!(
            run(&s, "{}"),
            "ERR Validation failed for property \"a\" with value \"undefined\" because the value is not of type string."
        );
        assert_eq!(run(&s, r#"{"a":"aa"}"#), r#"{"a":"aa"}"#);
        assert_eq!(run(&at_a(optional(re())), "{}"), "{}");
        assert_eq!(run(&at_a(fault("F", re())), r#"{"a":1}"#), "ERR F");
        assert!(!at_a(fault("F", re())).valid(&j(r#"{"a":1}"#)));
        let mut quiet = buildize(re());
        quiet.silent = true;
        assert_eq!(run(&at_a(quiet), r#"{"a":1}"#), r#"{"a":1}"#);

        // An absent container is probed on a shared cursor so that required
        // descendants still raise.
        let s = at_a(obj([("b", Spec::from(Token::String))]));
        assert!(!s.valid(&j("{}")));
        assert_eq!(s.error(&j("{}"))[0].path, "a.b");
        let s = at_a(arr([Spec::from(Token::String), Spec::from(1)]));
        assert!(!s.valid(&j("{}")));
        assert_eq!(run(&at_a(buildize(Token::Any)), "{}"), "{}");
        // An absent root, likewise.
        let s = Schema::new(obj([("b", Spec::from(Token::String))]));
        assert!(!s.valid(&Value::Undefined));
        assert_eq!(s.error(&Value::Undefined)[0].path, "b");
        let s = Schema::new(arr([Spec::from(Token::String), Spec::from(1)]));
        assert!(!s.valid(&Value::Undefined));
        assert_eq!(s.error(&Value::Undefined)[0].path, "0");
        assert!(Schema::new(obj([("b", Spec::from(1))])).valid(&Value::Undefined));
    }

    #[test]
    fn leaf_type_errors() {
        let cases: Vec<(Node, &str, &str)> = vec![
            (
                buildize(Token::Integer),
                r#"{"a":1.5}"#,
                "number \"1.5\" because the number is not of type integer.",
            ),
            (
                buildize(Token::Boolean),
                r#"{"a":1}"#,
                "number \"1\" because the number is not of type boolean.",
            ),
            (
                buildize(Spec::Value(Value::Date(0))),
                r#"{"a":1}"#,
                "number \"1\" because the number is not of type date.",
            ),
            (
                buildize(num_bigint::BigInt::from(1)),
                r#"{"a":1}"#,
                "number \"1\" because the number is not of type bigint.",
            ),
            (
                buildize(f64::NAN),
                r#"{"a":1}"#,
                "number \"1\" because the number is not of type nan.",
            ),
            (
                buildize(Spec::Value(Value::Null)),
                r#"{"a":1}"#,
                "number \"1\" because the number is not of type null.",
            ),
            (
                buildize(Spec::Value(Value::Func(1))),
                r#"{"a":1}"#,
                "number \"1\" because the number is not of type function.",
            ),
        ];
        for (n, input, tail) in cases {
            assert_eq!(
                run(&at_a(n), input),
                format!("ERR Validation failed for property \"a\" with {}", tail)
            );
        }
        let s = at_a(buildize(Spec::Value(Value::Date(0))));
        let dated = Value::Obj([("a".to_string(), Value::Date(5))].into_iter().collect());
        assert_eq!(s.validate(dated.clone()).unwrap(), dated);
    }

    #[test]
    fn objects() {
        let inner = || obj([("a", Spec::from(1))]);
        let s = Schema::new(obj([("o", Spec::from(fault("F $PATH", inner())))]));
        assert_eq!(run(&s, r#"{"o":{"z":1}}"#), "ERR F o");
        assert!(!s.valid(&j(r#"{"o":{"z":1}}"#)));
        let mut quiet = buildize(inner());
        quiet.silent = true;
        let s = Schema::new(obj([("o", Spec::from(quiet))]));
        assert_eq!(run(&s, r#"{"o":{"z":1}}"#), r#"{"o":{"z":1,"a":1}}"#);

        let s = Schema::new(obj([("o", inner())]));
        assert!(s.valid(&j(r#"{"o":{"a":2}}"#)));
        assert!(!s.valid(&j(r#"{"o":{"a":"x"}}"#)));
        assert!(s.valid(&j(r#"{"o":{}}"#)));
        assert!(!s.valid(&j(r#"{"o":1}"#)));

        let s = at_a(ignore(Token::Number));
        assert_eq!(run(&s, r#"{"a":"x"}"#), "{}");
        assert_eq!(run(&s, r#"{"a":5}"#), r#"{"a":5}"#);
        assert_eq!(run(&s, "{}"), "{}");
        assert!(s.valid(&j(r#"{"a":"x"}"#)));
        assert!(s.valid(&j(r#"{"a":5}"#)));

        let s = Schema::new(child(Token::Number, inner()));
        assert_eq!(run(&s, r#"{"a":1,"z":2}"#), r#"{"a":1,"z":2}"#);
        assert!(!s.valid(&j(r#"{"a":1,"z":"x"}"#)));
        let s = Schema::new(child(ignore(Token::Number), inner()));
        assert_eq!(run(&s, r#"{"a":1,"z":"x"}"#), r#"{"a":1}"#);
        assert!(s.valid(&j(r#"{"a":1,"z":"x"}"#)));
    }

    #[test]
    fn arrays() {
        let s = at_a(fault("F", arr([Spec::from(Token::Number)])));
        assert_eq!(run(&s, r#"{"a":1}"#), "ERR F");
        let tuple = || arr([Spec::from(1), Spec::from(2)]);
        assert_eq!(run(&at_a(fault("T", tuple())), r#"{"a":[1,2,3]}"#), "ERR T");
        assert!(!at_a(fault("T", tuple())).valid(&j(r#"{"a":[1,2,3]}"#)));
        let mut quiet = buildize(tuple());
        quiet.silent = true;
        assert_eq!(run(&at_a(quiet), r#"{"a":[1,2,3]}"#), r#"{"a":[1,2,3]}"#);

        let s = at_a(tuple());
        assert!(s.valid(&j(r#"{"a":[1,2]}"#)));
        assert!(!s.valid(&j(r#"{"a":["x"]}"#)));
        assert!(s.valid(&j(r#"{"a":[]}"#)));
        let s = at_a(arr([Spec::from(Token::String), Spec::from(2)]));
        assert!(!s.valid(&j(r#"{"a":[]}"#)));

        let s = at_a(arr([Spec::from(ignore(Token::Number)), Spec::from(2)]));
        assert_eq!(run(&s, r#"{"a":["x",2]}"#), r#"{"a":[null,2]}"#);
        assert_eq!(run(&s, r#"{"a":[1]}"#), r#"{"a":[1,2]}"#);
        assert!(s.valid(&j(r#"{"a":["x",2]}"#)));

        let s = at_a(arr([Spec::from(1), Spec::from(skip(Token::Number))]));
        assert_eq!(run(&s, r#"{"a":[]}"#), r#"{"a":[1]}"#);

        let s = at_a(arr([Spec::from(Token::Number)]));
        assert!(!s.valid(&j(r#"{"a":[1,"x"]}"#)));
        assert!(s.valid(&j(r#"{"a":[1,2]}"#)));
        let s = at_a(arr([Spec::from(ignore(Token::Number))]));
        assert_eq!(run(&s, r#"{"a":[1,"x"]}"#), r#"{"a":[1,null]}"#);
        assert!(s.valid(&j(r#"{"a":[1,"x"]}"#)));
    }

    #[test]
    fn root_ignore_drops_a_bad_value() {
        let s = Schema::new(ignore(Token::Number));
        assert_eq!(run(&s, "\"x\""), "null");
        assert_eq!(run(&s, "5"), "5");
        assert!(s.valid(&j("\"x\"")));
        assert!(s.error(&j("\"x\"")).is_empty());
        assert_eq!(s.node().kind, Kind::Number);
    }
}