diff options
| author | Vincent Prouillet | 2017-01-18 22:43:34 +0900 |
|---|---|---|
| committer | Vincent Prouillet | 2017-01-19 13:38:47 +0900 |
| commit | 696e4273173033a6b1d488416c5433d9c49eb8e9 (patch) | |
| tree | 8c6e1776e627412f16d1116a732b23561c819a6f /validator_derive/tests | |
| parent | 1fd1b3d708a2737c2a678df8a37e719c29c754bc (diff) | |
| download | validator-696e4273173033a6b1d488416c5433d9c49eb8e9.tar.bz2 | |
Validator works for Option<&
Diffstat (limited to 'validator_derive/tests')
| -rw-r--r-- | validator_derive/tests/run-pass/lifetime.rs | 8 | ||||
| -rw-r--r-- | validator_derive/tests/test_derive.rs | 51 |
2 files changed, 42 insertions, 17 deletions
diff --git a/validator_derive/tests/run-pass/lifetime.rs b/validator_derive/tests/run-pass/lifetime.rs index 8547a1a..b3de940 100644 --- a/validator_derive/tests/run-pass/lifetime.rs +++ b/validator_derive/tests/run-pass/lifetime.rs @@ -8,12 +8,8 @@ use validator::Validate; struct Test<'a> { #[validate(length(min = 1))] s: &'a str, - #[validate(length(min = 1, max = 2))] - s2: &'a str, - #[validate(length(equal = 1))] - s3: &'a str, - #[validate(length(max = 1))] - s4: &'a str, + #[validate(length(min = 1))] + s2: Option<&'a str>, } fn main() {} diff --git a/validator_derive/tests/test_derive.rs b/validator_derive/tests/test_derive.rs index 7ff542b..5d61755 100644 --- a/validator_derive/tests/test_derive.rs +++ b/validator_derive/tests/test_derive.rs @@ -272,14 +272,43 @@ fn test_can_check_regex_validator() { assert!(s2.validate().is_err()); } -// -//#[test] -//fn test_can_validate_option_fields() { -// #[derive(Debug, Validate)] -// struct PutStruct<'a> { -// #[validate(length(min = "1", max = "10"))] -// name: Option<&'a str>, -// } -// let s = PutStruct {name: Some("al")}; -// assert!(s.validate().is_ok()); -//} + +#[test] +fn test_can_validate_option_fields() { + lazy_static! { + static ref RE2: Regex = Regex::new(r"[a-z]{2}").unwrap(); + } + + #[derive(Debug, Validate)] + struct PutStruct<'a> { + #[validate(length(min = "1", max = "10"))] + name: Option<&'a str>, + #[validate(range(min = "1", max = "10"))] + range: Option<usize>, + #[validate(email)] + email: Option<&'a str>, + #[validate(url)] + url: Option<&'a str>, + #[validate(contains = "@")] + text: Option<&'a str>, + #[validate(regex = "RE2")] + re: Option<&'a str>, + #[validate(custom = "check_str")] + custom: Option<&'a str>, + } + + fn check_str(val: &str) -> Option<String> { + None + } + + let s = PutStruct { + name: Some("al"), + range: Some(2), + email: Some("hi@gmail.com"), + url: Some("http://google.com"), + text: Some("@someone"), + re: Some("hi"), + custom: Some("hey"), + }; + assert!(s.validate().is_ok()); +} |
