renox-core 1.0.0

Runtime of the Renox web framework: routing, views, database and models, validation, auth, queue, scheduler, mail, storage. Use it through the `renox` crate.
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
//! Form validation with Laravel-style rules, database-backed `unique` and
//! `exists`, and English messages (an app translates them in its lang files).
//!
//! ```
//! # use renox::prelude::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize)]
//! struct ProductForm {
//!     name: String,
//!     price: i64,
//!     email: Option<String>,
//! }
//!
//! impl Validate for ProductForm {
//!     fn rules(&self, v: &mut Validator) {
//!         v.field("name", &self.name).required().max(100).unique("products", "name");
//!         v.field("price", &self.price).label("sale price").min(1_000);
//!         v.field("email", &self.email).email();
//!     }
//! }
//!
//! async fn store(State(db): State<Db>, back: Back, Valid(form): Valid<ProductForm>) -> Result<Back> {
//!     // `form` passed every rule; invalid input never gets here.
//! #   let _ = (db, form);
//!     Ok(back)
//! }
//! ```

pub(crate) mod extract;
mod key_values;
mod messages;
pub(crate) mod nested;
mod value;

use std::collections::BTreeMap;

use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

pub use extract::Valid;
pub use key_values::KeyValues;
pub(crate) use messages::{render, template_for};
pub use value::{FieldValue, Inspected};

use crate::Result;
use crate::db::{Db, DbValue, ToDbValue, quote};
use chrono::NaiveDateTime;

/// Validation errors: messages keyed by field name.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Errors(BTreeMap<String, Vec<String>>);

impl Errors {
    /// No errors.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds `message` to the messages of `field`.
    pub fn add(&mut self, field: impl Into<String>, message: impl Into<String>) {
        self.0.entry(field.into()).or_default().push(message.into());
    }

    /// Whether there are no errors.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Whether `field` has at least one error.
    pub fn has(&self, field: &str) -> bool {
        self.0.contains_key(field)
    }

    /// The first message for a field.
    pub fn first(&self, field: &str) -> Option<&str> {
        self.0
            .get(field)
            .and_then(|m| m.first())
            .map(String::as_str)
    }

    /// Every field with its messages, fields in alphabetical order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &[String])> {
        self.0.iter().map(|(f, m)| (f.as_str(), m.as_slice()))
    }
}

/// Declares the rules for a form. Used by the `Valid<T>` extractor, which
/// calls, in order: [`prepare`](Validate::prepare),
/// [`authorize`](Validate::authorize), [`rules`](Validate::rules) and, when
/// they pass, [`after`](Validate::after). Only `rules` is required.
///
/// ```
/// # use renox::prelude::*;
/// use renox::validation::FormContext;
///
/// #[derive(serde::Deserialize)]
/// struct Invite { email: String, team_id: i64 }
///
/// impl Validate for Invite {
///     // Laravel's prepareForValidation: tidy the input first.
///     fn prepare(&mut self) {
///         self.email = self.email.trim().to_lowercase();
///     }
///
///     // Laravel's authorize: `false` answers 403 before any rule runs.
///     async fn authorize(&self, form: &FormContext<'_>) -> Result<bool> {
///         Ok(form.user.is_some_and(|user| user.has_role("owner")))
///     }
///
///     fn rules(&self, v: &mut Validator) {
///         v.field("email", &self.email).required().email();
///     }
///
///     // Laravel's `after`: checks that need the database or several fields,
///     // once the rules pass. Errors added here are shown like any other.
///     async fn after(&self, form: &FormContext<'_>, errors: &mut Errors) -> Result {
///         let members: i64 = renox::db::sql("SELECT COUNT(*) FROM team_user WHERE team_id = ?")
///             .bind(self.team_id)
///             .scalar(&form.state.db)
///             .await?;
///         if members >= 10 {
///             errors.add("email", "This team is full (10 members).");
///         }
///         Ok(())
///     }
/// }
/// ```
pub trait Validate {
    /// The named error bag this form's errors are flashed in (Laravel's
    /// error bags), for a page with two forms that share field names, e.g.
    /// `Some("login")` next to a sign-up form. Templates read them with
    /// `error('email', bag='login')`. `#[validate(bag = "login")]` on a
    /// derived struct sets it.
    const ERROR_BAG: Option<&'static str> = None;

    /// Declares the fields' rules on `v`; they run after `prepare` and `authorize`.
    fn rules(&self, v: &mut Validator);

    /// Tidies the input before anything checks it (Laravel's
    /// `prepareForValidation`): trim, lowercase an email, fill a slug. The
    /// form refilled after an error still shows what was typed.
    fn prepare(&mut self) {}

    /// Whether this request may send the form (Laravel's `authorize`), e.g.
    /// only the team's owner invites. `false` answers 403 before the rules
    /// run. It sees the input, so it runs once the input is read.
    fn authorize(
        &self,
        form: &FormContext<'_>,
    ) -> impl std::future::Future<Output = Result<bool>> + Send {
        let _ = form;
        std::future::ready(Ok(true))
    }

    /// Checks that need the database, another service or several fields,
    /// once the rules pass (Laravel's `after`, and async rules). Add
    /// errors with `errors.add(field, message)`.
    fn after(
        &self,
        form: &FormContext<'_>,
        errors: &mut Errors,
    ) -> impl std::future::Future<Output = Result> + Send {
        let _ = (form, errors);
        std::future::ready(Ok(()))
    }
}

/// `prepare`, `authorize` and `after` for a struct with
/// `#[derive(Validate)]`, which writes `rules` from its `#[validate(…)]`
/// attributes: add `#[validate(hooks)]` on the struct and implement the
/// ones you need.
///
/// ```
/// # use renox::prelude::*;
/// use renox::validation::{FormContext, ValidateHooks};
///
/// #[derive(serde::Deserialize, Validate)]
/// #[validate(hooks)]
/// struct Invite {
///     #[validate(required, email)]
///     email: String,
/// }
///
/// impl ValidateHooks for Invite {
///     fn prepare(&mut self) {
///         self.email = self.email.trim().to_lowercase();
///     }
///
///     async fn authorize(&self, form: &FormContext<'_>) -> Result<bool> {
///         Ok(form.user.is_some())
///     }
/// }
/// ```
pub trait ValidateHooks {
    /// Tidies the input before anything checks it; see [`Validate::prepare`].
    fn prepare(&mut self) {}

    /// Whether this request may send the form; `false` answers 403. See
    /// [`Validate::authorize`].
    fn authorize(
        &self,
        form: &FormContext<'_>,
    ) -> impl std::future::Future<Output = Result<bool>> + Send {
        let _ = form;
        std::future::ready(Ok(true))
    }

    /// Checks run once the rules pass; see [`Validate::after`].
    fn after(
        &self,
        form: &FormContext<'_>,
        errors: &mut Errors,
    ) -> impl std::future::Future<Output = Result> + Send {
        let _ = (form, errors);
        std::future::ready(Ok(()))
    }
}

/// What [`Validate::authorize`] and [`Validate::after`] see of the request.
#[non_exhaustive]
pub struct FormContext<'a> {
    /// The app's state, e.g. for its database.
    pub state: &'a crate::AppState,
    /// The logged-in user, if any.
    pub user: Option<&'a crate::auth::User>,
    /// The request's HTTP method.
    pub method: &'a axum::http::Method,
    /// The request's path, without the query string.
    pub path: &'a str,
}

struct Pending {
    field: String,
    label: String,
    table: String,
    column: String,
    value: DbValue,
    ignore_id: Option<crate::db::DbValue>,
    /// Extra conditions (`where_eq`, `where_null`, `where_not_null`).
    scope: Vec<ScopeCondition>,
    unique: bool,
    message: Option<String>,
}

/// A check that needs the request's user or a service, run by `finish`.
struct AsyncCheck {
    field: String,
    label: String,
    kind: AsyncKind,
    message: Option<String>,
}

enum AsyncKind {
    /// The logged-in user's password (Laravel's `current_password`).
    CurrentPassword(String),
    /// Not in a known data breach (Have I Been Pwned's range API).
    Uncompromised(String),
}

/// A condition added to a `unique`/`exists` check.
enum ScopeCondition {
    Eq(String, DbValue),
    Null(String),
    NotNull(String),
}

impl ScopeCondition {
    fn column(&self) -> &str {
        match self {
            Self::Eq(column, _) | Self::Null(column) | Self::NotNull(column) => column,
        }
    }
}

/// SQLite reads an unknown double-quoted column as a string literal, which
/// would make `unique("t", "typo")` always pass; refuse unknown columns.
async fn ensure_sqlite_column(db: &Db, table: &str, column: &str) -> Result {
    let found: i64 = crate::db::sql("SELECT COUNT(*) FROM pragma_table_info(?) WHERE name = ?")
        .bind(table)
        .bind(column)
        .scalar(db)
        .await?;
    if found == 0 {
        return Err(anyhow::anyhow!(
            "unique/exists rule: table `{table}` has no column `{column}`"
        )
        .into());
    }
    Ok(())
}

