aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive
diff options
context:
space:
mode:
authorVincent Prouillet2018-03-20 19:01:23 +0100
committerGitHub2018-03-20 19:01:23 +0100
commitf5b8e9910b9714a5990d173b3a1cc856641ee454 (patch)
treee31551babd75ccc41696347370f11c62d4b649c1 /validator_derive
parentf07c87b0e90f1d2ef8fe806160863ce179d133b7 (diff)
parent2911d169468a232e90be81b6a00e21ce18be74b0 (diff)
downloadvalidator-f5b8e9910b9714a5990d173b3a1cc856641ee454.tar.bz2
Merge pull request #44 from csharad/double-option
Support for validating Option<Option<T>>. Fixes #41
Diffstat (limited to 'validator_derive')
-rw-r--r--validator_derive/src/asserts.rs13
-rw-r--r--validator_derive/src/quoting.rs11
-rw-r--r--validator_derive/tests/complex.rs47
3 files changed, 67 insertions, 4 deletions
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<usize>", "Option<u8>", "Option<u16>", "Option<u32>", "Option<u64>",
"Option<isize>", "Option<i8>", "Option<i16>", "Option<i32>", "Option<i64>",
"Option<f32>", "Option<f64>",
+
+ "Option<Option<usize>>", "Option<Option<u8>>", "Option<Option<u16>>", "Option<Option<u32>>", "Option<Option<u64>>",
+ "Option<Option<isize>>", "Option<Option<i8>>", "Option<Option<i16>>", "Option<Option<i32>>", "Option<Option<i64>>",
+ "Option<Option<f32>>", "Option<Option<f64>>",
];
@@ -14,7 +18,9 @@ pub fn assert_string_type(name: &str, field_type: &String) {
if field_type != "String"
&& field_type != "&str"
&& field_type != "Option<String>"
- && !(field_type.starts_with("Option<") && field_type.ends_with("str>")) {
+ && field_type != "Option<Option<String>>"
+ && !(field_type.starts_with("Option<") && field_type.ends_with("str>"))
+ && !(field_type.starts_with("Option<Option<") && field_type.ends_with("str>>")) {
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<Vec<")
+ && !field_type.starts_with("Option<Option<Vec<")
&& field_type != "Option<String>"
+ && field_type != "Option<Option<String>>"
// a bit ugly
&& !(field_type.starts_with("Option<") && field_type.ends_with("str>"))
+ && !(field_type.starts_with("Option<Option<") && field_type.ends_with("str>>"))
&& field_type != "&str" {
panic!(
"Validator `length` can only be used on types `String`, `&str` or `Vec` but found `{}` for field `{}`",
diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs
index 028e864..b6053f4 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<Option<&")
+ || NUMBER_TYPES.contains(&self._type.as_ref()) {
quote!(#ident)
} else {
quote!(ref #ident)
@@ -52,7 +53,13 @@ impl FieldQuoter {
pub fn wrap_if_option(&self, tokens: quote::Tokens) -> 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<Option<") {
+ return quote!(
+ if let Some(Some(#optional_pattern_matched)) = self.#field_ident {
+ #tokens
+ }
+ )
+ } else if self._type.starts_with("Option<") {
return quote!(
if let Some(#optional_pattern_matched) = self.#field_ident {
#tokens
diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs
index 0bd74fa..bbbc2e6 100644
--- a/validator_derive/tests/complex.rs
+++ b/validator_derive/tests/complex.rs
@@ -81,6 +81,10 @@ fn test_can_validate_option_fields_with_lifetime() {
struct PutStruct<'a> {
#[validate(length(min = "1", max = "10"))]
name: Option<&'a str>,
+ #[validate(length(min = "1", max = "10"))]
+ address: Option<Option<&'a str>>,
+ #[validate(range(min = "1", max = "100"))]
+ age: Option<Option<usize>>,
#[validate(range(min = "1", max = "10"))]
range: Option<usize>,
#[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,13 @@ fn test_can_validate_option_fields_without_lifetime() {
#[validate(length(min = "1", max = "10"))]
name: Option<String>,
#[validate(length(min = "1", max = "10"))]
+ address: Option<Option<String>>,
+ #[validate(length(min = "1", max = "10"))]
ids: Option<Vec<usize>>,
+ #[validate(length(min = "1", max = "10"))]
+ opt_ids: Option<Option<Vec<usize>>>,
+ #[validate(range(min = "1", max = "100"))]
+ age: Option<Option<usize>>,
#[validate(range(min = "1", max = "10"))]
range: Option<usize>,
#[validate(email)]
@@ -143,7 +155,10 @@ 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]),
+ opt_ids: Some(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()),
@@ -170,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<String>,
+ #[validate(length(min = "1", max = "10"))]
+ address: Option<Option<String>>,
+ #[validate(range(min = "1", max = "100"))]
+ age: Option<Option<usize>>,
+ #[validate(range(min = "1", max = "10"))]
+ range: Option<usize>,
+ }
+
+ 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