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
//! # Second scheme of validator
//! The focus of this scheme is on string, as most inputs is string or number, and the verification of numbers is relatively simple.
//! The advantage of this scheme over the first one is that it does not require serialization of all data.
//! and you can still use build-in rules.
//!
//! this is an example:
//! ```rust
//! # use valitron::{
//! # available::{Email, Message, Required, Trim},
//! # register::string::Validator,
//! # rule::string::{custom, StringRuleExt},
//! # };
//!
//! pub fn main() {
//! let data = Input {
//! name:" Jone ".into(),
//! email:"jone@gmail.com".into(),
//! gender:"male".into(),
//! password: "Abc123".into(),
//! age: 12,
//! weight: 101.2,
//! };
//!
//! let data = Input::new(data).unwrap();
//!
//! assert_eq!(data.name, "Jone");
//! }
//!
//! struct Input {
//! name: String,
//! email: String,
//! gender: String,
//! password: String,
//! age: u8,
//! weight: f32,
//! }
//!
//! impl Input {
//! fn new(mut input: Input) -> Result<Self, Validator<Message>> {
//! let valid = Validator::new()
//! .insert("name", &mut input.name, Trim.and(Required))
//! .insert("email", &mut input.email, Trim.and(Required).and(Email))
//! .insert("gender", &mut input.gender, custom(validate_gender))
//! .insert(
//! "password",
//! &mut input.password,
//! Trim.custom(validate_password),
//! )
//! .insert_fn("age", || {
//! if input.age < 10 {
//! input.age = 10;
//! }
//! if input.age > 18 {
//! Err(Message::fallback("age is out of range"))
//! } else {
//! Ok(())
//! }
//! });
//!
//! valid.validate(input)
//! }
//! }
//!
//! fn validate_password(pass: &mut String) -> Result<(), Message> {
//! let upper = pass.find(char::is_uppercase);
//! let lower = pass.find(char::is_lowercase);
//! let num = pass.find(char::is_numeric);
//! if upper.is_some() && lower.is_some() && num.is_some() {
//! Ok(())
//! } else {
//! Err(Message::fallback(
//! "password need to contain uppercase, lowercase and numeric",
//! ))
//! }
//! }
//!
//! fn validate_gender(gender: &mut String) -> Result<(), Message> {
//! Ok(())
//! }
//!
//! ```
//!
//! This validator is highly abstract, This means that you can use custom rules,They can also be used together with build-in rules,
//! This only need to converting the error types they return.
//!
//! > custom rule need to implement Clone.
use HashMap;
use crateIntoRuleList;
use InnerValidatorError;
// fn validate<R: IntoRuleList<String, M>, M>(value: String, rules: R) -> Vec<M> {
// let list = rules.into_list();
// let mut string = value;
// list.call(&mut string)
// }
pub type Validator<M> = ;