/// Collects rule failures. Rules run in order and stop at a field's first
/// failure; rules other than `required` and `accepted` skip empty values.
pub struct Validator {
    /// The request language's lang file, for overridden messages and labels.
    texts: Option<crate::i18n::Texts>,
    errors: Errors,
    pending: Vec<Pending>,
    checks: Vec<AsyncCheck>,
}

impl Default for Validator {
    fn default() -> Self {
        Self::new()
    }
}

impl Validator {
    /// A validator with Renox's English messages.
    pub fn new() -> Self {
        Self {
            texts: None,
            errors: Errors::new(),
            pending: Vec::new(),
            checks: Vec::new(),
        }
    }

    /// Messages and field names in `lang`, from the app's lang file
    /// (`renox.validation.*`, `renox.validation.attributes.*`), e.g. for
    /// rules run in a handler or a command: `Validator::new().in_lang(&lang)`.
    pub fn in_lang(self, lang: &crate::Lang) -> Self {
        self.with_texts(lang.texts())
    }

    /// Uses the app's translations of messages (`renox.validation.*`) and
    /// field names (`renox.validation.attributes.*`).
    pub(crate) fn with_texts(mut self, texts: crate::i18n::Texts) -> Self {
        self.texts = Some(texts);
        self
    }

    fn template(&self, key: &str) -> std::borrow::Cow<'static, str> {
        messages::template_for(self.texts.as_ref(), key)
    }

    /// A field's name for messages: the app's translation
    /// (`renox.validation.attributes.{name}`) or the name with spaces.
    fn translated_label(&self, name: &str) -> Option<String> {
        self.texts.as_ref().and_then(|t| {
            t.get(&format!("renox.validation.attributes.{name}"))
                .cloned()
        })
    }

    fn label_for(&self, name: &str) -> String {
        nested::label(name, |key| self.translated_label(key))
    }

    /// Rules for each item of a list, e.g. every tag or every uploaded
    /// photo; errors are keyed `name.0`, `name.1`, … and labelled
    /// "`name` #1", "#2", …
    ///
    /// ```
    /// # use renox::prelude::*;
    /// # struct Form { tags: Vec<String>, photos: Vec<Upload> }
    /// # impl Validate for Form {
    /// fn rules(&self, v: &mut Validator) {
    ///     v.field("tags", &self.tags).max(5);
    ///     v.each("tags", &self.tags, |tag| tag.required().max(20));
    ///     v.each("photos", &self.photos, |photo| photo.image().max(2048));
    /// }
    /// # }
    /// ```
    pub fn each<T: FieldValue>(
        &mut self,
        name: &str,
        items: &[T],
        rules: impl for<'a> Fn(Field<'a>) -> Field<'a>,
    ) {
        let base = self.label_for(name);
        for (i, item) in items.iter().enumerate() {
            let key = format!("{name}.{i}");
            let label = format!("{base} #{}", i + 1);
            rules(self.field(&key, item).label(&label));
        }
    }

    /// Each item's own `Validate` rules, for a list of structs (e.g. the
    /// lines of an order sent as JSON); errors are keyed `name.0.field`.
    pub fn nested<T: Validate>(&mut self, name: &str, items: &[T]) {
        for (i, item) in items.iter().enumerate() {
            let mut inner = Validator {
                texts: self.texts.clone(),
                errors: Errors::new(),
                pending: Vec::new(),
                checks: Vec::new(),
            };
            item.rules(&mut inner);
            for (field, messages) in inner.errors.iter() {
                for message in messages {
                    self.errors
                        .add(format!("{name}.{i}.{field}"), message.clone());
                }
            }
            for mut pending in inner.pending {
                pending.field = format!("{name}.{i}.{}", pending.field);
                self.pending.push(pending);
            }
            for mut check in inner.checks {
                check.field = format!("{name}.{i}.{}", check.field);
                self.checks.push(check);
            }
        }
    }

    /// No two items of a list are the same (Laravel's `distinct`), e.g. the
    /// emails invited at once; each repeat gets the error, keyed `name.i`.
    pub fn distinct<T: FieldValue>(&mut self, name: &str, items: &[T]) {
        let base = self.label_for(name);
        let template = self.template("distinct");
        let mut seen: Vec<Inspected> = Vec::new();
        for (i, item) in items.iter().enumerate() {
            let value = match item.inspect() {
                Inspected::Text(text) => Inspected::Text(text.trim().to_lowercase()),
                other => other,
            };
            if value == Inspected::Missing {
                continue;
            }
            if seen.contains(&value) {
                let label = format!("{base} #{}", i + 1);
                self.errors
                    .add(format!("{name}.{i}"), render(&template, &label, &[]));
            } else {
                seen.push(value);
            }
        }
    }

    /// Starts the rules for one field. The label in messages defaults to the
    /// name with `_` replaced by spaces.
    pub fn field<'v>(&'v mut self, name: &str, value: &impl FieldValue) -> Field<'v> {
        let translated = self.translated_label(name).is_some();
        let mut field = Field {
            translated,
            // `items.0.name` reads "name" (or the app's `items.*.name`).
            label: self.label_for(name),
            name: name.to_owned(),
            value: value.inspect(),
            db_value: value.db_value(),
            failed: false,
            last_pending: None,
            last_check: None,
            v: self,
        };
        // `NaN`, `inf` and `1e999` parse as floats, but aren't numbers anyone entered.
        if let Inspected::Number(n) = field.value
            && !n.is_finite()
        {
            field.fail("numeric", &[]);
        }
        field
    }

    /// Adds an error that no rule covers.
    pub fn error(&mut self, field: &str, message: impl Into<String>) {
        self.errors.add(field, message);
    }

    /// Runs the database checks and returns every error (empty when valid).
    /// `current_password` fails here, as there's no user to check against,
    /// and the breach check of `Password::uncompromised` is skipped; use
    /// [`finish_for`](Self::finish_for) for those (`Valid<T>` does).
    pub async fn finish(self, db: &Db) -> Result<Errors> {
        self.finish_with(db, None, None).await
    }

    /// Like [`finish`](Self::finish), with the logged-in user for
    /// `current_password` and the app's HTTP client for
    /// `Password::uncompromised`.
    pub async fn finish_for(
        self,
        state: &crate::AppState,
        user: Option<&crate::auth::User>,
    ) -> Result<Errors> {
        self.finish_with(&state.db, Some(state), user).await
    }

    async fn finish_with(
        self,
        db: &Db,
        state: Option<&crate::AppState>,
        user: Option<&crate::auth::User>,
    ) -> Result<Errors> {
        let mut errors = self.errors;
        for check in self.pending {
            if errors.has(&check.field) {
                continue;
            }
            let dialect = db.dialect();
            if dialect == crate::db::Dialect::Sqlite {
                ensure_sqlite_column(db, &check.table, &check.column).await?;
                for condition in &check.scope {
                    ensure_sqlite_column(db, &check.table, condition.column()).await?;
                }
            }
            // Form input is text; PostgreSQL won't compare text with a number
            // column, so compare as text (a no-op for text columns).
            let column = match (&check.value, dialect) {
                (DbValue::Text(_), crate::db::Dialect::Postgres) => {
                    format!("CAST({} AS TEXT)", quote(&check.column))
                }
                _ => quote(&check.column),
            };
            let mut sql = format!(
                "SELECT EXISTS(SELECT 1 FROM {} WHERE {column} = ?",
                quote(&check.table),
            );
            if check.ignore_id.is_some() {
                sql.push_str(" AND \"id\" != ?");
            }
            let mut scope_values = Vec::new();
            for condition in check.scope {
                match condition {
                    ScopeCondition::Eq(column, value) => {
                        sql.push_str(&format!(" AND {} = ?", quote(&column)));
                        scope_values.push(value);
                    }
                    ScopeCondition::Null(column) => {
                        sql.push_str(&format!(" AND {} IS NULL", quote(&column)));
                    }
                    ScopeCondition::NotNull(column) => {
                        sql.push_str(&format!(" AND {} IS NOT NULL", quote(&column)));
                    }
                }
            }
            sql.push(')');
            let mut query = crate::db::sql(sql).bind(check.value);
            if let Some(id) = check.ignore_id {
                query = query.bind(id);
            }
            let query = query.bind_all(scope_values);
            let found: bool = query.scalar(db).await?;
            if found == check.unique {
                let key = if check.unique { "unique" } else { "exists" };
                let message = check.message.unwrap_or_else(|| {
                    render(
                        &messages::template_for(self.texts.as_ref(), key),
                        &check.label,
                        &[],
                    )
                });
                errors.add(check.field, message);
            }
        }
        for check in self.checks {
            if errors.has(&check.field) {
                continue;
            }
            let key = match &check.kind {
                AsyncKind::CurrentPassword(password) => match user {
                    Some(user) if user.check_password(password).await => continue,
                    _ => "current_password",
                },
                AsyncKind::Uncompromised(password) => match state {
                    Some(state) if breached(state, password).await => "password.uncompromised",
                    _ => continue,
                },
            };
            let message = check.message.unwrap_or_else(|| {
                render(
                    &messages::template_for(self.texts.as_ref(), key),
                    &check.label,
                    &[],
                )
            });
            errors.add(check.field, message);
        }
        Ok(errors)
    }

    /// Like `rules_of`, with the app's translations of messages and labels.
    pub(crate) fn rules_with_texts(data: &impl Validate, texts: crate::i18n::Texts) -> Self {
        let mut validator = Self::new().with_texts(texts);
        data.rules(&mut validator);
        validator
    }

    /// Applies `data`'s rules; call `finish` to run the database checks.
    ///
    /// ```
    /// # use renox::prelude::*;
    /// # #[derive(serde::Deserialize)] struct ProductForm { name: String }
    /// # impl Validate for ProductForm { fn rules(&self, v: &mut Validator) { v.field("name", &self.name).required(); } }
    /// # async fn demo(form: ProductForm, db: Db) -> Result {
    /// let errors = Validator::rules_of(&form).finish(&db).await?;
    /// # let _ = errors; Ok(()) }
    /// ```
    pub fn rules_of(data: &impl Validate) -> Self {
        let mut validator = Self::new();
        data.rules(&mut validator);
        validator
    }
}

