flyer 2.1.3

HTTP framework for rust
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
use crate::request::form::Form;
use regex::Regex;
use chrono::{NaiveDate, NaiveDateTime};
use std::net::IpAddr;
use std::str::FromStr;
use reqwest::Url;
use uuid::Uuid;
use ulid::Ulid;
use serde_json::Value as JsonValue;

fn pretty(value: String) -> String {
    let temp: Vec<&str> = value.split('_').collect();
    temp.join(" ")
}

fn get_value(form: &Form, field: &str) -> Option<String> {
    form.values.get(field).cloned()
}

pub fn is_empty(form: &Form, field: &str) -> bool {
    if let Some(val) = form.values.get(field) {
        return val.is_empty();
    }
    if form.files.get(field).is_some() {
        return false;
    }
    true
}

fn is_present(form: &Form, field: &str) -> bool {
    form.values.contains_key(field) || form.files.contains_key(field)
}

// Booleans
pub async fn accepted(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        let accepted_vals = vec!["yes", "on", "1", "true"];
        if accepted_vals.contains(&val.to_lowercase().as_str()) || val == "1" {
            return None;
        }
    }
    Some(format!("The {} must be accepted", pretty(field)))
}

pub async fn accepted_if(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];

    if let Some(val) = get_value(form, another_field) {
        if val == *expected_val {
            return accepted(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn boolean(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        let bool_vals = vec!["true", "false", "1", "0", "on", "off", "yes", "no"];
        if bool_vals.contains(&val.to_lowercase().as_str()) {
            return None;
        }
    }
    Some(format!("The {} must be a boolean", pretty(field)))
}

pub async fn declined(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        let declined_vals = vec!["no", "off", "0", "false"];
        if declined_vals.contains(&val.to_lowercase().as_str()) {
            return None;
        }
    }
    Some(format!("The {} must be declined", pretty(field)))
}

pub async fn declined_if(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];

    if let Some(val) = get_value(form, another_field) {
        if val == *expected_val {
            return declined(form, field, Vec::new()).await;
        }
    }
    None
}

// Strings
pub async fn active_url(_form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(_form, &field) {
        if let Ok(url) = Url::parse(&val) {
            if let Some(host) = url.host_str() {
                if tokio::net::lookup_host(format!("{}:80", host)).await.is_ok() {
                    return None;
                }
            }
        }
    }
    Some(format!("The {} is not a valid active URL", pretty(field)))
}

pub async fn alpha(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if !val.is_empty() && val.chars().all(|c| c.is_alphabetic()) {
            return None;
        }
    }
    Some(format!("The {} must only contain alphabetic characters", pretty(field)))
}

pub async fn alpha_dash(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if !val.is_empty() && val.chars().all(|c| c.is_alphanumeric() || c == '-' || c == '_') {
            return None;
        }
    }
    Some(format!("The {} must only contain letters, numbers, dashes and underscores", pretty(field)))
}

pub async fn alpha_numeric(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if !val.is_empty() && val.chars().all(|c| c.is_alphanumeric()) {
            return None;
        }
    }
    Some(format!("The {} must only contain alphanumeric characters", pretty(field)))
}

pub async fn ascii(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if val.is_ascii() {
            return None;
        }
    }
    Some(format!("The {} must only contain ASCII characters", pretty(field)))
}

pub async fn confirmed(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    let confirmation_field = format!("{}_confirmation", field);
    if let Some(val) = get_value(form, &field) {
        if let Some(conf) = get_value(form, &confirmation_field) {
            if val == conf {
                return None;
            }
        }
    }
    Some(format!("The {} confirmation does not match", pretty(field)))
}

pub async fn different(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if let Some(val) = get_value(form, &field) {
        if let Some(another_val) = get_value(form, another_field) {
            if val != another_val {
                return None;
            }
        } else {
            return None; 
        }
    }
    Some(format!("The {} must be different from {}", pretty(field), pretty(another_field.clone())))
}

