From 89416fd17eadd6e04b1b1a6829302e56a4f9a806 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sun, 18 Mar 2018 09:16:24 +0545 Subject: Added assertions for Option> --- validator_derive/src/asserts.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'validator_derive') diff --git a/validator_derive/src/asserts.rs b/validator_derive/src/asserts.rs index 65db5cf..53d76ae 100644 --- a/validator_derive/src/asserts.rs +++ b/validator_derive/src/asserts.rs @@ -1,5 +1,5 @@ -pub static NUMBER_TYPES: [&'static str; 24] = [ +pub static NUMBER_TYPES: [&'static str; 36] = [ "usize", "u8", "u16", "u32", "u64", "isize", "i8", "i16", "i32", "i64", "f32", "f64", @@ -7,6 +7,10 @@ pub static NUMBER_TYPES: [&'static str; 24] = [ "Option", "Option", "Option", "Option", "Option", "Option", "Option", "Option", "Option", "Option", "Option", "Option", + + "Option>", "Option>", "Option>", "Option>", "Option>", + "Option>", "Option>", "Option>", "Option>", "Option>", + "Option>", "Option>", ]; @@ -14,7 +18,9 @@ pub fn assert_string_type(name: &str, field_type: &String) { if field_type != "String" && field_type != "&str" && field_type != "Option" - && !(field_type.starts_with("Option<") && field_type.ends_with("str>")) { + && field_type != "Option>" + && !(field_type.starts_with("Option<") && field_type.ends_with("str>")) + && !(field_type.starts_with("Option>")) { panic!("`{}` validator can only be used on String, &str or an Option of those", name); } } @@ -33,9 +39,12 @@ pub fn assert_has_len(field_name: String, field_type: &String) { if field_type != "String" && !field_type.starts_with("Vec<") && !field_type.starts_with("Option")) + && !(field_type.starts_with("Option>")) && field_type != "&str" { panic!( "Validator `length` can only be used on types `String`, `&str` or `Vec` but found `{}` for field `{}`", -- cgit v1.2.3 From c4c325ef653126a5d0150a3aff03f9cef03a6d6e Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sun, 18 Mar 2018 09:16:59 +0545 Subject: Optional validator params and wraping for Option> --- validator_derive/src/quoting.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'validator_derive') diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs index bf2a531..2ffade8 100644 --- a/validator_derive/src/quoting.rs +++ b/validator_derive/src/quoting.rs @@ -40,7 +40,8 @@ impl FieldQuoter { pub fn get_optional_validator_param(&self) -> quote::Tokens { let ident = &self.ident; - if self._type.starts_with("Option<&") || NUMBER_TYPES.contains(&self._type.as_ref()) { + if self._type.starts_with("Option<&") || self._type.starts_with("Option quote::Tokens { let field_ident = &self.ident; let optional_pattern_matched = self.get_optional_validator_param(); - if self._type.starts_with("Option<") { + if self._type.starts_with("Option { #[validate(length(min = "1", max = "10"))] name: Option<&'a str>, + #[validate(length(min = "1", max = "10"))] + address: Option>, + #[validate(range(min = "1", max = "100"))] + age: Option>, #[validate(range(min = "1", max = "10"))] range: Option, #[validate(email)] @@ -101,6 +105,8 @@ fn test_can_validate_option_fields_with_lifetime() { let s = PutStruct { name: Some("al"), + address: Some(Some("gol")), + age: Some(Some(20)), range: Some(2), email: Some("hi@gmail.com"), url: Some("http://google.com"), @@ -122,7 +128,11 @@ fn test_can_validate_option_fields_without_lifetime() { #[validate(length(min = "1", max = "10"))] name: Option, #[validate(length(min = "1", max = "10"))] + address: Option>, + #[validate(length(min = "1", max = "10"))] ids: Option>, + #[validate(range(min = "1", max = "100"))] + age: Option>, #[validate(range(min = "1", max = "10"))] range: Option, #[validate(email)] @@ -143,7 +153,9 @@ fn test_can_validate_option_fields_without_lifetime() { let s = PutStruct { name: Some("al".to_string()), + address: Some(Some("gol".to_string())), ids: Some(vec![1, 2, 3]), + age: Some(Some(20)), range: Some(2), email: Some("hi@gmail.com".to_string()), url: Some("http://google.com".to_string()), -- cgit v1.2.3 From bcc9b005a4784d1c67a1622c93589377dae4d11c Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sun, 18 Mar 2018 09:21:10 +0545 Subject: Added Option> field to test --- validator_derive/tests/complex.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'validator_derive') diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs index 0a4c5f0..e7495b4 100644 --- a/validator_derive/tests/complex.rs +++ b/validator_derive/tests/complex.rs @@ -131,6 +131,8 @@ fn test_can_validate_option_fields_without_lifetime() { address: Option>, #[validate(length(min = "1", max = "10"))] ids: Option>, + #[validate(length(min = "1", max = "10"))] + opt_ids: Option>>, #[validate(range(min = "1", max = "100"))] age: Option>, #[validate(range(min = "1", max = "10"))] @@ -155,6 +157,7 @@ fn test_can_validate_option_fields_without_lifetime() { name: Some("al".to_string()), address: Some(Some("gol".to_string())), ids: Some(vec![1, 2, 3]), + opt_ids: Some(Some(vec![1, 2, 3])), age: Some(Some(20)), range: Some(2), email: Some("hi@gmail.com".to_string()), -- cgit v1.2.3 From 2911d169468a232e90be81b6a00e21ce18be74b0 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Sun, 18 Mar 2018 09:25:40 +0545 Subject: Added test to validate None values --- validator_derive/tests/complex.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'validator_derive') diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs index e7495b4..bbbc2e6 100644 --- a/validator_derive/tests/complex.rs +++ b/validator_derive/tests/complex.rs @@ -185,3 +185,35 @@ fn test_works_with_question_mark_operator() { assert!(some_fn().is_err()); } + +#[test] +fn test_works_with_none_values() { + #[derive(Debug, Validate)] + struct PutStruct { + #[validate(length(min = "1", max = "10"))] + name: Option, + #[validate(length(min = "1", max = "10"))] + address: Option>, + #[validate(range(min = "1", max = "100"))] + age: Option>, + #[validate(range(min = "1", max = "10"))] + range: Option, + } + + let p = PutStruct { + name: None, + address: None, + age: None, + range: None, + }; + + let q = PutStruct { + name: None, + address: Some(None), + age: Some(None), + range: None, + }; + + assert!(p.validate().is_ok()); + assert!(q.validate().is_ok()); +} \ No newline at end of file -- cgit v1.2.3