/// The rules for one field, chained: `v.field("name", &self.name).required().max(100)`.
pub struct Field<'v> {
    v: &'v mut Validator,
    name: String,
    label: String,
    /// The label came from the app's lang file.
    translated: bool,
    value: Inspected,
    db_value: DbValue,
    failed: bool,
    last_pending: Option<usize>,
    /// The `current_password`/`uncompromised` check just added, for `message`.
    last_check: Option<usize>,
}

fn number(n: f64) -> String {
    if n.fract() == 0.0 && n.abs() < 1e15 {
        format!("{}", n as i64)
    } else {
        n.to_string()
    }
}

impl Field<'_> {
    /// The name used in messages, e.g. `.label("sale price")`.
    pub fn label(mut self, label: &str) -> Self {
        self.label = label.to_owned();
        self
    }

    /// Uses `label` unless the app's lang file names this field.
    pub(crate) fn fallback_label(mut self, label: &str) -> Self {
        if !self.translated {
            self.label = label.to_owned();
        }
        self
    }

    fn fail(&mut self, key: &str, params: &[(&str, String)]) {
        if !self.failed {
            let message = render(&self.v.template(key), &self.label, params);
            self.v.errors.add(&self.name, message);
            self.failed = true;
            self.last_pending = None;
            self.last_check = None;
        }
    }

    fn check(&mut self, kind: AsyncKind) {
        self.v.checks.push(AsyncCheck {
            field: self.name.clone(),
            label: self.label.clone(),
            kind,
            message: None,
        });
        self.last_pending = None;
        self.last_check = Some(self.v.checks.len() - 1);
    }

    fn present(&self) -> bool {
        !self.failed && self.value != Inspected::Missing
    }

    /// Replaces this field's error so far with `message`: the error of the
    /// rule just before it, or of any earlier rule that failed (only one error
    /// is kept per field). A database or async check just before it gets the
    /// message when it fails.
    pub fn message(self, message: impl Into<String>) -> Self {
        let message = message.into();
        if let Some(i) = self.last_pending {
            self.v.pending[i].message = Some(message);
        } else if let Some(i) = self.last_check {
            self.v.checks[i].message = Some(message);
        } else if self.failed
            && let Some(last) = self
                .v
                .errors
                .0
                .get_mut(&self.name)
                .and_then(|messages| messages.last_mut())
        {
            *last = message;
        }
        self
    }

    /// Fails when the value is missing: `None`, or text that is blank after trimming.
    pub fn required(mut self) -> Self {
        if !self.failed && self.value == Inspected::Missing {
            self.fail("required", &[]);
        }
        self
    }

    fn size_rule(
        mut self,
        kind: &str,
        ok: impl Fn(f64) -> bool,
        params: &[(&str, String)],
    ) -> Self {
        if !self.present() {
            return self;
        }
        let (size, suffix) = match &self.value {
            Inspected::Text(text) => (text.chars().count() as f64, "string"),
            Inspected::Number(n) => (*n, "numeric"),
            Inspected::Items(n) => (*n as f64, "array"),
            Inspected::File { kilobytes, .. } => (*kilobytes, "file"),
            _ => return self,
        };
        if !ok(size) {
            self.fail(&format!("{kind}.{suffix}"), params);
        }
        self
    }

    /// At least `min` characters, items, kilobytes (files), or as a number.
    pub fn min(self, min: impl Into<f64>) -> Self {
        let min = min.into();
        self.size_rule("min", |s| s >= min, &[("min", number(min))])
    }

    /// At most `max` characters, items, kilobytes (files), or as a number.
    pub fn max(self, max: impl Into<f64>) -> Self {
        let max = max.into();
        self.size_rule("max", |s| s <= max, &[("max", number(max))])
    }

    /// Between `min` and `max` inclusive, measured as `min`/`max` do.
    pub fn between(self, min: impl Into<f64>, max: impl Into<f64>) -> Self {
        let (min, max) = (min.into(), max.into());
        self.size_rule(
            "between",
            |s| s >= min && s <= max,
            &[("min", number(min)), ("max", number(max))],
        )
    }

    /// An email address: something before a single `@`, a dotted domain, no spaces.
    pub fn email(mut self) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value)
            && !is_email(text)
        {
            self.fail("email", &[]);
        }
        self
    }

    /// An `http://` or `https://` URL with a host and no spaces.
    pub fn url(mut self) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value)
            && !is_url(text)
        {
            self.fail("url", &[]);
        }
        self
    }

    /// An uploaded image: PNG, JPEG, GIF or WebP, checked from the file's
    /// content rather than its name.
    pub fn image(mut self) -> Self {
        if let (true, Inspected::File { image, .. }) = (self.present(), &self.value)
            && !*image
        {
            self.fail("image", &[]);
        }
        self
    }

    /// An uploaded file of one of these types, e.g. `&["jpg", "png", "pdf"]`
    /// (`jpeg` counts as `jpg`). The content decides for the formats Renox can
    /// recognise, the file name for the rest.
    pub fn mimes(mut self, extensions: &[&str]) -> Self {
        if let (true, Inspected::File { extension, .. }) = (self.present(), &self.value) {
            let normalise = |e: &str| match e.to_ascii_lowercase().as_str() {
                "jpeg" => "jpg".to_owned(),
                other => other.to_owned(),
            };
            let ok = extensions
                .iter()
                .any(|e| normalise(e) == normalise(extension));
            if !ok {
                self.fail("mimes", &[("values", extensions.join(", "))]);
            }
        }
        self
    }

    /// One of the given values (Laravel's `in`).
    pub fn one_of<V: FieldValue>(mut self, allowed: &[V]) -> Self {
        if self.present() && !allowed.iter().any(|a| a.inspect() == self.value) {
            self.fail("in", &[]);
        }
        self
    }

    /// Equal to its confirmation field, e.g. `password` and `password_confirmation`.
    pub fn confirmed(mut self, confirmation: &impl FieldValue) -> Self {
        if self.present() && confirmation.inspect() != self.value {
            self.fail("confirmed", &[]);
        }
        self
    }

    /// A checkbox that must be ticked.
    pub fn accepted(mut self) -> Self {
        if !self.failed && self.value != Inspected::Bool(true) {
            self.fail("accepted", &[]);
        }
        self
    }

    /// A custom check: fails with `message` when `valid` is false.
    pub fn rule(mut self, valid: bool, message: impl Into<String>) -> Self {
        if !self.failed && !valid {
            self.v.errors.add(&self.name, message);
            self.failed = true;
            self.last_pending = None;
            self.last_check = None;
        }
        self
    }

    /// The whole text matches `pattern` (a regular expression; anchor it
    /// with `^…$` to match all of it), e.g. `.matches(r"^[A-Z]{2}\d{4}$")`.
    pub fn matches(mut self, pattern: &str) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value) {
            let ok = match cached_regex(pattern) {
                Ok(regex) => regex.is_match(text),
                Err(err) => {
                    tracing::error!(pattern, error = %err, "invalid pattern in a `matches` rule");
                    false
                }
            };
            if !ok {
                self.fail("regex", &[]);
            }
        }
        self
    }

    /// Exactly `n` digits (and nothing else), e.g. a PIN.
    pub fn digits(mut self, n: usize) -> Self {
        if self.present() && digit_count(&self.value) != Some(n) {
            self.fail("digits", &[("digits", n.to_string())]);
        }
        self
    }

    /// Between `min` and `max` digits (and nothing else), e.g. a phone number.
    pub fn digits_between(mut self, min: usize, max: usize) -> Self {
        if self.present() && !digit_count(&self.value).is_some_and(|n| n >= min && n <= max) {
            self.fail(
                "digits_between",
                &[("min", min.to_string()), ("max", max.to_string())],
            );
        }
        self
    }

    /// A date (`2026-10-01`) or a date and time (`2026-10-01T10:30`).
    pub fn date(mut self) -> Self {
        if self.present() && self.as_date().is_none() {
            self.fail("date", &[]);
        }
        self
    }

    fn date_rule(
        mut self,
        key: &str,
        limit: NaiveDateTime,
        ok: impl Fn(NaiveDateTime, NaiveDateTime) -> bool,
    ) -> Self {
        if !self.present() {
            return self;
        }
        match self.as_date() {
            None => self.fail("date", &[]),
            Some(date) if !ok(date, limit) => {
                let shown = if limit.time() == chrono::NaiveTime::MIN {
                    limit.date().to_string()
                } else {
                    limit.format("%Y-%m-%d %H:%M").to_string()
                };
                self.fail(key, &[("date", shown)]);
            }
            Some(_) => {}
        }
        self
    }

    /// A date before `limit` (a `NaiveDate`, `NaiveDateTime` or `DateTime`,
    /// or text such as `"2026-01-01"`), e.g. `.before(today)` for a birth date.
    pub fn before(self, limit: impl FieldValue) -> Self {
        match limit_date(&limit) {
            Some(limit) => self.date_rule("before", limit, |d, l| d < l),
            None => self,
        }
    }

    /// A date on or before `limit`.
    pub fn before_or_equal(self, limit: impl FieldValue) -> Self {
        match limit_date(&limit) {
            Some(limit) => self.date_rule("before_or_equal", limit, |d, l| d <= l),
            None => self,
        }
    }

    /// A date after `limit`, e.g. `.after(self.start)` for an end date.
    pub fn after(self, limit: impl FieldValue) -> Self {
        match limit_date(&limit) {
            Some(limit) => self.date_rule("after", limit, |d, l| d > l),
            None => self,
        }
    }

    /// A date on or after `limit`.
    pub fn after_or_equal(self, limit: impl FieldValue) -> Self {
        match limit_date(&limit) {
            Some(limit) => self.date_rule("after_or_equal", limit, |d, l| d >= l),
            None => self,
        }
    }

    fn as_date(&self) -> Option<NaiveDateTime> {
        match &self.value {
            Inspected::Date(date) => Some(*date),
            Inspected::Text(text) => parse_date(text.trim()),
            _ => None,
        }
    }

    /// None of the given values (Laravel's `not_in`).
    pub fn none_of<V: FieldValue>(mut self, refused: &[V]) -> Self {
        if self.present() && refused.iter().any(|r| r.inspect() == self.value) {
            self.fail("not_in", &[]);
        }
        self
    }

    fn text_rule(
        mut self,
        key: &str,
        ok: impl Fn(&str) -> bool,
        params: &[(&str, String)],
    ) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value)
            && !ok(text)
        {
            self.fail(key, params);
        }
        self
    }

    /// Letters only (any language's: `é`, `ü`, `ß` count).
    pub fn alpha(self) -> Self {
        self.text_rule("alpha", |t| t.chars().all(char::is_alphabetic), &[])
    }

    /// Letters and digits only.
    pub fn alpha_num(self) -> Self {
        self.text_rule("alpha_num", |t| t.chars().all(char::is_alphanumeric), &[])
    }

    /// Letters, digits, `-` and `_`, e.g. a username or a slug.
    pub fn alpha_dash(self) -> Self {
        self.text_rule(
            "alpha_dash",
            |t| {
                t.chars()
                    .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
            },
            &[],
        )
    }

    /// No uppercase letters.
    pub fn lowercase(self) -> Self {
        self.text_rule("lowercase", |t| !t.chars().any(char::is_uppercase), &[])
    }

    /// No lowercase letters.
    pub fn uppercase(self) -> Self {
        self.text_rule("uppercase", |t| !t.chars().any(char::is_lowercase), &[])
    }

    /// Starts with one of `prefixes`, e.g. `.starts_with(&["08", "+62"])`.
    pub fn starts_with(self, prefixes: &[&str]) -> Self {
        let values = prefixes.join(", ");
        self.text_rule(
            "starts_with",
            |t| prefixes.iter().any(|p| t.starts_with(p)),
            &[("values", values)],
        )
    }

    /// Ends with one of `suffixes`, e.g. `.ends_with(&["@company.com"])`.
    pub fn ends_with(self, suffixes: &[&str]) -> Self {
        let values = suffixes.join(", ");
        self.text_rule(
            "ends_with",
            |t| suffixes.iter().any(|s| t.ends_with(s)),
            &[("values", values)],
        )
    }

    /// A UUID (`8-4-4-4-12` hexadecimal digits).
    pub fn uuid(self) -> Self {
        self.text_rule("uuid", is_uuid, &[])
    }

    /// An IPv4 or IPv6 address.
    pub fn ip(self) -> Self {
        self.text_rule("ip", |t| t.trim().parse::<std::net::IpAddr>().is_ok(), &[])
    }

    /// Exactly `size` characters, items, kilobytes (files), or equal to it as
    /// a number (Laravel's `size`).
    pub fn size(self, size: impl Into<f64>) -> Self {
        let size = size.into();
        self.size_rule("size", |s| s == size, &[("size", number(size))])
    }

    /// Required when `other` is empty, e.g. an email when there's no phone.
    pub fn required_without(self, other: &impl FieldValue) -> Self {
        let missing = other.inspect() == Inspected::Missing;
        self.required_if(missing)
    }

    /// Must be empty when `condition` holds (Laravel's `prohibited_if`), e.g.
    /// no discount code on a gift card order.
    pub fn prohibited_if(mut self, condition: bool) -> Self {
        if condition && !self.failed && self.value != Inspected::Missing {
            self.fail("prohibited", &[]);
        }
        self
    }

    /// Required when `condition` holds, e.g.
    /// `.required_if(self.kind == "company")` for a company name.
    pub fn required_if(self, condition: bool) -> Self {
        if condition { self.required() } else { self }
    }

    /// Required unless `condition` holds.
    pub fn required_unless(self, condition: bool) -> Self {
        self.required_if(!condition)
    }

    /// Required when `other` has a value, e.g. a phone number's country
    /// code when a phone number is given.
    pub fn required_with(self, other: &impl FieldValue) -> Self {
        let given = other.inspect() != Inspected::Missing;
        self.required_if(given)
    }

    /// Equal to another field, named `other` in the message.
    pub fn same(mut self, other: &str, value: &impl FieldValue) -> Self {
        if self.present() && value.inspect() != self.value {
            let other = self.v.label_for(other);
            self.fail("same", &[("other", other)]);
        }
        self
    }

    /// Different from another field, named `other` in the message.
    pub fn different(mut self, other: &str, value: &impl FieldValue) -> Self {
        if self.present() && value.inspect() == self.value {
            let other = self.v.label_for(other);
            self.fail("different", &[("other", other)]);
        }
        self
    }

    fn compare_rule(
        mut self,
        key: &str,
        other: &str,
        value: &impl FieldValue,
        ok: fn(std::cmp::Ordering) -> bool,
    ) -> Self {
        if !self.present() {
            return self;
        }
        let other_value = value.inspect();
        if other_value == Inspected::Missing {
            return self;
        }
        // Two texts that both read as numbers compare as numbers (a price
        // in a `String`); other text compares by length.
        let both_numeric = matches!(
            (&self.value, &other_value),
            (Inspected::Text(_), Inspected::Text(_))
        ) && numeric_value(&self.value).is_some()
            && numeric_value(&other_value).is_some();
        let measured = |value: &Inspected| -> Option<(f64, &'static str)> {
            match value {
                Inspected::Text(_) if both_numeric => Some((numeric_value(value)?, "numeric")),
                Inspected::Number(n) => Some((*n, "numeric")),
                Inspected::Text(text) => Some((text.chars().count() as f64, "string")),
                Inspected::Items(n) => Some((*n as f64, "array")),
                Inspected::File { kilobytes, .. } => Some((*kilobytes, "file")),
                _ => None,
            }
        };
        let label = self.v.label_for(other);
        // Dates compare as dates, whatever form they came in.
        let as_date = |value: &Inspected| match value {
            Inspected::Date(date) => Some(*date),
            Inspected::Text(text) => parse_date(text.trim()),
            _ => None,
        };
        let dates = as_date(&self.value).zip(as_date(&other_value));
        if let Some((a, b)) = dates {
            if !ok(a.cmp(&b)) {
                self.fail(&format!("{key}.date"), &[("other", label)]);
            }
            return self;
        }
        match (measured(&self.value), measured(&other_value)) {
            (Some((a, kind)), Some((b, other_kind))) if kind == other_kind => {
                if !a.partial_cmp(&b).is_some_and(ok) {
                    self.fail(&format!("{key}.{kind}"), &[("other", label)]);
                }
            }
            _ => self.fail(&format!("{key}.numeric"), &[("other", label)]),
        }
        self
    }

    /// Greater than another field (Laravel's `gt`), named `other` in the
    /// message: numbers by value (also two texts that both read as
    /// numbers), dates by date, other text by length, lists by items,
    /// files by size, e.g. a maximum price above the minimum. Skipped when
    /// the other field is empty.
    pub fn gt(self, other: &str, value: &impl FieldValue) -> Self {
        self.compare_rule("gt", other, value, |o| o.is_gt())
    }

    /// Greater than or equal to another field; see [`gt`](Self::gt).
    pub fn gte(self, other: &str, value: &impl FieldValue) -> Self {
        self.compare_rule("gte", other, value, |o| o.is_ge())
    }

    /// Less than another field; see [`gt`](Self::gt).
    pub fn lt(self, other: &str, value: &impl FieldValue) -> Self {
        self.compare_rule("lt", other, value, |o| o.is_lt())
    }

    /// Less than or equal to another field; see [`gt`](Self::gt).
    pub fn lte(self, other: &str, value: &impl FieldValue) -> Self {
        self.compare_rule("lte", other, value, |o| o.is_le())
    }

    /// A number with `min` to `max` decimal places (Laravel's `decimal`),
    /// e.g. `.decimal(2, 2)` for a price typed as `12.50`. A text field
    /// keeps what was typed; an `f64` field has lost trailing zeros
    /// (`12.50` is `12.5`), so check prices as text.
    pub fn decimal(mut self, min: usize, max: usize) -> Self {
        if !self.present() {
            return self;
        }
        let text = match &self.value {
            Inspected::Text(text) => text.trim().to_owned(),
            Inspected::Number(n) => n.to_string(),
            _ => String::new(),
        };
        let places = decimal_places(&text);
        if !places.is_some_and(|p| p >= min && p <= max) {
            let places = if min == max {
                min.to_string()
            } else {
                format!("{min}-{max}")
            };
            self.fail("decimal", &[("decimal", places)]);
        }
        self
    }

    /// An uploaded image within `limits`, e.g.
    /// `.dimensions(&Dimensions::new().min_width(400).ratio(16, 9))`; see
    /// [`Dimensions`]. A file that isn't an image fails.
    pub fn dimensions(mut self, limits: &Dimensions) -> Self {
        if let (true, Inspected::File { dimensions, .. }) = (self.present(), &self.value) {
            let ok = dimensions.is_some_and(|(w, h)| limits.allows(w, h));
            if !ok {
                self.fail("dimensions", &[]);
            }
        }
        self
    }

    /// Must be empty (Laravel's `prohibited`), e.g. a field only an admin
    /// may send, checked for everyone else.
    pub fn prohibited(self) -> Self {
        self.prohibited_if(true)
    }

    /// Must be empty unless `condition` holds.
    pub fn prohibited_unless(self, condition: bool) -> Self {
        self.prohibited_if(!condition)
    }

    /// When this field has a value, `other` must be empty (Laravel's
    /// `prohibits`), e.g. a coupon code and a gift card can't both be used.
    pub fn prohibits(mut self, other: &str, value: &impl FieldValue) -> Self {
        if self.present() && value.inspect() != Inspected::Missing {
            let other = self.v.label_for(other);
            self.fail("prohibits", &[("other", other)]);
        }
        self
    }

    /// Required when every one of `others` has a value.
    pub fn required_with_all(self, others: &[&dyn FieldValue]) -> Self {
        let all = others.iter().all(|o| o.inspect() != Inspected::Missing);
        self.required_if(all)
    }

    /// Required when none of `others` has a value, e.g. one way to reach
    /// the customer at least.
    pub fn required_without_all(self, others: &[&dyn FieldValue]) -> Self {
        let none = others.iter().all(|o| o.inspect() == Inspected::Missing);
        self.required_if(none)
    }

    /// An integer of at least `min` digits.
    pub fn min_digits(mut self, min: usize) -> Self {
        if self.present() && !integer_digits(&self.value).is_some_and(|n| n >= min) {
            self.fail("min_digits", &[("min", min.to_string())]);
        }
        self
    }

    /// An integer of at most `max` digits.
    pub fn max_digits(mut self, max: usize) -> Self {
        if self.present() && !integer_digits(&self.value).is_some_and(|n| n <= max) {
            self.fail("max_digits", &[("max", max.to_string())]);
        }
        self
    }

    /// A multiple of `step`, e.g. `.multiple_of(500)` for amounts in
    /// steps of 500, or `.multiple_of(0.25)`.
    pub fn multiple_of(mut self, step: impl Into<f64>) -> Self {
        let step = step.into();
        if !self.present() {
            return self;
        }
        let ok = numeric_value(&self.value).is_some_and(|n| {
            let ratio = n / step;
            step != 0.0 && (ratio - ratio.round()).abs() < 1e-9
        });
        if !ok {
            self.fail("multiple_of", &[("value", number(step))]);
        }
        self
    }

    /// A number: a number field, or text that reads as one (`12`, `-3.5`).
    pub fn numeric(mut self) -> Self {
        if self.present() && numeric_value(&self.value).is_none() {
            self.fail("numeric", &[]);
        }
        self
    }

    /// A whole number: a number field without a fraction, or text that
    /// reads as one.
    pub fn integer(mut self) -> Self {
        if self.present() && !numeric_value(&self.value).is_some_and(|n| n.fract() == 0.0) {
            self.fail("integer", &[]);
        }
        self
    }

    /// Valid JSON text, e.g. a settings field edited by hand.
    pub fn json(self) -> Self {
        self.text_rule(
            "json",
            |t| serde_json::from_str::<serde_json::Value>(t).is_ok(),
            &[],
        )
    }

    /// A ULID (26 characters of Crockford's base 32).
    pub fn ulid(self) -> Self {
        self.text_rule("ulid", |t| t.trim().parse::<crate::db::Ulid>().is_ok(), &[])
    }

    /// An IANA time zone name, e.g. `Asia/Jakarta`, or `UTC`.
    pub fn timezone(self) -> Self {
        self.text_rule(
            "timezone",
            |t| t.trim().parse::<chrono_tz::Tz>().is_ok(),
            &[],
        )
    }

    /// A MAC address: `00:1A:2B:3C:4D:5E`, with `-` as well, or
    /// `001A.2B3C.4D5E`.
    pub fn mac_address(self) -> Self {
        self.text_rule("mac_address", is_mac_address, &[])
    }

    /// ASCII characters only.
    pub fn ascii(self) -> Self {
        self.text_rule("ascii", |t| t.is_ascii(), &[])
    }

    /// A hex colour: `#RGB`, `#RGBA`, `#RRGGBB` or `#RRGGBBAA`.
    pub fn hex_color(self) -> Self {
        self.text_rule(
            "hex_color",
            |t| {
                t.strip_prefix('#').is_some_and(|hex| {
                    matches!(hex.len(), 3 | 4 | 6 | 8) && hex.chars().all(|c| c.is_ascii_hexdigit())
                })
            },
            &[],
        )
    }

    /// Doesn't start with any of `prefixes`.
    pub fn doesnt_start_with(self, prefixes: &[&str]) -> Self {
        let values = prefixes.join(", ");
        self.text_rule(
            "doesnt_start_with",
            |t| !prefixes.iter().any(|p| t.starts_with(p)),
            &[("values", values)],
        )
    }

    /// Doesn't end with any of `suffixes`.
    pub fn doesnt_end_with(self, suffixes: &[&str]) -> Self {
        let values = suffixes.join(", ");
        self.text_rule(
            "doesnt_end_with",
            |t| !suffixes.iter().any(|s| t.ends_with(s)),
            &[("values", values)],
        )
    }

    /// The text does not match `pattern` (Laravel's `not_regex`).
    pub fn not_matches(mut self, pattern: &str) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value) {
            let matched = match cached_regex(pattern) {
                Ok(regex) => regex.is_match(text),
                Err(err) => {
                    tracing::error!(pattern, error = %err, "invalid pattern in a `not_matches` rule");
                    true
                }
            };
            if matched {
                self.fail("not_regex", &[]);
            }
        }
        self
    }

    /// A checkbox that must be ticked when `condition` holds.
    pub fn accepted_if(self, condition: bool) -> Self {
        if condition { self.accepted() } else { self }
    }

    /// Must be declined: an unticked checkbox (`false`), or `no`, `off`,
    /// `0` or `false`, e.g. "Don't share my data" answered no.
    pub fn declined(mut self) -> Self {
        let declined = match &self.value {
            Inspected::Bool(b) => !b,
            Inspected::Number(n) => *n == 0.0,
            Inspected::Text(text) => {
                matches!(
                    text.trim().to_ascii_lowercase().as_str(),
                    "no" | "off" | "0" | "false"
                )
            }
            _ => false,
        };
        if !self.failed && !declined {
            self.fail("declined", &[]);
        }
        self
    }

    /// Must be declined when `condition` holds; see [`declined`](Self::declined).
    pub fn declined_if(self, condition: bool) -> Self {
        if condition { self.declined() } else { self }
    }

    /// The value meets `policy` (length, letters, mixed case, numbers,
    /// symbols); see [`Password`].
    pub fn password(mut self, policy: &Password) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value) {
            match policy.broken(text) {
                Some((key, params)) => self.fail(key, &params),
                None if policy.uncompromised => {
                    let text = text.clone();
                    self.check(AsyncKind::Uncompromised(text));
                }
                None => {}
            }
        }
        self
    }

    /// The logged-in user's password (Laravel's `current_password`), e.g.
    /// before changing an email address. Checked last, once the other
    /// rules pass; it fails when no one is logged in. Needs
    /// [`Validator::finish_for`], which `Valid<T>` uses.
    pub fn current_password(mut self) -> Self {
        if let (true, Inspected::Text(text)) = (self.present(), &self.value) {
            let text = text.clone();
            self.check(AsyncKind::CurrentPassword(text));
        }
        self
    }

    /// A reusable rule; see [`Rule`].
    pub fn apply(mut self, rule: &impl Rule) -> Self {
        if !self.present() {
            return self;
        }
        if let Err(message) = rule.check(&self.value) {
            let message = render(&message, &self.label, &[]);
            self.v.errors.add(&self.name, message);
            self.failed = true;
            self.last_pending = None;
            self.last_check = None;
        }
        self
    }

    fn database(mut self, table: &str, column: &str, unique: bool) -> Self {
        if self.present() {
            self.v.pending.push(Pending {
                field: self.name.clone(),
                label: self.label.clone(),
                table: table.to_owned(),
                column: column.to_owned(),
                value: self.db_value.clone(),
                ignore_id: None,
                scope: Vec::new(),
                unique,
                message: None,
            });
            self.last_pending = Some(self.v.pending.len() - 1);
            self.last_check = None;
        }
        self
    }

    /// No row in `table` has this value in `column`.
    pub fn unique(self, table: &str, column: &str) -> Self {
        self.database(table, column, true)
    }

    /// Skips the row with this id in the preceding `unique`, for updates:
    /// `.unique("products", "sku").ignore(product.id)`, whatever the key's
    /// type (`i64`, `Ulid`, `Uuid`, `String`).
    pub fn ignore(self, id: impl crate::db::ToDbValue) -> Self {
        if let Some(i) = self.last_pending {
            self.v.pending[i].ignore_id = Some(id.to_db_value());
        }
        self
    }

    /// Some row in `table` has this value in `column`.
    pub fn exists(self, table: &str, column: &str) -> Self {
        self.database(table, column, false)
    }

    /// Only rows where `column = value` count for the preceding `unique` or
    /// `exists`, e.g. the current team's: SKUs are unique per team.
    ///
    /// ```
    /// # use renox::prelude::*;
    /// # struct ProductForm { id: i64, sku: String, category_id: i64, team_id: i64 }
    /// # impl Validate for ProductForm {
    /// fn rules(&self, v: &mut Validator) {
    ///     v.field("sku", &self.sku)
    ///         .unique("products", "sku")
    ///         .ignore(self.id)
    ///         .where_eq("team_id", self.team_id)
    ///         .where_null("deleted_at"); // soft-deleted rows don't count
    ///     v.field("category_id", &self.category_id)
    ///         .exists("categories", "id")
    ///         .where_eq("team_id", self.team_id); // not another team's category
    /// }
    /// # }
    /// ```
    pub fn where_eq(self, column: &str, value: impl ToDbValue) -> Self {
        self.scope(ScopeCondition::Eq(column.to_owned(), value.to_db_value()))
    }

    /// Only rows where `column` is null count (e.g. `deleted_at`).
    pub fn where_null(self, column: &str) -> Self {
        self.scope(ScopeCondition::Null(column.to_owned()))
    }

    /// Only rows where `column` is not null count.
    pub fn where_not_null(self, column: &str) -> Self {
        self.scope(ScopeCondition::NotNull(column.to_owned()))
    }

    fn scope(self, condition: ScopeCondition) -> Self {
        if let Some(i) = self.last_pending {
            self.v.pending[i].scope.push(condition);
        }
        self
    }
}