pub async fn doesnt_start_with(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        for arg in &args {
            if val.starts_with(arg) {
                return Some(format!("The {} must not start with {}", pretty(field), arg));
            }
        }
    }
    None
}

pub async fn doesnt_end_with(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        for arg in &args {
            if val.ends_with(arg) {
                return Some(format!("The {} must not end with {}", pretty(field), arg));
            }
        }
    }
    None
}

pub async fn email(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        let email_regex = Regex::new(r"^[^\s@]+@[^\s@]+\.[^\s@]+$").unwrap();
        if email_regex.is_match(&val) { return None; }
    }
    Some(format!("The {} must be a valid email address", pretty(field)))
}

pub async fn ends_with(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        for arg in &args {
            if val.ends_with(arg) {
                return None;
            }
        }
    }
    Some(format!("The {} must end with one of: {}", pretty(field), args.join(", ")))
}

pub async fn hex_color(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        let hex_regex = Regex::new(r"^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$").unwrap();
        if hex_regex.is_match(&val) { return None; }
    }
    Some(format!("The {} must be a valid hexadecimal color", pretty(field)))
}

pub async fn in_rule(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if args.contains(&val) {
            return None;
        }
    }
    Some(format!("The selected {} is invalid", pretty(field)))
}

pub async fn ip(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if IpAddr::from_str(&val).is_ok() { return None; }
    }
    Some(format!("The {} must be a valid IP address", pretty(field)))
}

pub async fn ipv4(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if let Ok(IpAddr::V4(_)) = IpAddr::from_str(&val) { return None; }
    }
    Some(format!("The {} must be a valid IPv4 address", pretty(field)))
}

pub async fn ipv6(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if let Ok(IpAddr::V6(_)) = IpAddr::from_str(&val) { return None; }
    }
    Some(format!("The {} must be a valid IPv6 address", pretty(field)))
}

pub async fn json(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if serde_json::from_str::<JsonValue>(&val).is_ok() { return None; }
    }
    Some(format!("The {} must be a valid JSON string", pretty(field)))
}

pub async fn lowercase(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if val == val.to_lowercase() { return None; }
    }
    Some(format!("The {} must be lowercase", pretty(field)))
}

pub async fn mac_address(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        let mac_regex = Regex::new(r"^([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})$").unwrap();
        if mac_regex.is_match(&val) { return None; }
    }
    Some(format!("The {} must be a valid MAC address", pretty(field)))
}

pub async fn not_in(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if !args.contains(&val) {
            return None;
        }
    }
    Some(format!("The selected {} is invalid", pretty(field)))
}

pub async fn regex(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    if let Some(val) = get_value(form, &field) {
        if let Ok(re) = Regex::new(&args[0]) {
            if re.is_match(&val) { return None; }
        }
    }
    Some(format!("The {} format is invalid", pretty(field)))
}

pub async fn not_regex(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    if let Some(val) = get_value(form, &field) {
        if let Ok(re) = Regex::new(&args[0]) {
            if !re.is_match(&val) { return None; }
        }
    }
    Some(format!("The {} format is invalid", pretty(field)))
}

pub async fn same(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if let Some(val) = get_value(form, &field) {
        if let Some(another_val) = get_value(form, another_field) {
            if val == another_val { return None; }
        }
    }
    Some(format!("The {} and {} must match", pretty(field), pretty(another_field.clone())))
}

pub async fn starts_with(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        for arg in &args {
            if val.starts_with(arg) {
                return None;
            }
        }
    }
    Some(format!("The {} must start with one of: {}", pretty(field), args.join(", ")))
}

pub async fn uppercase(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if val == val.to_uppercase() { return None; }
    }
    Some(format!("The {} must be uppercase", pretty(field)))
}

pub async fn url(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if Url::parse(&val).is_ok() { return None; }
    }
    Some(format!("The {} must be a valid URL", pretty(field)))
}

pub async fn uuid(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if Uuid::parse_str(&val).is_ok() { return None; }
    }
    Some(format!("The {} must be a valid UUID", pretty(field)))
}

