dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Validation Module
//!
//! This module provides comprehensive validation utilities for Ri, including
//! input validation, schema validation, and data verification. It supports
//! various validation rules and provides detailed error messages.
//!
//! ## Key Components
//!
//! - **RiValidator**: Core validation trait
//! - **RiValidationRule**: Individual validation rule
//! - **RiValidationResult**: Validation result with details
//! - **Built-in Validators**: Email, URL, length, pattern, range, etc.
//!
//! ## Design Principles
//!
//! 1. **Composable**: Rules can be combined using `and`, `or`, `not`
//! 2. **Extensible**: Easy to implement custom validation rules
//! 3. **Type-safe**: Strongly typed validation for different data types
//! 4. **Informative**: Detailed error messages with field locations
//! 5. **Async Support**: Async validation for I/O-bound checks
//! 6. **Schema Validation**: JSON Schema support for complex structures
//!
//! ## Usage
//!
//! ```rust,ignore
//! use ri::validation::{Validator, ValidationRule, RiValidator};
//! use ri::prelude::*;
//!
//! let validator = RiValidator::new("user_email")
//!     .not_empty()
//!     .is_email()
//!     .max_length(255);
//!
//! let result = validator.validate("test@example.com");
//! assert!(result.is_valid());
//! ```


use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use regex::Regex;
use url::Url;
use lazy_static::lazy_static;

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RiValidationSeverity {
    Error,
    Warning,
    Info,
    Critical,
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiValidationError {
    pub field: String,
    pub message: String,
    pub code: String,
    pub severity: RiValidationSeverity,
    pub value: Option<serde_json::Value>,
}

impl RiValidationError {
    pub fn new(field: &str, message: &str, code: &str) -> Self {
        Self {
            field: field.to_string(),
            message: message.to_string(),
            code: code.to_string(),
            severity: RiValidationSeverity::Error,
            value: None,
        }
    }

    pub fn with_value(field: &str, message: &str, code: &str, value: serde_json::Value) -> Self {
        Self {
            field: field.to_string(),
            message: message.to_string(),
            code: code.to_string(),
            severity: RiValidationSeverity::Error,
            value: Some(value),
        }
    }

    pub fn warning(field: &str, message: &str, code: &str) -> Self {
        Self {
            field: field.to_string(),
            message: message.to_string(),
            code: code.to_string(),
            severity: RiValidationSeverity::Warning,
            value: None,
        }
    }
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiValidationResult {
    pub is_valid: bool,
    pub errors: Vec<RiValidationError>,
    pub warnings: Vec<RiValidationError>,
}

impl RiValidationResult {
    pub fn valid() -> Self {
        Self {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
        }
    }

    pub fn invalid(errors: Vec<RiValidationError>) -> Self {
        let warnings: Vec<RiValidationError> = errors
            .iter()
            .filter(|e| e.severity == RiValidationSeverity::Warning)
            .cloned()
            .collect();

        let errors: Vec<RiValidationError> = errors
            .into_iter()
            .filter(|e| e.severity == RiValidationSeverity::Error)
            .collect();

        Self {
            is_valid: errors.is_empty(),
            errors,
            warnings,
        }
    }

    pub fn add_error(&mut self, error: RiValidationError) {
        if error.severity == RiValidationSeverity::Error {
            self.is_valid = false;
            self.errors.push(error);
        } else {
            self.warnings.push(error);
        }
    }

    pub fn merge(&mut self, other: RiValidationResult) {
        self.errors.extend(other.errors);
        self.warnings.extend(other.warnings);
        self.is_valid = self.errors.is_empty();
    }

    pub fn error_count(&self) -> usize {
        self.errors.len()
    }

    pub fn warning_count(&self) -> usize {
        self.warnings.len()
    }

    pub fn to_string(&self) -> String {
        if self.is_valid {
            "Validation passed".to_string()
        } else {
            format!(
                "Validation failed with {} error(s): {}",
                self.error_count(),
                self.errors
                    .iter()
                    .map(|e| format!("{}: {}", e.field, e.message))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl RiValidationResult {
    #[new]
    fn py_new(is_valid: bool) -> Self {
        if is_valid {
            Self::valid()
        } else {
            Self::invalid(vec![])
        }
    }

    #[staticmethod]
    fn success() -> Self {
        Self::valid()
    }

    #[staticmethod]
    fn failure(errors: Vec<RiValidationError>) -> Self {
        Self::invalid(errors)
    }

    fn __str__(&self) -> String {
        self.to_string()
    }
}

#[async_trait]
pub trait RiValidator: Send + Sync {
    async fn validate(&self, value: &str) -> RiValidationResult;
    fn name(&self) -> &'static str;
}

#[async_trait]
impl RiValidator for Box<dyn RiValidator> {
    async fn validate(&self, value: &str) -> RiValidationResult {
        self.as_ref().validate(value).await
    }

    fn name(&self) -> &'static str {
        self.as_ref().name()
    }
}

pub trait RiValidationRule: Send + Sync {
    fn validate(&self, value: &str) -> Option<RiValidationError>;
    fn name(&self) -> &'static str;
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiValidatorBuilder {
    field_name: String,
    rules: Vec<Arc<dyn RiValidationRule>>,
    nullable: bool,
    optional: bool,
}

impl RiValidatorBuilder {
    pub fn new(field_name: &str) -> Self {
        Self {
            field_name: field_name.to_string(),
            rules: Vec::new(),
            nullable: false,
            optional: false,
        }
    }

    pub fn with_nullable(mut self, nullable: bool) -> Self {
        self.nullable = nullable;
        self
    }

    pub fn with_optional(mut self, optional: bool) -> Self {
        self.optional = optional;
        self
    }

    pub fn not_empty(self) -> Self {
        self.add_rule(NotEmptyRule)
    }

    pub fn is_email(self) -> Self {
        self.add_rule(EmailRule)
    }

    pub fn is_url(self) -> Self {
        self.add_rule(UrlRule)
    }

    pub fn is_ip(self) -> Self {
        self.add_rule(IpAddressRule)
    }

    pub fn is_uuid(self) -> Self {
        self.add_rule(UuidRule)
    }

    pub fn is_base64(self) -> Self {
        self.add_rule(Base64Rule)
    }

    pub fn min_length(self, min: usize) -> Self {
        self.add_rule(MinLengthRule(min))
    }

    pub fn max_length(self, max: usize) -> Self {
        self.add_rule(MaxLengthRule(max))
    }

    pub fn exact_length(self, length: usize) -> Self {
        self.add_rule(ExactLengthRule(length))
    }

    pub fn min_value(self, min: i64) -> Self {
        self.add_rule(MinValueRule(min))
    }

    pub fn max_value(self, max: i64) -> Self {
        self.add_rule(MaxValueRule(max))
    }

    pub fn range(self, min: i64, max: i64) -> Self {
        self.add_rule(RangeRule(min, max))
    }

    pub fn matches_regex(self, pattern: &str) -> Self {
        self.add_rule(RegexRule(pattern.to_string()))
    }

    pub fn alphanumeric(self) -> Self {
        self.add_rule(AlphanumericRule)
    }

    pub fn alphabetic(self) -> Self {
        self.add_rule(AlphabeticRule)
    }

    pub fn numeric(self) -> Self {
        self.add_rule(NumericRule)
    }

    pub fn lowercase(self) -> Self {
        self.add_rule(LowercaseRule)
    }

    pub fn uppercase(self) -> Self {
        self.add_rule(UppercaseRule)
    }

    pub fn contains(self, substring: &str) -> Self {
        self.add_rule(ContainsRule(substring.to_string()))
    }

    pub fn starts_with(self, prefix: &str) -> Self {
        self.add_rule(StartsWithRule(prefix.to_string()))
    }

    pub fn ends_with(self, suffix: &str) -> Self {
        self.add_rule(EndsWithRule(suffix.to_string()))
    }

    pub fn is_in(self, values: Vec<String>) -> Self {
        self.add_rule(InRule(values))
    }

    pub fn not_in(self, values: Vec<String>) -> Self {
        self.add_rule(NotInRule(values))
    }

    fn add_rule(mut self, rule: impl RiValidationRule + Send + Sync + 'static) -> Self {
        self.rules.push(Arc::new(rule));
        self
    }

    pub fn build(self) -> RiValidationRunner {
        RiValidationRunner {
            field_name: self.field_name,
            rules: self.rules,
            nullable: self.nullable,
            optional: self.optional,
        }
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl RiValidatorBuilder {
    #[new]
    fn py_new(field_name: String) -> Self {
        Self {
            field_name,
            rules: Vec::new(),
            nullable: false,
            optional: false,
        }
    }

    fn set_nullable(&mut self, nullable: bool) {
        self.nullable = nullable;
    }

    fn set_optional(&mut self, optional: bool) {
        self.optional = optional;
    }

    fn add_not_empty(&mut self) {
        self.rules.push(Arc::new(NotEmptyRule));
    }

    fn add_email(&mut self) {
        self.rules.push(Arc::new(EmailRule));
    }

    fn add_url(&mut self) {
        self.rules.push(Arc::new(UrlRule));
    }

    fn add_ip(&mut self) {
        self.rules.push(Arc::new(IpAddressRule));
    }

    fn add_uuid(&mut self) {
        self.rules.push(Arc::new(UuidRule));
    }

    fn add_base64(&mut self) {
        self.rules.push(Arc::new(Base64Rule));
    }

    fn add_min_length(&mut self, min: usize) {
        self.rules.push(Arc::new(MinLengthRule(min)));
    }

    fn add_max_length(&mut self, max: usize) {
        self.rules.push(Arc::new(MaxLengthRule(max)));
    }

    fn add_min_value(&mut self, min: i64) {
        self.rules.push(Arc::new(MinValueRule(min)));
    }

    fn add_max_value(&mut self, max: i64) {
        self.rules.push(Arc::new(MaxValueRule(max)));
    }

    fn add_range(&mut self, min: i64, max: i64) {
        self.rules.push(Arc::new(RangeRule(min, max)));
    }

    fn add_alphanumeric(&mut self) {
        self.rules.push(Arc::new(AlphanumericRule));
    }

    fn add_numeric(&mut self) {
        self.rules.push(Arc::new(NumericRule));
    }

    fn add_contains(&mut self, substring: String) {
        self.rules.push(Arc::new(ContainsRule(substring)));
    }
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiValidationRunner {
    field_name: String,
    rules: Vec<Arc<dyn RiValidationRule>>,
    nullable: bool,
    optional: bool,
}

impl RiValidationRunner {
    pub fn new(field_name: &str) -> RiValidatorBuilder {
        RiValidatorBuilder::new(field_name)
    }

    pub fn validate_value(&self, value: Option<&str>) -> RiValidationResult {
        let value = match value {
            Some(v) => v,
            None if self.optional => return RiValidationResult::valid(),
            None if self.nullable => return RiValidationResult::valid(),
            None => {
                return RiValidationResult::invalid(vec![RiValidationError::new(
                    &self.field_name,
                    "Value is required",
                    "REQUIRED",
                )]);
            }
        };

        let mut errors = Vec::with_capacity(4);

        for rule in &self.rules {
            if let Some(error) = rule.validate(value) {
                errors.push(RiValidationError {
                    field: self.field_name.clone(),
                    ..error
                });
            }
        }

        if errors.is_empty() {
            RiValidationResult::valid()
        } else {
            RiValidationResult::invalid(errors)
        }
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl RiValidationRunner {
    #[new]
    fn py_new(field_name: String) -> Self {
        RiValidatorBuilder::new(&field_name).build()
    }

    fn validate(&self, value: String) -> RiValidationResult {
        self.validate_value(Some(&value))
    }

    fn validate_optional(&self, value: Option<String>) -> RiValidationResult {
        self.validate_value(value.as_deref())
    }
}

struct NotEmptyRule;

impl RiValidationRule for NotEmptyRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if value.trim().is_empty() {
            Some(RiValidationError::new(
                "value",
                "Value cannot be empty",
                "NOT_EMPTY",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "NotEmpty"
    }
}

lazy_static! {
    static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap();
    static ref URL_REGEX: Regex = Regex::new(r"^https?://[^\s]+$").unwrap();
    static ref IP_REGEX: Regex = Regex::new(r"^(\d{1,3}\.){3}\d{1,3}$").unwrap();
    static ref UUID_REGEX: Regex = Regex::new(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$").unwrap();
    static ref BASE64_REGEX: Regex = Regex::new(r"^[A-Za-z0-9+/]*={0,2}$").unwrap();
    static ref ALPHANUMERIC_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9]+$").unwrap();
    static ref ALPHABETIC_REGEX: Regex = Regex::new(r"^[a-zA-Z]+$").unwrap();
    static ref NUMERIC_REGEX: Regex = Regex::new(r"^-?\d+(\.\d+)?$").unwrap();
}

struct EmailRule;

impl RiValidationRule for EmailRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !EMAIL_REGEX.is_match(value) {
            Some(RiValidationError::new(
                "value",
                "Invalid email format",
                "EMAIL",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Email"
    }
}

struct UrlRule;

impl RiValidationRule for UrlRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if Url::parse(value).is_err() {
            Some(RiValidationError::new(
                "value",
                "Invalid URL format",
                "URL",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Url"
    }
}

struct IpAddressRule;

impl RiValidationRule for IpAddressRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !IP_REGEX.is_match(value) {
            return Some(RiValidationError::new(
                "value",
                "Invalid IP address format",
                "IP_ADDRESS",
            ));
        }

        let parts: Vec<&str> = value.split('.').collect();
        for part in parts {
            if let Ok(num) = part.parse::<u32>() {
                if num > 255 {
                    return Some(RiValidationError::new(
                        "value",
                        "IP address octet out of range",
                        "IP_ADDRESS_RANGE",
                    ));
                }
            }
        }

        None
    }

    fn name(&self) -> &'static str {
        "IpAddress"
    }
}

struct UuidRule;

impl RiValidationRule for UuidRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !UUID_REGEX.is_match(value) {
            Some(RiValidationError::new(
                "value",
                "Invalid UUID format",
                "UUID",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Uuid"
    }
}

struct Base64Rule;

impl RiValidationRule for Base64Rule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !BASE64_REGEX.is_match(value) {
            Some(RiValidationError::new(
                "value",
                "Invalid Base64 format",
                "BASE64",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Base64"
    }
}

struct MinLengthRule(usize);

impl RiValidationRule for MinLengthRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if value.len() < self.0 {
            Some(RiValidationError::new(
                "value",
                &format!("Value must be at least {} characters", self.0),
                "MIN_LENGTH",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "MinLength"
    }
}

struct MaxLengthRule(usize);

impl RiValidationRule for MaxLengthRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if value.len() > self.0 {
            Some(RiValidationError::new(
                "value",
                &format!("Value must be at most {} characters", self.0),
                "MAX_LENGTH",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "MaxLength"
    }
}

struct ExactLengthRule(usize);

impl RiValidationRule for ExactLengthRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if value.len() != self.0 {
            Some(RiValidationError::new(
                "value",
                &format!("Value must be exactly {} characters", self.0),
                "EXACT_LENGTH",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "ExactLength"
    }
}

struct MinValueRule(i64);

impl RiValidationRule for MinValueRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if let Ok(num) = value.parse::<i64>() {
            if num < self.0 {
                return Some(RiValidationError::new(
                    "value",
                    &format!("Value must be at least {}", self.0),
                    "MIN_VALUE",
                ));
            }
        }
        None
    }

    fn name(&self) -> &'static str {
        "MinValue"
    }
}

struct MaxValueRule(i64);

impl RiValidationRule for MaxValueRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if let Ok(num) = value.parse::<i64>() {
            if num > self.0 {
                return Some(RiValidationError::new(
                    "value",
                    &format!("Value must be at most {}", self.0),
                    "MAX_VALUE",
                ));
            }
        }
        None
    }

    fn name(&self) -> &'static str {
        "MaxValue"
    }
}

struct RangeRule(i64, i64);

impl RiValidationRule for RangeRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if let Ok(num) = value.parse::<i64>() {
            if num < self.0 || num > self.1 {
                return Some(RiValidationError::new(
                    "value",
                    &format!("Value must be between {} and {}", self.0, self.1),
                    "RANGE",
                ));
            }
        }
        None
    }

    fn name(&self) -> &'static str {
        "Range"
    }
}

struct RegexRule(String);

impl RiValidationRule for RegexRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        // Security: Limit input length to prevent ReDoS attacks
        // Most legitimate values should be under 10KB
        const MAX_REGEX_INPUT_LENGTH: usize = 10 * 1024;
        
        if value.len() > MAX_REGEX_INPUT_LENGTH {
            return Some(RiValidationError::new(
                "value",
                &format!("Value too long for regex validation (max {} bytes)", MAX_REGEX_INPUT_LENGTH),
                "REGEX_INPUT_TOO_LONG",
            ));
        }
        
        // Security: Use regex with timeout to prevent catastrophic backtracking
        // For simplicity, we just compile and match with a length-limited input
        // In production, consider using regex with explicit timeout
        match Regex::new(&self.0) {
            Ok(regex) => {
                if !regex.is_match(value) {
                    return Some(RiValidationError::new(
                        "value",
                        "Value does not match required pattern",
                        "REGEX",
                    ));
                }
            }
            Err(e) => {
                // Log invalid regex pattern but don't reveal details to user
                log::warn!("[Ri.Validation] Invalid regex pattern: {}", e);
                return Some(RiValidationError::new(
                    "value",
                    "Invalid validation pattern",
                    "REGEX_INVALID",
                ));
            }
        }
        None
    }

    fn name(&self) -> &'static str {
        "Regex"
    }
}

struct AlphanumericRule;

impl RiValidationRule for AlphanumericRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !ALPHANUMERIC_REGEX.is_match(value) {
            Some(RiValidationError::new(
                "value",
                "Value must contain only alphanumeric characters",
                "ALPHANUMERIC",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Alphanumeric"
    }
}

struct AlphabeticRule;

impl RiValidationRule for AlphabeticRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !ALPHABETIC_REGEX.is_match(value) {
            Some(RiValidationError::new(
                "value",
                "Value must contain only alphabetic characters",
                "ALPHABETIC",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Alphabetic"
    }
}

struct NumericRule;

impl RiValidationRule for NumericRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !NUMERIC_REGEX.is_match(value) {
            Some(RiValidationError::new(
                "value",
                "Value must be a valid number",
                "NUMERIC",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Numeric"
    }
}

struct LowercaseRule;

impl RiValidationRule for LowercaseRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !value.chars().all(|c| !c.is_uppercase()) {
            Some(RiValidationError::new(
                "value",
                "Value must be lowercase",
                "LOWERCASE",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Lowercase"
    }
}

struct UppercaseRule;

impl RiValidationRule for UppercaseRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !value.chars().all(|c| !c.is_lowercase()) {
            Some(RiValidationError::new(
                "value",
                "Value must be uppercase",
                "UPPERCASE",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Uppercase"
    }
}

struct ContainsRule(String);

impl RiValidationRule for ContainsRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !value.contains(&self.0) {
            Some(RiValidationError::new(
                "value",
                &format!("Value must contain '{}'", self.0),
                "CONTAINS",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "Contains"
    }
}

struct StartsWithRule(String);

impl RiValidationRule for StartsWithRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !value.starts_with(&self.0) {
            Some(RiValidationError::new(
                "value",
                &format!("Value must start with '{}'", self.0),
                "STARTS_WITH",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "StartsWith"
    }
}

struct EndsWithRule(String);

impl RiValidationRule for EndsWithRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !value.ends_with(&self.0) {
            Some(RiValidationError::new(
                "value",
                &format!("Value must end with '{}'", self.0),
                "ENDS_WITH",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "EndsWith"
    }
}

struct InRule(Vec<String>);

impl RiValidationRule for InRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if !self.0.contains(&value.to_string()) {
            Some(RiValidationError::new(
                "value",
                &format!("Value must be one of: {}", self.0.join(", ")),
                "IN",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "In"
    }
}

struct NotInRule(Vec<String>);

impl RiValidationRule for NotInRule {
    fn validate(&self, value: &str) -> Option<RiValidationError> {
        if self.0.contains(&value.to_string()) {
            Some(RiValidationError::new(
                "value",
                "Value is not allowed",
                "NOT_IN",
            ))
        } else {
            None
        }
    }

    fn name(&self) -> &'static str {
        "NotIn"
    }
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiSanitizationConfig {
    pub trim_whitespace: bool,
    pub lowercase: bool,
    pub uppercase: bool,
    pub remove_extra_spaces: bool,
    pub remove_html_tags: bool,
    pub escape_special_chars: bool,
}

impl Default for RiSanitizationConfig {
    fn default() -> Self {
        Self {
            trim_whitespace: true,
            lowercase: false,
            uppercase: false,
            remove_extra_spaces: false,
            remove_html_tags: false,
            escape_special_chars: false,
        }
    }
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Clone)]
pub struct RiSanitizer {
    config: RiSanitizationConfig,
}

impl RiSanitizer {
    pub fn new() -> Self {
        Self {
            config: RiSanitizationConfig::default(),
        }
    }

    pub fn with_config(config: RiSanitizationConfig) -> Self {
        Self { config }
    }

    pub fn sanitize(&self, input: &str) -> String {
        let mut result = input.to_string();

        if self.config.trim_whitespace {
            result = result.trim().to_string();
        }

        if self.config.lowercase {
            result = result.to_lowercase();
        }

        if self.config.uppercase {
            result = result.to_uppercase();
        }

        if self.config.remove_extra_spaces {
            let re = regex::Regex::new(r"\s+").unwrap();
            result = re.replace_all(&result, " ").to_string();
        }

        if self.config.remove_html_tags {
            let re = regex::Regex::new(r"<[^>]*>").unwrap();
            result = re.replace_all(&result, "").to_string();
        }

        if self.config.escape_special_chars {
            result = html_escape::encode_safe(&result).to_string();
        }

        result
    }

    pub fn sanitize_email(&self, input: &str) -> String {
        let re = regex::Regex::new(r"[^\w.%+-]").unwrap();
        re.replace_all(&self.sanitize(input), "").to_string()
    }

    pub fn sanitize_filename(&self, input: &str) -> String {
        let re = regex::Regex::new(r"[^\w.-]").unwrap();
        re.replace_all(&input, "_").to_string()
    }
}

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

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiSchemaValidator {
    schema: serde_json::Value,
}

impl RiSchemaValidator {
    pub fn new(schema: serde_json::Value) -> Self {
        Self { schema }
    }

    pub fn validate(&self, data: &serde_json::Value) -> RiValidationResult {
        let mut result = RiValidationResult::valid();

        if let Some(schema_type) = self.schema.get("type") {
            match schema_type {
                serde_json::Value::String(type_str) => {
                    match type_str.as_str() {
                        "string" => {
                            if !data.is_string() {
                                result.add_error(RiValidationError::new(
                                    "root",
                                    &format!("Expected string, got {}", data),
                                    "TYPE_MISMATCH",
                                ));
                            }
                        }
                        "number" => {
                            if !data.is_number() {
                                result.add_error(RiValidationError::new(
                                    "root",
                                    &format!("Expected number, got {}", data),
                                    "TYPE_MISMATCH",
                                ));
                            }
                        }
                        "integer" => {
                            if !data.is_i64() {
                                result.add_error(RiValidationError::new(
                                    "root",
                                    &format!("Expected integer, got {}", data),
                                    "TYPE_MISMATCH",
                                ));
                            }
                        }
                        "boolean" => {
                            if !data.is_boolean() {
                                result.add_error(RiValidationError::new(
                                    "root",
                                    &format!("Expected boolean, got {}", data),
                                    "TYPE_MISMATCH",
                                ));
                            }
                        }
                        "array" => {
                            if !data.is_array() {
                                result.add_error(RiValidationError::new(
                                    "root",
                                    &format!("Expected array, got {}", data),
                                    "TYPE_MISMATCH",
                                ));
                            }
                        }
                        "object" => {
                            if !data.is_object() {
                                result.add_error(RiValidationError::new(
                                    "root",
                                    &format!("Expected object, got {}", data),
                                    "TYPE_MISMATCH",
                                ));
                            }
                        }
                        _ => {}
                    }
                }
                _ => {}
            }
        }

        if let Some(required) = self.schema.get("required") {
            if let serde_json::Value::Array(req_fields) = required {
                if let serde_json::Value::Object(obj) = data {
                    for field in req_fields {
                        if let serde_json::Value::String(field_name) = field {
                            if !obj.contains_key(field_name) {
                                result.add_error(RiValidationError::new(
                                    field_name,
                                    "Field is required",
                                    "REQUIRED",
                                ));
                            }
                        }
                    }
                }
            }
        }

        if let Some(min_length) = self.schema.get("minLength") {
            if let serde_json::Value::Number(min) = min_length {
                if let Some(str_val) = data.as_str() {
                    if (str_val.len() as u64) < min.as_u64().unwrap_or(0) {
                        result.add_error(RiValidationError::new(
                            "root",
                            &format!("String must be at least {} characters", min),
                            "MIN_LENGTH",
                        ));
                    }
                }
            }
        }

        if let Some(max_length) = self.schema.get("maxLength") {
            if let serde_json::Value::Number(max) = max_length {
                if let Some(str_val) = data.as_str() {
                    if (str_val.len() as u64) > max.as_u64().unwrap_or(u64::MAX) {
                        result.add_error(RiValidationError::new(
                            "root",
                            &format!("String must be at most {} characters", max),
                            "MAX_LENGTH",
                        ));
                    }
                }
            }
        }

        if let Some(pattern) = self.schema.get("pattern") {
            if let serde_json::Value::String(pattern_str) = pattern {
                if let Ok(regex) = Regex::new(pattern_str) {
                    if let Some(str_val) = data.as_str() {
                        if !regex.is_match(str_val) {
                            result.add_error(RiValidationError::new(
                                "root",
                                "String does not match required pattern",
                                "PATTERN",
                            ));
                        }
                    }
                }
            }
        }

        if let Some(enum_values) = self.schema.get("enum") {
            if let serde_json::Value::Array(enum_array) = enum_values {
                let mut found = false;
                for enum_val in enum_array {
                    if enum_val == data {
                        found = true;
                        break;
                    }
                }
                if !found {
                    result.add_error(RiValidationError::new(
                        "root",
                        "Value must be one of the allowed values",
                        "ENUM",
                    ));
                }
            }
        }

        result
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl RiSchemaValidator {
    #[new]
    fn py_new(schema: String) -> Self {
        let json_value: serde_json::Value = serde_json::from_str(&schema).unwrap_or_default();
        Self::new(json_value)
    }

    fn validate_json(&self, data: String) -> RiValidationResult {
        let json_value: serde_json::Value = serde_json::from_str(&data).unwrap_or(serde_json::Value::Null);
        self.validate(&json_value)
    }
}

#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone)]
pub struct RiValidationModule;

#[cfg(feature = "pyo3")]
#[pymethods]
impl RiValidationModule {
    #[staticmethod]
    fn validate_email(value: String) -> RiValidationResult {
        RiValidatorBuilder::new("email").is_email().max_length(255).build().validate_value(Some(&value))
    }

    #[staticmethod]
    fn validate_username(value: String) -> RiValidationResult {
        RiValidatorBuilder::new("username")
            .not_empty()
            .min_length(3)
            .max_length(32)
            .alphanumeric()
            .build()
            .validate_value(Some(&value))
    }

    #[staticmethod]
    fn validate_password(value: String) -> RiValidationResult {
        RiValidatorBuilder::new("password")
            .not_empty()
            .min_length(8)
            .build()
            .validate_value(Some(&value))
    }

    #[staticmethod]
    fn validate_url(value: String) -> RiValidationResult {
        RiValidatorBuilder::new("url").is_url().build().validate_value(Some(&value))
    }

    #[staticmethod]
    fn validate_ip(value: String) -> RiValidationResult {
        RiValidatorBuilder::new("ip").is_ip().build().validate_value(Some(&value))
    }

    #[staticmethod]
    fn validate_not_empty(field_name: String, value: String) -> RiValidationResult {
        RiValidatorBuilder::new(&field_name).not_empty().build().validate_value(Some(&value))
    }

    #[staticmethod]
    fn validate_length(field_name: String, value: String, min: usize, max: usize) -> RiValidationResult {
        RiValidatorBuilder::new(&field_name)
            .min_length(min)
            .max_length(max)
            .build()
            .validate_value(Some(&value))
    }
}