/// What a password must contain; `Field::password(&policy)` checks it.
/// The built-in register, reset and account forms use the app's policy
/// (`Auth::password_rules`), `Password::min(8)` by default.
///
/// ```
/// # use renox::prelude::*;
/// use renox::validation::Password;
/// # struct Form { password: String }
/// # impl Validate for Form {
/// fn rules(&self, v: &mut Validator) {
///     let policy = Password::min(12).mixed_case().numbers().symbols();
///     v.field("password", &self.password).required().password(&policy);
/// }
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Password {
    min: usize,
    letters: bool,
    mixed_case: bool,
    numbers: bool,
    symbols: bool,
    uncompromised: bool,
}

impl Password {
    /// At least `min` characters.
    pub fn min(min: usize) -> Self {
        Self {
            min,
            letters: false,
            mixed_case: false,
            numbers: false,
            symbols: false,
            uncompromised: false,
        }
    }

    /// Not a password found in a known data breach (Laravel's
    /// `uncompromised`), checked with Have I Been Pwned: only the first
    /// five characters of the password's SHA-1 leave the server
    /// (k-anonymity), through `state.http` (faked in tests). When the
    /// service can't be reached the password is allowed and a warning
    /// logged. Runs once the other rules pass.
    pub fn uncompromised(mut self) -> Self {
        self.uncompromised = true;
        self
    }

