|
| 1 | +#[allow(deprecated)] |
| 2 | +mod test { |
| 3 | + |
| 4 | + use serde_json::json; |
| 5 | + use serde_valid::Validate; |
| 6 | + |
| 7 | + use serde::Deserialize; |
| 8 | + |
| 9 | + #[test] |
| 10 | + fn deprecated_enumerate_custom_err_message_fn() { |
| 11 | + fn error_message(_params: &serde_valid::error::EnumerateError) -> String { |
| 12 | + "this is custom message.".to_string() |
| 13 | + } |
| 14 | + |
| 15 | + #[derive(Validate)] |
| 16 | + struct TestStruct { |
| 17 | + #[validate(enumerate(1, 2, 3), message_fn(error_message))] |
| 18 | + val: i32, |
| 19 | + } |
| 20 | + |
| 21 | + let s = TestStruct { val: 4 }; |
| 22 | + |
| 23 | + assert_eq!( |
| 24 | + s.validate().unwrap_err().to_string(), |
| 25 | + json!({ |
| 26 | + "errors": [], |
| 27 | + "properties": { |
| 28 | + "val": { |
| 29 | + "errors": [ |
| 30 | + "this is custom message." |
| 31 | + ] |
| 32 | + } |
| 33 | + } |
| 34 | + }) |
| 35 | + .to_string() |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn enumerate_custom_err_message_fn() { |
| 41 | + fn error_message(_params: &serde_valid::error::EnumerateError) -> String { |
| 42 | + "this is custom message.".to_string() |
| 43 | + } |
| 44 | + |
| 45 | + #[derive(Validate)] |
| 46 | + struct TestStruct { |
| 47 | + #[validate(enumerate = [1, 2, 3], message_fn(error_message))] |
| 48 | + val: i32, |
| 49 | + } |
| 50 | + |
| 51 | + let s = TestStruct { val: 4 }; |
| 52 | + |
| 53 | + assert_eq!( |
| 54 | + s.validate().unwrap_err().to_string(), |
| 55 | + json!({ |
| 56 | + "errors": [], |
| 57 | + "properties": { |
| 58 | + "val": { |
| 59 | + "errors": [ |
| 60 | + "this is custom message." |
| 61 | + ] |
| 62 | + } |
| 63 | + } |
| 64 | + }) |
| 65 | + .to_string() |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn items_custom_err_message_fn() { |
| 71 | + #[inline] |
| 72 | + fn min_error_message(_params: &serde_valid::MinItemsError) -> String { |
| 73 | + "this is min custom message.".to_string() |
| 74 | + } |
| 75 | + #[inline] |
| 76 | + fn max_error_message(_params: &serde_valid::MaxItemsError) -> String { |
| 77 | + "this is max custom message.".to_string() |
| 78 | + } |
| 79 | + |
| 80 | + #[derive(Validate)] |
| 81 | + struct TestStruct { |
| 82 | + #[validate(min_items = 4, message_fn(min_error_message))] |
| 83 | + #[validate(max_items = 2, message_fn(max_error_message))] |
| 84 | + val: Vec<i32>, |
| 85 | + } |
| 86 | + |
| 87 | + let s = TestStruct { val: vec![1, 2, 3] }; |
| 88 | + |
| 89 | + assert_eq!( |
| 90 | + s.validate().unwrap_err().to_string(), |
| 91 | + json!({ |
| 92 | + "errors": [], |
| 93 | + "properties": { |
| 94 | + "val": { |
| 95 | + "errors": [ |
| 96 | + "this is min custom message.", |
| 97 | + "this is max custom message." |
| 98 | + ] |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + .to_string() |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn length_custom_err_message_fn() { |
| 108 | + fn custom_min_error_message(_params: &serde_valid::MinLengthError) -> String { |
| 109 | + "this is min custom message.".to_string() |
| 110 | + } |
| 111 | + |
| 112 | + fn custom_max_error_message(_params: &serde_valid::MaxLengthError) -> String { |
| 113 | + "this is max custom message.".to_string() |
| 114 | + } |
| 115 | + |
| 116 | + #[derive(Validate)] |
| 117 | + struct TestStruct { |
| 118 | + #[validate(min_length = 5, message_fn(custom_min_error_message))] |
| 119 | + #[validate(max_length = 3, message_fn(custom_max_error_message))] |
| 120 | + val: String, |
| 121 | + } |
| 122 | + |
| 123 | + let s = TestStruct { |
| 124 | + val: String::from("test"), |
| 125 | + }; |
| 126 | + |
| 127 | + assert_eq!( |
| 128 | + s.validate().unwrap_err().to_string(), |
| 129 | + json!({ |
| 130 | + "errors": [], |
| 131 | + "properties": { |
| 132 | + "val": { |
| 133 | + "errors": [ |
| 134 | + "this is min custom message.", |
| 135 | + "this is max custom message." |
| 136 | + ] |
| 137 | + } |
| 138 | + } |
| 139 | + }) |
| 140 | + .to_string() |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn multiple_of_custom_err_message_fn() { |
| 146 | + fn error_message(_params: &serde_valid::MultipleOfError) -> String { |
| 147 | + "this is custom message.".to_string() |
| 148 | + } |
| 149 | + |
| 150 | + #[derive(Validate)] |
| 151 | + struct TestStruct { |
| 152 | + #[validate(multiple_of = 5, message_fn(error_message))] |
| 153 | + val: i32, |
| 154 | + } |
| 155 | + |
| 156 | + let s = TestStruct { val: 14 }; |
| 157 | + |
| 158 | + assert_eq!( |
| 159 | + s.validate().unwrap_err().to_string(), |
| 160 | + json!({ |
| 161 | + "errors": [], |
| 162 | + "properties": { |
| 163 | + "val": { |
| 164 | + "errors": [ |
| 165 | + "this is custom message." |
| 166 | + ] |
| 167 | + } |
| 168 | + } |
| 169 | + }) |
| 170 | + .to_string() |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn pattern_custom_err_message_fn() { |
| 176 | + fn error_message(_params: &serde_valid::PatternError) -> String { |
| 177 | + "this is custom message.".to_string() |
| 178 | + } |
| 179 | + |
| 180 | + #[derive(Validate)] |
| 181 | + struct TestStruct { |
| 182 | + #[validate(pattern = r"^\d{4}-\d{2}-\d{2}$", message_fn = error_message)] |
| 183 | + val: String, |
| 184 | + } |
| 185 | + |
| 186 | + let s = TestStruct { |
| 187 | + val: String::from("2020/09/10"), |
| 188 | + }; |
| 189 | + |
| 190 | + assert_eq!( |
| 191 | + s.validate().unwrap_err().to_string(), |
| 192 | + json!({ |
| 193 | + "errors": [], |
| 194 | + "properties": { |
| 195 | + "val": { |
| 196 | + "errors": [ |
| 197 | + "this is custom message." |
| 198 | + ] |
| 199 | + } |
| 200 | + } |
| 201 | + }) |
| 202 | + .to_string() |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn properties_custom_err_message_fn() { |
| 208 | + fn min_custom_error_message(_params: &serde_valid::MinPropertiesError) -> String { |
| 209 | + "this is min custom message.".to_string() |
| 210 | + } |
| 211 | + |
| 212 | + fn max_custom_error_message(_params: &serde_valid::MaxPropertiesError) -> String { |
| 213 | + "this is max custom message.".to_string() |
| 214 | + } |
| 215 | + |
| 216 | + #[derive(Deserialize, Validate)] |
| 217 | + struct TestStruct { |
| 218 | + #[validate(min_properties = 3, message_fn(min_custom_error_message))] |
| 219 | + #[validate(max_properties = 1, message_fn(max_custom_error_message))] |
| 220 | + val: serde_json::Map<String, serde_json::Value>, |
| 221 | + } |
| 222 | + |
| 223 | + let s: TestStruct = serde_json::from_value(json!({ |
| 224 | + "val": { |
| 225 | + "key1": "value1", |
| 226 | + "key2": "value2", |
| 227 | + } |
| 228 | + })) |
| 229 | + .unwrap(); |
| 230 | + |
| 231 | + assert_eq!( |
| 232 | + s.validate().unwrap_err().to_string(), |
| 233 | + json!({ |
| 234 | + "errors": [], |
| 235 | + "properties": { |
| 236 | + "val": { |
| 237 | + "errors": [ |
| 238 | + "this is min custom message.", |
| 239 | + "this is max custom message." |
| 240 | + ] |
| 241 | + } |
| 242 | + } |
| 243 | + }) |
| 244 | + .to_string() |
| 245 | + ); |
| 246 | + } |
| 247 | + |
| 248 | + #[test] |
| 249 | + fn range_custom_err_message_fn() { |
| 250 | + fn custom_min_error_message(_params: &serde_valid::MinimumError) -> String { |
| 251 | + "this is min custom message.".to_string() |
| 252 | + } |
| 253 | + |
| 254 | + fn custom_max_error_message(_params: &serde_valid::MaximumError) -> String { |
| 255 | + "this is max custom message.".to_string() |
| 256 | + } |
| 257 | + |
| 258 | + #[derive(Validate)] |
| 259 | + struct TestStruct { |
| 260 | + #[validate(minimum = 5, message_fn(custom_min_error_message))] |
| 261 | + #[validate(maximum = 3, message_fn(custom_max_error_message))] |
| 262 | + val: i32, |
| 263 | + } |
| 264 | + |
| 265 | + let s = TestStruct { val: 4 }; |
| 266 | + |
| 267 | + assert_eq!( |
| 268 | + s.validate().unwrap_err().to_string(), |
| 269 | + json!({ |
| 270 | + "errors": [], |
| 271 | + "properties": { |
| 272 | + "val": { |
| 273 | + "errors": [ |
| 274 | + "this is min custom message.", |
| 275 | + "this is max custom message." |
| 276 | + ] |
| 277 | + } |
| 278 | + } |
| 279 | + }) |
| 280 | + .to_string() |
| 281 | + ); |
| 282 | + } |
| 283 | + |
| 284 | + #[test] |
| 285 | + fn unique_items_custom_err_message_fn() { |
| 286 | + fn error_message(_params: &serde_valid::UniqueItemsError) -> String { |
| 287 | + "this is custom message.".to_string() |
| 288 | + } |
| 289 | + |
| 290 | + #[derive(Validate)] |
| 291 | + struct TestStruct { |
| 292 | + #[validate(unique_items, message_fn(error_message))] |
| 293 | + val: Vec<i32>, |
| 294 | + } |
| 295 | + |
| 296 | + let s = TestStruct { |
| 297 | + val: vec![1, 2, 3, 2], |
| 298 | + }; |
| 299 | + |
| 300 | + assert_eq!( |
| 301 | + s.validate().unwrap_err().to_string(), |
| 302 | + json!({ |
| 303 | + "errors": [], |
| 304 | + "properties": { |
| 305 | + "val": { |
| 306 | + "errors": [ |
| 307 | + "this is custom message." |
| 308 | + ] |
| 309 | + } |
| 310 | + } |
| 311 | + }) |
| 312 | + .to_string() |
| 313 | + ); |
| 314 | + } |
| 315 | +} |
0 commit comments