pub async fn ulid(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if Ulid::from_str(&val).is_ok() { return None; }
    }
    Some(format!("The {} must be a valid ULID", pretty(field)))
}

// Numbers
pub async fn between(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let min: f64 = args[0].parse().unwrap_or(0.0);
    let max: f64 = args[1].parse().unwrap_or(0.0);

    if let Some(val) = get_value(form, &field) {
        if let Ok(num) = val.parse::<f64>() {
            if num >= min && num <= max { return None; }
            return Some(format!("The {} must be between {} and {}", pretty(field), min, max));
        }
        if val.len() >= min as usize && val.len() <= max as usize { return None; }
        return Some(format!("The {} must be between {} and {} characters", pretty(field), min, max));
    }
    if let Some(file) = form.files.get(&field) {
        let size_kb = file.content.len() / 1024;
        if size_kb >= min as usize && size_kb <= max as usize { return None; }
        return Some(format!("The {} must be between {} and {} kilobytes", pretty(field), min, max));
    }
    None
}

pub async fn decimal(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if let Ok(_) = val.parse::<f64>() {
            if val.contains('.') {
                let parts: Vec<&str> = val.split('.').collect();
                if parts.len() == 2 {
                    let decimal_places = parts[1].len();
                    if args.len() >= 1 {
                        let min: usize = args[0].parse().unwrap_or(0);
                        if args.len() >= 2 {
                             let max: usize = args[1].parse().unwrap_or(min);
                             if decimal_places >= min && decimal_places <= max { return None; }
                             return Some(format!("The {} must have between {} and {} decimal places", pretty(field), min, max));
                        }
                        if decimal_places == min { return None; }
                        return Some(format!("The {} must have {} decimal places", pretty(field), min));
                    }
                    return None;
                }
            }
        }
    }
    Some(format!("The {} must be a decimal", pretty(field)))
}

pub async fn digits(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let len: usize = args[0].parse().unwrap_or(0);
    if let Some(val) = get_value(form, &field) {
        if val.chars().all(|c| c.is_numeric()) && val.len() == len {
            return None;
        }
    }
    Some(format!("The {} must be {} digits", pretty(field), len))
}

pub async fn digits_between(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let min: usize = args[0].parse().unwrap_or(0);
    let max: usize = args[1].parse().unwrap_or(0);
    if let Some(val) = get_value(form, &field) {
        if val.chars().all(|c| c.is_numeric()) && val.len() >= min && val.len() <= max {
            return None;
        }
    }
    Some(format!("The {} must be between {} and {} digits", pretty(field), min, max))
}

pub async fn gt(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if let Some(val) = get_value(form, &field) {
        if let Ok(v1) = val.parse::<f64>() {
            if let Some(another_val) = get_value(form, another_field) {
                if let Ok(v2) = another_val.parse::<f64>() {
                    if v1 > v2 { return None; }
                }
            } else if let Ok(v2) = another_field.parse::<f64>() {
                 if v1 > v2 { return None; }
            }
        }
    }
    Some(format!("The {} must be greater than {}", pretty(field), pretty(another_field.clone())))
}

pub async fn gte(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if let Some(val) = get_value(form, &field) {
        if let Ok(v1) = val.parse::<f64>() {
            if let Some(another_val) = get_value(form, another_field) {
                if let Ok(v2) = another_val.parse::<f64>() {
                    if v1 >= v2 { return None; }
                }
            } else if let Ok(v2) = another_field.parse::<f64>() {
                 if v1 >= v2 { return None; }
            }
        }
    }
    Some(format!("The {} must be greater than or equal {}", pretty(field), pretty(another_field.clone())))
}

pub async fn integer(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if val.parse::<i128>().is_ok() { return None; }
    }
    Some(format!("The {} must be an integer", pretty(field)))
}

