diff options
Diffstat (limited to 'validator_derive/tests')
| -rw-r--r-- | validator_derive/tests/complex.rs | 10 | ||||
| -rw-r--r-- | validator_derive/tests/phone.rs | 77 | ||||
| -rw-r--r-- | validator_derive/tests/run-pass/phone.rs | 13 |
3 files changed, 100 insertions, 0 deletions
diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs index db5f1bf..80ae360 100644 --- a/validator_derive/tests/complex.rs +++ b/validator_derive/tests/complex.rs @@ -33,6 +33,8 @@ fn validate_signup(data: &SignupData) -> Result<(), ValidationError> { struct SignupData { #[validate(email)] mail: String, + #[validate(phone)] + phone: String, #[validate(url)] site: String, #[validate(length(min = "1"), custom = "validate_unique_username")] @@ -47,6 +49,7 @@ struct SignupData { fn is_fine_with_many_valid_validations() { let signup = SignupData { mail: "bob@bob.com".to_string(), + phone: "+14152370800".to_string(), site: "http://hello.com".to_string(), first_name: "Bob".to_string(), age: 18, @@ -59,6 +62,7 @@ fn is_fine_with_many_valid_validations() { fn failed_validation_points_to_original_field_name() { let signup = SignupData { mail: "bob@bob.com".to_string(), + phone: "+14152370800".to_string(), site: "http://hello.com".to_string(), first_name: "".to_string(), age: 18, @@ -85,6 +89,8 @@ fn test_can_validate_option_fields_with_lifetime() { range: Option<usize>, #[validate(email)] email: Option<&'a str>, + #[validate(phone)] + phone: Option<&'a str>, #[validate(url)] url: Option<&'a str>, #[validate(contains = "@")] @@ -103,6 +109,7 @@ fn test_can_validate_option_fields_with_lifetime() { name: Some("al"), range: Some(2), email: Some("hi@gmail.com"), + phone: Some("+14152370800"), url: Some("http://google.com"), text: Some("@someone"), re: Some("hi"), @@ -127,6 +134,8 @@ fn test_can_validate_option_fields_without_lifetime() { range: Option<usize>, #[validate(email)] email: Option<String>, + #[validate(phone)] + phone: Option<String>, #[validate(url)] url: Option<String>, #[validate(contains = "@")] @@ -146,6 +155,7 @@ fn test_can_validate_option_fields_without_lifetime() { ids: Some(vec![1, 2, 3]), range: Some(2), email: Some("hi@gmail.com".to_string()), + phone: Some("+14152370800".to_string()), url: Some("http://google.com".to_string()), text: Some("@someone".to_string()), re: Some("hi".to_string()), diff --git a/validator_derive/tests/phone.rs b/validator_derive/tests/phone.rs new file mode 100644 index 0000000..675d4c5 --- /dev/null +++ b/validator_derive/tests/phone.rs @@ -0,0 +1,77 @@ +#[macro_use] +extern crate validator_derive; +extern crate validator; + +use validator::Validate; + + +#[test] +fn can_validate_phone_ok() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(phone)] + val: String, + } + + let s = TestStruct { + val: "+14152370800".to_string(), + }; + + assert!(s.validate().is_ok()); +} + +#[test] +fn bad_phone_fails_validation() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(phone)] + val: String, + } + + let s = TestStruct { + val: "bob".to_string(), + }; + let res = s.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err().inner(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].code, "phone"); +} + +#[test] +fn can_specify_code_for_phone() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(phone(code = "oops"))] + val: String, + } + let s = TestStruct { + val: "bob".to_string(), + }; + let res = s.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err().inner(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].code, "oops"); + assert_eq!(errs["val"][0].params["value"], "bob"); +} + +#[test] +fn can_specify_message_for_phone() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(phone(message = "oops"))] + val: String, + } + let s = TestStruct { + val: "bob".to_string(), + }; + let res = s.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err().inner(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].clone().message.unwrap(), "oops"); +} diff --git a/validator_derive/tests/run-pass/phone.rs b/validator_derive/tests/run-pass/phone.rs new file mode 100644 index 0000000..d6dff9e --- /dev/null +++ b/validator_derive/tests/run-pass/phone.rs @@ -0,0 +1,13 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +struct Test { + #[validate(phone)] + s: String, +} + +fn main() {} |