    /// At least one letter.
    pub fn letters(mut self) -> Self {
        self.letters = true;
        self
    }

    /// At least one uppercase and one lowercase letter.
    pub fn mixed_case(mut self) -> Self {
        self.mixed_case = true;
        self
    }

    /// At least one digit.
    pub fn numbers(mut self) -> Self {
        self.numbers = true;
        self
    }

    /// At least one character that isn't a letter, digit or space.
    pub fn symbols(mut self) -> Self {
        self.symbols = true;
        self
    }

    /// The first rule `password` breaks: a message key and its parameters.
    fn broken(&self, password: &str) -> Option<(&'static str, Vec<(&'static str, String)>)> {
        if password.chars().count() < self.min {
            return Some(("min.string", vec![("min", self.min.to_string())]));
        }
        let has = |test: fn(&char) -> bool| password.chars().any(|c| test(&c));
        if self.letters && !has(|c| c.is_alphabetic()) {
            return Some(("password.letters", Vec::new()));
        }
        if self.mixed_case && !(has(|c| c.is_uppercase()) && has(|c| c.is_lowercase())) {
            return Some(("password.mixed", Vec::new()));
        }
        if self.numbers && !has(|c| c.is_numeric()) {
            return Some(("password.numbers", Vec::new()));
        }
        if self.symbols && !has(|c| !c.is_alphanumeric() && !c.is_whitespace()) {
            return Some(("password.symbols", Vec::new()));
        }
        None
    }
}