pub async fn lt(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if let Some(val) = get_value(form, &field) {
        if let Ok(v1) = val.parse::<f64>() {
            if let Some(another_val) = get_value(form, another_field) {
                if let Ok(v2) = another_val.parse::<f64>() {
                    if v1 < v2 { return None; }
                }
            } else if let Ok(v2) = another_field.parse::<f64>() {
                 if v1 < v2 { return None; }
            }
        }
    }
    Some(format!("The {} must be less than {}", pretty(field), pretty(another_field.clone())))
}

pub async fn lte(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if let Some(val) = get_value(form, &field) {
        if let Ok(v1) = val.parse::<f64>() {
            if let Some(another_val) = get_value(form, another_field) {
                if let Ok(v2) = another_val.parse::<f64>() {
                    if v1 <= v2 { return None; }
                }
            } else if let Ok(v2) = another_field.parse::<f64>() {
                 if v1 <= v2 { return None; }
            }
        }
    }
    Some(format!("The {} must be less than or equal {}", pretty(field), pretty(another_field.clone())))
}

pub async fn max(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let max_val: f64 = args[0].parse().unwrap_or(0.0);

    if let Some(val) = get_value(form, &field) {
        if let Ok(num) = val.parse::<f64>() {
            if num <= max_val { return None; }
            return Some(format!("The {} must not be greater than {}", pretty(field), max_val));
        }
        if val.len() <= max_val as usize { return None; }
        return Some(format!("The {} must not be greater than {} characters", pretty(field), max_val));
    }
    if let Some(file) = form.files.get(&field) {
        let size_kb = file.content.len() / 1024;
        if size_kb <= max_val as usize { return None; }
        return Some(format!("The {} must not be greater than {} kilobytes", pretty(field), max_val));
    }
    None
}

pub async fn min(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let min_val: f64 = args[0].parse().unwrap_or(0.0);

    if let Some(val) = get_value(form, &field) {
        if let Ok(num) = val.parse::<f64>() {
            if num >= min_val { return None; }
            return Some(format!("The {} must be at least {}", pretty(field), min_val));
        }
        if val.len() >= min_val as usize { return None; }
        return Some(format!("The {} must be at least {} characters", pretty(field), min_val));
    }
    if let Some(file) = form.files.get(&field) {
        let size_kb = file.content.len() / 1024;
        if size_kb >= min_val as usize { return None; }
        return Some(format!("The {} must be at least {} kilobytes", pretty(field), min_val));
    }
    None
}

pub async fn max_digits(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let max: usize = args[0].parse().unwrap_or(0);
    if let Some(val) = get_value(form, &field) {
        if val.chars().all(|c| c.is_numeric()) && val.len() <= max {
            return None;
        }
    }
    Some(format!("The {} must not have more than {} digits", pretty(field), max))
}

pub async fn min_digits(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let min: usize = args[0].parse().unwrap_or(0);
    if let Some(val) = get_value(form, &field) {
        if val.chars().all(|c| c.is_numeric()) && val.len() >= min {
            return None;
        }
    }
    Some(format!("The {} must have at least {} digits", pretty(field), min))
}

pub async fn multiple_of(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let factor: f64 = args[0].parse().unwrap_or(1.0);
    if let Some(val) = get_value(form, &field) {
        if let Ok(num) = val.parse::<f64>() {
            if num % factor == 0.0 { return None; }
        }
    }
    Some(format!("The {} must be a multiple of {}", pretty(field), factor))
}

pub async fn numeric(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if val.parse::<f64>().is_ok() { return None; }
    }
    Some(format!("The {} must be a number", pretty(field)))
}

// Dates
fn parse_date(date_str: &str) -> Option<NaiveDateTime> {
    let formats = vec![
        "%Y-%m-%d %H:%M:%S",
        "%Y-%m-%d",
        "%d-%m-%Y",
        "%m/%d/%Y",
    ];
    for format in formats {
        if let Ok(dt) = NaiveDateTime::parse_from_str(date_str, format) {
            return Some(dt);
        }
        if let Ok(d) = NaiveDate::parse_from_str(date_str, format) {
            return Some(d.and_hms_opt(0, 0, 0).unwrap());
        }
    }
    None
}

