-
Notifications
You must be signed in to change notification settings - Fork 153
/
custom_args.rs
198 lines (166 loc) · 5.14 KB
/
custom_args.rs
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
use std::ops::AddAssign;
use validator::{Validate, ValidateArgs, ValidationError};
#[derive(Debug, PartialEq, Clone)]
struct TestContext {
val: String,
}
fn valid_fn(_: &String, _arg: &TestContext) -> Result<(), ValidationError> {
Ok(())
}
fn valid_fn_with_ref(_: &String, _arg: &TestContext) -> Result<(), ValidationError> {
Ok(())
}
fn valid_fn_with_mut_ref(_: &String, arg: &mut TestContext) -> Result<(), ValidationError> {
arg.val = "new value".to_string();
Ok(())
}
#[test]
fn validate_simple_custom_fn() {
#[derive(Validate)]
#[validate(context = TestContext)]
struct TestStruct {
#[validate(custom(function = valid_fn, use_context))]
value: String,
}
let test_struct = TestStruct { value: "Something".to_string() };
let c = TestContext { val: "asd".to_string() };
assert!(test_struct.validate_with_args(&c).is_ok());
}
#[test]
fn validate_multiple_custom_fn() {
#[derive(Validate)]
#[validate(context = TestContext)]
struct TestStruct {
#[validate(custom(function = valid_fn, use_context))]
value: String,
#[validate(custom(function = valid_fn, use_context))]
value2: String,
#[validate(custom(function = valid_fn, use_context))]
value3: String,
}
let test_struct = TestStruct {
value: "Something".to_string(),
value2: "asd".to_string(),
value3: "fgre".to_string(),
};
let test_arg = TestContext { val: "test".to_string() };
assert!(test_struct.validate_with_args(&test_arg).is_ok());
}
#[test]
fn validate_custom_fn_with_ref() {
#[derive(Validate)]
#[validate(context = TestContext)]
struct TestStruct {
#[validate(custom(function = valid_fn_with_ref, use_context))]
value: String,
}
let val = TestContext { val: "asd".to_string() };
let test_struct = TestStruct { value: "Something".to_string() };
assert!(test_struct.validate_with_args(&val).is_ok());
// test reference
assert_eq!(val, TestContext { val: "asd".to_string() });
}
#[test]
fn validate_custom_fn_with_mut_ref() {
#[derive(Validate)]
#[validate(context = TestContext, mutable)]
struct TestStruct<'a> {
#[validate(custom(function = valid_fn_with_mut_ref, use_context))]
value: &'a mut String,
}
let mut val = TestContext { val: "old value".to_string() };
let test_struct = TestStruct { value: &mut "Something".to_string() };
assert!(test_struct.validate_with_args(&mut val).is_ok());
assert_eq!(val, TestContext { val: "new value".to_string() });
}
#[test]
fn validate_custom_fn_with_complex_args() {
#[derive(Validate)]
#[validate(context = "Arg<i32>", mutable)]
struct TestStruct {
#[validate(custom(function = add_assign, use_context))]
value: String,
}
struct Arg<T: AddAssign> {
counter: T,
}
fn add_assign(_value: &str, arg: &mut Arg<i32>) -> Result<(), ValidationError> {
arg.counter += 1;
Ok(())
}
let mut arg = Arg { counter: 0 };
let test_struct = TestStruct { value: "test".to_string() };
let res = test_struct.validate_with_args(&mut arg);
assert!(res.is_ok());
assert_eq!(arg.counter, 1);
}
#[test]
fn validate_custom_fn_with_multiple_args() {
#[derive(Debug, Validate, PartialEq)]
#[validate(context = Arg, mutable)]
struct TestStruct {
#[validate(custom(function = add_assign, use_context))]
value: String,
}
#[derive(Debug, PartialEq)]
struct Arg {
counter: i32,
counter2: u8,
}
fn add_assign(_: &String, arg: &mut Arg) -> Result<(), ValidationError> {
arg.counter += 1;
arg.counter2 += 2;
Ok(())
}
let test_struct = TestStruct { value: "something".to_string() };
let mut arg = Arg { counter: 5, counter2: 16 };
assert!(test_struct.validate_with_args(&mut arg).is_ok());
assert_eq!(arg, Arg { counter: 6, counter2: 18 });
}
// #[test]
// fn validate_nested_custom_fn() {
// #[derive(Validate)]
// #[validate(context = Arg)]
// struct TestStruct {
// #[validate(nested)]
// child: Child,
// }
//
// #[derive(Validate)]
// #[validate(context = Arg)]
// struct Child {
// #[validate(custom(function = add_assign, use_context))]
// value: String,
// }
//
// struct Arg {
// _counter: i32,
// }
//
// fn add_assign(_: &String, _arg: &Arg) -> Result<(), ValidationError> {
// Ok(())
// }
//
// let t = TestStruct { child: Child { value: "test".to_string() } };
// let arg = Arg { _counter: 123 };
//
// assert!(t.validate_with_args(&arg).is_ok());
// }
#[test]
fn validate_custom_arg_with_lifetime() {
#[derive(Validate)]
#[validate(context = "Arg<'v_a>")]
struct Test {
#[validate(custom(function = custom_with_lifetime, use_context))]
val: String,
}
struct Arg<'a> {
_arg: &'a str,
}
fn custom_with_lifetime(_: &str, _: &Arg) -> Result<(), ValidationError> {
Ok(())
}
let test = Test { val: "Test".to_string() };
let arg = Arg { _arg: "Test" };
assert!(test.validate_with_args(&arg).is_ok());
}