impl Default for Password {
    fn default() -> Self {
        Self::min(8)
    }
}

/// A rule to reuse across forms, e.g. a tax number:
///
/// ```
/// # use renox::prelude::*;
/// use renox::validation::{Inspected, Rule};
///
/// struct TaxId;
///
/// impl Rule for TaxId {
///     fn check(&self, value: &Inspected) -> std::result::Result<(), String> {
///         let Inspected::Text(text) = value else { return Ok(()) };
///         let digits = text.chars().filter(char::is_ascii_digit).count();
///         if digits == 15 || digits == 16 {
///             Ok(())
///         } else {
///             Err("The :attribute must be a valid tax ID.".into()) // :attribute is the field's label
///         }
///     }
/// }
///
/// # struct Form { tax_id: String }
/// # impl Validate for Form {
/// fn rules(&self, v: &mut Validator) {
///     v.field("tax_id", &self.tax_id).required().apply(&TaxId);
/// }
/// # }
/// ```
///
/// Missing values skip the rule (combine it with `required`).
pub trait Rule {
    /// `Err(message)` when `value` breaks the rule.
    fn check(&self, value: &Inspected) -> std::result::Result<(), String>;
}

/// Limits for an uploaded image's size in pixels, for
/// [`Field::dimensions`] (Laravel's `dimensions`).
///
/// ```
/// # use renox::prelude::*;
/// use renox::validation::Dimensions;
/// # struct Form { banner: Option<Upload> }
/// # impl Validate for Form {
/// fn rules(&self, v: &mut Validator) {
///     let banner = Dimensions::new().min_width(1200).ratio(3, 1);
///     v.field("banner", &self.banner).image().dimensions(&banner);
/// }
/// # }
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Dimensions {
    min_width: Option<u32>,
    max_width: Option<u32>,
    min_height: Option<u32>,
    max_height: Option<u32>,
    width: Option<u32>,
    height: Option<u32>,
    ratio: Option<(u32, u32)>,
}