pub async fn after(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let target_date_str = if let Some(val) = get_value(form, &args[0]) { val } else { args[0].clone() };
    if let Some(val) = get_value(form, &field) {
        if let (Some(d1), Some(d2)) = (parse_date(&val), parse_date(&target_date_str)) {
            if d1 > d2 { return None; }
        }
    }
    Some(format!("The {} must be a date after {}", pretty(field), target_date_str))
}

pub async fn after_or_equal(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let target_date_str = if let Some(val) = get_value(form, &args[0]) { val } else { args[0].clone() };
    if let Some(val) = get_value(form, &field) {
        if let (Some(d1), Some(d2)) = (parse_date(&val), parse_date(&target_date_str)) {
            if d1 >= d2 { return None; }
        }
    }
    Some(format!("The {} must be a date after or equal to {}", pretty(field), target_date_str))
}

pub async fn before(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let target_date_str = if let Some(val) = get_value(form, &args[0]) { val } else { args[0].clone() };
    if let Some(val) = get_value(form, &field) {
        if let (Some(d1), Some(d2)) = (parse_date(&val), parse_date(&target_date_str)) {
            if d1 < d2 { return None; }
        }
    }
    Some(format!("The {} must be a date before {}", pretty(field), target_date_str))
}

pub async fn before_or_equal(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let target_date_str = if let Some(val) = get_value(form, &args[0]) { val } else { args[0].clone() };
    if let Some(val) = get_value(form, &field) {
        if let (Some(d1), Some(d2)) = (parse_date(&val), parse_date(&target_date_str)) {
            if d1 <= d2 { return None; }
        }
    }
    Some(format!("The {} must be a date before or equal to {}", pretty(field), target_date_str))
}

pub async fn date(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(val) = get_value(form, &field) {
        if parse_date(&val).is_some() { return None; }
    }
    Some(format!("The {} is not a valid date", pretty(field)))
}

pub async fn date_equals(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let target_date_str = if let Some(val) = get_value(form, &args[0]) { val } else { args[0].clone() };
    if let Some(val) = get_value(form, &field) {
        if let (Some(d1), Some(d2)) = (parse_date(&val), parse_date(&target_date_str)) {
            if d1 == d2 { return None; }
        }
    }
    Some(format!("The {} must be a date equal to {}", pretty(field), target_date_str))
}

pub async fn date_format(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let format = &args[0];
    if let Some(val) = get_value(form, &field) {
        if NaiveDateTime::parse_from_str(&val, format).is_ok() || NaiveDate::parse_from_str(&val, format).is_ok() {
            return None;
        }
    }
    Some(format!("The {} does not match the format {}", pretty(field), format))
}

// Files
pub async fn file(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if form.files.get(&field).is_some() { return None; }
    Some(format!("The {} must be a file", pretty(field)))
}

pub async fn image(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if let Some(file) = form.files.get(&field) {
        let image_mimes = vec!["image/jpeg", "image/png", "image/gif", "image/bmp", "image/svg+xml", "image/webp"];
        if image_mimes.contains(&file.mime.as_str()) {
            return None;
        }
    }
    Some(format!("The {} must be an image", pretty(field)))
}

pub async fn mimetypes(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(file) = form.files.get(&field) {
        if args.contains(&file.mime) {
            return None;
        }
    }
    Some(format!("The {} must be a file of type: {}", pretty(field), args.join(", ")))
}

pub async fn mimes(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if let Some(file) = form.files.get(&field) {
        let parts: Vec<&str> = file.name.split('.').collect();
        if let Some(ext) = parts.last() {
            if args.contains(&ext.to_string()) {
                return None;
            }
        }
    }
    Some(format!("The {} must be a file of type: {}", pretty(field), args.join(", ")))
}

pub async fn extensions(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    mimes(form, field, args).await
}

// Utilities
pub async fn required(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if !is_empty(form, &field) { return None; }
    Some(format!("The {} field is required", pretty(field)))
}

