diff options
| author | Vincent Prouillet | 2017-01-19 13:35:05 +0900 |
|---|---|---|
| committer | Vincent Prouillet | 2017-01-19 13:38:47 +0900 |
| commit | c8fb03bdd7391fc7b9fdc56efcc55a5eec08e19c (patch) | |
| tree | 9e11068da6092c5f072ea95a6265e5391adeca75 /validator_derive/tests/test_derive.rs | |
| parent | 696e4273173033a6b1d488416c5433d9c49eb8e9 (diff) | |
| download | validator-c8fb03bdd7391fc7b9fdc56efcc55a5eec08e19c.tar.bz2 | |
Validators work for Option<TYPE>
Diffstat (limited to 'validator_derive/tests/test_derive.rs')
| -rw-r--r-- | validator_derive/tests/test_derive.rs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/validator_derive/tests/test_derive.rs b/validator_derive/tests/test_derive.rs index 5d61755..d456be6 100644 --- a/validator_derive/tests/test_derive.rs +++ b/validator_derive/tests/test_derive.rs @@ -274,7 +274,7 @@ fn test_can_check_regex_validator() { #[test] -fn test_can_validate_option_fields() { +fn test_can_validate_option_fields_with_lifetime() { lazy_static! { static ref RE2: Regex = Regex::new(r"[a-z]{2}").unwrap(); } @@ -312,3 +312,46 @@ fn test_can_validate_option_fields() { }; assert!(s.validate().is_ok()); } + +#[test] +fn test_can_validate_option_fields_without_lifetime() { + lazy_static! { + static ref RE2: Regex = Regex::new(r"[a-z]{2}").unwrap(); + } + + #[derive(Debug, Validate)] + struct PutStruct { + #[validate(length(min = "1", max = "10"))] + name: Option<String>, + #[validate(length(min = "1", max = "10"))] + ids: Option<Vec<usize>>, + #[validate(range(min = "1", max = "10"))] + range: Option<usize>, + #[validate(email)] + email: Option<String>, + #[validate(url)] + url: Option<String>, + #[validate(contains = "@")] + text: Option<String>, + #[validate(regex = "RE2")] + re: Option<String>, + #[validate(custom = "check_str")] + custom: Option<String>, + } + + fn check_str(_: &str) -> Option<String> { + None + } + + let s = PutStruct { + name: Some("al".to_string()), + ids: Some(vec![1, 2, 3]), + range: Some(2), + email: Some("hi@gmail.com".to_string()), + url: Some("http://google.com".to_string()), + text: Some("@someone".to_string()), + re: Some("hi".to_string()), + custom: Some("hey".to_string()), + }; + assert!(s.validate().is_ok()); +} |