impl Dimensions {
    /// No limits yet.
    pub fn new() -> Self {
        Self::default()
    }

    /// At least `px` wide.
    pub fn min_width(mut self, px: u32) -> Self {
        self.min_width = Some(px);
        self
    }

    /// At most `px` wide.
    pub fn max_width(mut self, px: u32) -> Self {
        self.max_width = Some(px);
        self
    }

    /// At least `px` high.
    pub fn min_height(mut self, px: u32) -> Self {
        self.min_height = Some(px);
        self
    }

    /// At most `px` high.
    pub fn max_height(mut self, px: u32) -> Self {
        self.max_height = Some(px);
        self
    }

    /// Exactly `px` wide.
    pub fn width(mut self, px: u32) -> Self {
        self.width = Some(px);
        self
    }

    /// Exactly `px` high.
    pub fn height(mut self, px: u32) -> Self {
        self.height = Some(px);
        self
    }

    /// Width to height in this ratio, e.g. `ratio(16, 9)` or `ratio(1, 1)`
    /// for a square (to within a pixel of rounding).
    pub fn ratio(mut self, width: u32, height: u32) -> Self {
        self.ratio = Some((width, height));
        self
    }

    fn allows(&self, w: u32, h: u32) -> bool {
        let at_least = |limit: Option<u32>, v: u32| limit.is_none_or(|l| v >= l);
        let at_most = |limit: Option<u32>, v: u32| limit.is_none_or(|l| v <= l);
        let exactly = |limit: Option<u32>, v: u32| limit.is_none_or(|l| v == l);
        let ratio = self.ratio.is_none_or(|(rw, rh)| {
            // Within one pixel of the exact height for this width.
            rw > 0 && (h as f64 - w as f64 * rh as f64 / rw as f64).abs() <= 1.0
        });
        at_least(self.min_width, w)
            && at_most(self.max_width, w)
            && at_least(self.min_height, h)
            && at_most(self.max_height, h)
            && exactly(self.width, w)
            && exactly(self.height, h)
            && ratio
    }
}

/// The Have I Been Pwned range API (`app.fake_http()` answers it in tests).
const PWNED_RANGE: &str = "https://api.pwnedpasswords.com/range/";

/// Whether `password` appears in a known breach. Errors (no network, a
/// slow service) count as "no", with a warning, so sign-ups keep working.
async fn breached(state: &crate::AppState, password: &str) -> bool {
    use sha1::{Digest, Sha1};
    let hash: String = Sha1::digest(password.as_bytes())
        .iter()
        .map(|b| format!("{b:02X}"))
        .collect();
    let (prefix, suffix) = hash.split_at(5);
    let response = state
        .http
        .get(format!("{PWNED_RANGE}{prefix}"))
        .header("Add-Padding", "true")
        .timeout(std::time::Duration::from_secs(5))
        .send()
        .await;
    match response {
        Ok(response) if response.status().is_success() => response.text().lines().any(|line| {
            line.split_once(':').is_some_and(|(candidate, count)| {
                candidate.trim().eq_ignore_ascii_case(suffix)
                    && count.trim().parse::<u64>().is_ok_and(|n| n > 0)
            })
        }),
        Ok(response) => {
            tracing::warn!(status = %response.status(), "the password breach check answered with an error; allowing the password");
            false
        }
        Err(err) => {
            tracing::warn!(error = ?err, "the password breach check failed; allowing the password");
            false
        }
    }
}

/// Decimal places of a plain decimal number (`12`, `-3.50`), else `None`.
fn decimal_places(text: &str) -> Option<usize> {
    let digits = text.strip_prefix(['-', '+']).unwrap_or(text);
    let (whole, fraction) = digits.split_once('.').unwrap_or((digits, ""));
    let all_digits = |s: &str| s.chars().all(|c| c.is_ascii_digit());
    (!whole.is_empty()
        && all_digits(whole)
        && all_digits(fraction)
        && !(digits.contains('.') && fraction.is_empty()))
    .then_some(fraction.len())
}

/// The date a date rule compares with: a date value, or text that reads as
/// one (`"2026-01-01"`). Anything else leaves the rule out.
fn limit_date(limit: &impl FieldValue) -> Option<NaiveDateTime> {
    match limit.inspect() {
        Inspected::Date(date) => Some(date),
        Inspected::Text(text) => parse_date(text.trim()),
        _ => None,
    }
}

/// The value as a number: a number, or text that reads as a finite one.
fn numeric_value(value: &Inspected) -> Option<f64> {
    match value {
        Inspected::Number(n) => Some(*n),
        Inspected::Text(text) => {
            let text = text.trim();
            decimal_places(text)?;
            text.parse::<f64>().ok().filter(|n| n.is_finite())
        }
        _ => None,
    }
}

/// How many digits an integer value has (the sign doesn't count).
fn integer_digits(value: &Inspected) -> Option<usize> {
    let n = numeric_value(value)?;
    (n.fract() == 0.0).then(|| format!("{}", n.abs() as u64).len())
}

fn is_mac_address(text: &str) -> bool {
    let text = text.trim();
    let hex = |s: &str, n: usize| s.len() == n && s.chars().all(|c| c.is_ascii_hexdigit());
    for separator in [':', '-'] {
        let parts: Vec<&str> = text.split(separator).collect();
        if parts.len() == 6 && parts.iter().all(|p| hex(p, 2)) {
            return true;
        }
    }
    let parts: Vec<&str> = text.split('.').collect();
    parts.len() == 3 && parts.iter().all(|p| hex(p, 4))
}

fn is_uuid(text: &str) -> bool {
    let groups: Vec<&str> = text.trim().split('-').collect();
    groups.len() == 5
        && groups
            .iter()
            .zip([8, 4, 4, 4, 12])
            .all(|(g, n)| g.len() == n && g.chars().all(|c| c.is_ascii_hexdigit()))
}

/// Compiled patterns, so a rule in a hot form doesn't recompile each time.
fn cached_regex(pattern: &str) -> std::result::Result<regex::Regex, regex::Error> {
    static CACHE: std::sync::LazyLock<
        std::sync::Mutex<std::collections::HashMap<String, regex::Regex>>,
    > = std::sync::LazyLock::new(Default::default);
    let mut cache = CACHE.lock().unwrap_or_else(|e| e.into_inner());
    if let Some(regex) = cache.get(pattern) {
        return Ok(regex.clone());
    }
    let regex = regex::Regex::new(pattern)?;
    if cache.len() < 1000 {
        cache.insert(pattern.to_owned(), regex.clone());
    }
    Ok(regex)
}

/// How many digits the value is made of, if it's only digits.
fn digit_count(value: &Inspected) -> Option<usize> {
    let text = match value {
        Inspected::Text(text) => text.trim().to_owned(),
        Inspected::Number(n) if n.fract() == 0.0 && *n >= 0.0 => format!("{}", *n as u64),
        _ => return None,
    };
    (!text.is_empty() && text.chars().all(|c| c.is_ascii_digit())).then_some(text.len())
}

/// `2026-10-01`, `2026-10-01T10:30`, `2026-10-01 10:30:00` or RFC 3339.
fn parse_date(text: &str) -> Option<NaiveDateTime> {
    if let Ok(date) = chrono::NaiveDate::parse_from_str(text, "%Y-%m-%d") {
        return Some(date.and_time(chrono::NaiveTime::MIN));
    }
    for format in [
        "%Y-%m-%dT%H:%M",
        "%Y-%m-%dT%H:%M:%S",
        "%Y-%m-%d %H:%M",
        "%Y-%m-%d %H:%M:%S",
    ] {
        if let Ok(date) = NaiveDateTime::parse_from_str(text, format) {
            return Some(date);
        }
    }
    chrono::DateTime::parse_from_rfc3339(text)
        .ok()
        .map(|d| d.naive_utc())
}