pub async fn required_if(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];

    if let Some(val) = get_value(form, another_field) {
        if val == *expected_val {
            return required(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn required_if_accepted(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let another_field = &args[0];
    if accepted(form, another_field.clone(), Vec::new()).await.is_none() {
        return required(form, field, Vec::new()).await;
    }
    None
}

pub async fn required_unless(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];

    if let Some(val) = get_value(form, another_field) {
        if val != *expected_val {
            return required(form, field, Vec::new()).await;
        }
    } else {
        return required(form, field, Vec::new()).await;
    }
    None
}

pub async fn required_with(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    for arg in &args {
        if !is_empty(form, arg) {
            return required(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn required_without(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    for arg in &args {
        if is_empty(form, arg) {
            return required(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn required_with_all(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    for arg in &args {
        if is_empty(form, arg) {
            return None;
        }
    }
    required(form, field, Vec::new()).await
}

pub async fn required_without_all(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    for arg in &args {
        if !is_empty(form, arg) {
            return None;
        }
    }
    required(form, field, Vec::new()).await
}

pub async fn prohibited(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if is_empty(form, &field) { return None; }
    Some(format!("The {} field is prohibited", pretty(field)))
}

pub async fn prohibited_if(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];

    if let Some(val) = get_value(form, another_field) {
        if val == *expected_val {
            return prohibited(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn prohibited_unless(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];

    if let Some(val) = get_value(form, another_field) {
        if val != *expected_val {
            return prohibited(form, field, Vec::new()).await;
        }
    } else {
        return prohibited(form, field, Vec::new()).await;
    }
    None
}

pub async fn prohibited_with(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    for arg in &args {
        if !is_empty(form, arg) {
            return prohibited(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn prohibited_with_all(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    for arg in &args {
        if is_empty(form, arg) {
            return None;
        }
    }
    prohibited(form, field, Vec::new()).await
}

pub async fn filled(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if is_present(form, &field) && is_empty(form, &field) {
        return Some(format!("The {} field must have a value", pretty(field)));
    }
    None
}

pub async fn missing(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if is_present(form, &field) {
        return Some(format!("The {} field must be missing", pretty(field)));
    }
    None
}

pub async fn missing_if(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];
    if let Some(val) = get_value(form, another_field) {
        if val == *expected_val {
            return missing(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn missing_unless(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];
    if let Some(val) = get_value(form, another_field) {
        if val != *expected_val {
            return missing(form, field, Vec::new()).await;
        }
    } else {
        return missing(form, field, Vec::new()).await;
    }
    None
}

pub async fn present(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if !is_present(form, &field) {
        return Some(format!("The {} field must be present", pretty(field)));
    }
    None
}

pub async fn present_if(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];
    if let Some(val) = get_value(form, another_field) {
        if val == *expected_val {
            return present(form, field, Vec::new()).await;
        }
    }
    None
}

pub async fn present_unless(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.len() < 2 { return None; }
    let another_field = &args[0];
    let expected_val = &args[1];
    if let Some(val) = get_value(form, another_field) {
        if val != *expected_val {
            return present(form, field, Vec::new()).await;
        }
    } else {
        return present(form, field, Vec::new()).await;
    }
    None
}

pub async fn string(form: &Form, field: String, _args: Vec<String>) -> Option<String> {
    if get_value(form, &field).is_some() { return None; }
    Some(format!("The {} must be a string", pretty(field)))
}

pub async fn size(form: &Form, field: String, args: Vec<String>) -> Option<String> {
    if args.is_empty() { return None; }
    let size: usize = args[0].parse().unwrap_or(0);

    if let Some(val) = get_value(form, &field) {
        if let Ok(num) = val.parse::<f64>() {
            if num == size as f64 { return None; }
            return Some(format!("The {} must be {}", pretty(field), size));
        }
        if val.len() == size { return None; }
        return Some(format!("The {} must be {} characters", pretty(field), size));
    }
    if let Some(file) = form.files.get(&field) {
        let size_kb = file.content.len() / 1024;
        if size_kb == size { return None; }
        return Some(format!("The {} must be {} kilobytes", pretty(field), size));
    }
    None
}