fn is_email(text: &str) -> bool {
    let Some((local, domain)) = text.split_once('@') else {
        return false;
    };
    !local.is_empty()
        && !text.chars().any(char::is_whitespace)
        && !domain.contains('@')
        && domain.contains('.')
        && domain.split('.').all(|part| !part.is_empty())
}

fn is_url(text: &str) -> bool {
    let rest = text
        .strip_prefix("https://")
        .or_else(|| text.strip_prefix("http://"));
    matches!(rest, Some(rest) if !rest.is_empty()
        && !rest.starts_with('/')
        && !text.chars().any(char::is_whitespace))
}

/// Keys never flashed back into forms.
const DONT_FLASH: &[&str] = &[
    "password",
    "password_confirmation",
    "current_password",
    "_token",
];

/// A failed validation. As a response it is `422` with
/// `{"message": ..., "errors": {...}}`; for regular (non-HTMX, non-JSON)
/// requests Renox turns it into a redirect back with the errors and old
/// input flashed to the session.
///
/// Return it from a handler for errors found after validation:
///
/// ```
/// # use renox::prelude::*;
/// # #[derive(serde::Serialize)] struct StockForm { quantity: i64 }
/// # fn demo(form: StockForm) -> Result {
/// let mut errors = Errors::new();
/// errors.add("quantity", "Not enough stock.");
/// return Err(ValidationError::new(errors).with_input(&form).into());
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct ValidationError {
    /// The messages, keyed by field name.
    pub errors: Errors,
    /// The submitted values flashed back into the form (never passwords).
    pub input: Map<String, Value>,
    /// The named error bag (`in_bag`), for a page with several forms.
    bag: Option<String>,
}

impl ValidationError {
    /// An error with these messages and no input to refill.
    pub fn new(errors: Errors) -> Self {
        Self {
            errors,
            input: Map::new(),
            bag: None,
        }
    }

    /// Flashes the errors in the named bag `bag` (Laravel's error bags) when
    /// the form is sent back: the page shows them with
    /// `error('email', bag='login')`, and the default `error('email')` of
    /// another form on the page stays empty. `Valid<T>` does it for
    /// [`Validate::ERROR_BAG`].
    pub fn in_bag(mut self, bag: impl Into<String>) -> Self {
        self.bag = Some(bag.into());
        self
    }

    /// The named error bag, if any.
    pub fn bag(&self) -> Option<&str> {
        self.bag.as_deref()
    }

    /// The submitted values to refill the form with (passwords are dropped).
    pub fn with_input(mut self, input: &impl Serialize) -> Self {
        if let Ok(Value::Object(map)) = serde_json::to_value(input) {
            self.input = map;
        }
        self.input
            .retain(|key, _| !DONT_FLASH.contains(&key.as_str()));
        self
    }

    pub(crate) fn with_input_map(mut self, input: Map<String, Value>) -> Self {
        self.input = input;
        self.input
            .retain(|key, _| !DONT_FLASH.contains(&key.as_str()));
        self
    }
}

impl From<Errors> for ValidationError {
    fn from(errors: Errors) -> Self {
        Self::new(errors)
    }
}

impl IntoResponse for ValidationError {
    fn into_response(self) -> Response {
        let message = self
            .errors
            .iter()
            .next()
            .and_then(|(_, m)| m.first().cloned())
            .unwrap_or_default();
        let body = json!({ "message": message, "errors": self.errors });
        let mut res = (StatusCode::UNPROCESSABLE_ENTITY, axum::Json(body)).into_response();
        res.extensions_mut().insert(self);
        res
    }
}

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

    #[test]
    fn checks_emails_and_urls() {
        assert!(is_email("alex@example.com"));
        assert!(!is_email("alex@localhost"));
        assert!(!is_email("alex example@x.com"));
        assert!(!is_email("@x.com"));
        assert!(is_url("https://renox.dev/docs"));
        assert!(!is_url("ftp://renox.dev"));
        assert!(!is_url("https://"));
    }

    #[test]
    fn formats_numbers_without_trailing_zeros() {
        assert_eq!(number(3.0), "3");
        assert_eq!(number(2.5), "2.5");
    }
    /// A value that inspects as whatever a test needs (a file, a bool).
    struct As(Inspected);

    impl FieldValue for As {
        fn inspect(&self) -> Inspected {
            self.0.clone()
        }
        fn db_value(&self) -> DbValue {
            DbValue::Null
        }
    }

    /// A rule that fails every value it sees.
    struct Never;

    impl Rule for Never {
        fn check(&self, _: &Inspected) -> std::result::Result<(), String> {
            Err("never".into())
        }
    }

    fn file(kilobytes: f64, dimensions: Option<(u32, u32)>) -> As {
        As(Inspected::File {
            kilobytes,
            extension: "png".into(),
            image: true,
            dimensions,
        })
    }

    // #250: the branches rules take for values they don't measure, and the
    // passing side of rules only failures had reached.
    #[test]
    fn rules_pass_or_step_aside_for_values_they_dont_judge() {
        let mut v = Validator::default();
        let photo = file(120.0, Some((800, 600)));
        v.field("photo", &photo)
            .mimes(&["PNG", "jpeg"])
            .dimensions(&Dimensions::new().min_width(400).max_height(600));
        v.field("code", &"AB1234")
            .matches(r"^[A-Z]{2}\d{4}$")
            .not_matches(r"^\d+$");
        v.field("secret", &"long enough password")
            .password(&Password::min(8));
        // Rules that can't read the value leave it alone.
        let missing: Option<String> = None;
        v.field("note", &missing)
            .decimal(2, 2)
            .apply(&Never)
            .gt("other", &"5");
        v.field("price", &"12").gt("budget", &missing);
        // A limit that isn't a date leaves the date rules out.
        v.field("day", &"2026-01-01")
            .before_or_equal(5)
            .after(true)
            .after_or_equal(2.5);
        assert!(v.errors.is_empty(), "{:?}", v.errors);
    }

    #[test]
    fn rules_fail_values_of_the_wrong_kind() {
        let mut v = Validator::default();
        // A number isn't a date; a bool has no digits, no decimals, no number.
        v.field("day", &7).before("2026-01-01");
        v.field("agree", &true).digits(3);
        v.field("flag", &true).decimal(2, 2);
        v.field("on", &true).numeric();
        // Two files compare by size; two booleans can't be compared.
        v.field("photo", &file(10.0, None))
            .gt("thumb", &file(20.0, None));
        v.field("yes", &true).lt("no", &false);
        assert_eq!(v.errors.first("day"), Some("The day is not a valid date."));
        assert_eq!(v.errors.first("agree"), Some("The agree must be 3 digits."));
        assert_eq!(
            v.errors.first("flag"),
            Some("The flag must have 2 decimal places.")
        );
        assert_eq!(v.errors.first("on"), Some("The on must be a number."));
        assert_eq!(
            v.errors.first("photo"),
            Some("The photo must be larger than thumb.")
        );
        assert!(v.errors.has("yes"), "{:?}", v.errors);
        // An image too wide fails `dimensions`.
        let mut v = Validator::new();
        v.field("photo", &file(1.0, Some((2000, 10))))
            .dimensions(&Dimensions::new().max_width(100));
        assert!(v.errors.has("photo"));
    }

    #[test]
    fn nested_rows_carry_their_async_checks_under_the_row_name() {
        struct Row {
            password: String,
        }
        impl Validate for Row {
            fn rules(&self, v: &mut Validator) {
                v.field("password", &self.password).current_password();
            }
        }
        let mut v = Validator::new();
        v.nested(
            "rows",
            &[
                Row {
                    password: "a".into(),
                },
                Row {
                    password: "b".into(),
                },
            ],
        );
        let fields: Vec<_> = v.checks.iter().map(|c| c.field.as_str()).collect();
        assert_eq!(fields, ["rows.0.password", "rows.1.password"]);
    }

    #[tokio::test]
    async fn checks_on_a_field_that_already_failed_are_skipped() {
        let db = crate::db::connect(&crate::Config::default()).await.unwrap();
        let mut v = Validator::new();
        // The field failed a rule before its database and async checks were
        // added: neither runs (no `users` table is asked), one error stays.
        v.field("email", &"not an email").email();
        v.field("email", &"not an email")
            .unique("no_such_table", "email")
            .current_password();
        // `.message` after an async check is that check's message.
        v.field("password", &"guess")
            .current_password()
            .message("That isn't your password.");
        let errors = v.finish(&db).await.unwrap();
        assert_eq!(
            errors.first("email"),
            Some("The email must be a valid email address.")
        );
        assert_eq!(
            errors.iter().find(|(f, _)| *f == "email").unwrap().1.len(),
            1
        );
        assert_eq!(errors.first("password"), Some("That isn't your password."));
    }
}