diff options
| author | Vincent Prouillet | 2017-11-08 17:16:24 +0100 |
|---|---|---|
| committer | Vincent Prouillet | 2017-11-08 17:16:24 +0100 |
| commit | 9b175bc3ca7a2a65add0ec4a42a79b5c9f47314b (patch) | |
| tree | 0d359b1d047841e1be14aeca8e0cf157cbdd6b42 | |
| parent | f04f03f33c057e160856800bdd1a106b035d713b (diff) | |
| download | validator-9b175bc3ca7a2a65add0ec4a42a79b5c9f47314b.tar.bz2 | |
Add missing credit_card derive impl
| -rw-r--r-- | validator/Cargo.toml | 2 | ||||
| -rw-r--r-- | validator/src/validation/mod.rs | 4 | ||||
| -rw-r--r-- | validator_derive/Cargo.toml | 9 | ||||
| -rw-r--r-- | validator_derive/src/lib.rs | 6 | ||||
| -rw-r--r-- | validator_derive/src/quoting.rs | 17 | ||||
| -rw-r--r-- | validator_derive/tests/compile_test.rs | 2 | ||||
| -rw-r--r-- | validator_derive/tests/complex.rs | 1 | ||||
| -rw-r--r-- | validator_derive/tests/credit_card.rs | 77 |
8 files changed, 111 insertions, 7 deletions
diff --git a/validator/Cargo.toml b/validator/Cargo.toml index f081b7f..06ef32c 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "validator" -version = "0.6.1" +version = "0.6.2" authors = ["Vincent Prouillet <vincent@wearewizards.io>"] license = "MIT" description = "Common validation functions (email, url, length, ...) and trait" diff --git a/validator/src/validation/mod.rs b/validator/src/validation/mod.rs index 6a21322..93a9ec3 100644 --- a/validator/src/validation/mod.rs +++ b/validator/src/validation/mod.rs @@ -35,7 +35,7 @@ pub enum Validator { max: Option<u64>, equal: Option<u64>, }, - CreditCard(String), + CreditCard, #[cfg(feature = "phone")] Phone, } @@ -51,7 +51,7 @@ impl Validator { Validator::Regex(_) => "regex", Validator::Range {..} => "range", Validator::Length {..} => "length", - Validator::CreditCard(_) => "credit_card", + Validator::CreditCard => "credit_card", #[cfg(feature = "phone")] Validator::Phone => "phone", } diff --git a/validator_derive/Cargo.toml b/validator_derive/Cargo.toml index 99e88c2..707dc84 100644 --- a/validator_derive/Cargo.toml +++ b/validator_derive/Cargo.toml @@ -25,5 +25,10 @@ regex = "0.2" lazy_static = "0.2" [dependencies.validator] -# path = "../validator" -version = "0.6.0" +path = "../validator" +# version = "0.6" + +[dev-dependencies.validator] +path = "../validator" +# version = "0.6" +features = ["phone"] diff --git a/validator_derive/src/lib.rs b/validator_derive/src/lib.rs index 9cfc381..adb5515 100644 --- a/validator_derive/src/lib.rs +++ b/validator_derive/src/lib.rs @@ -270,6 +270,10 @@ fn find_validators_for_field(field: &syn::Field, field_types: &HashMap<String, S assert_string_type("phone", field_type); validators.push(FieldValidation::new(Validator::Phone)); }, + "credit_card" => { + assert_string_type("credit_card", field_type); + validators.push(FieldValidation::new(Validator::CreditCard)); + }, _ => panic!("Unexpected validator: {}", name) }, // custom, contains, must_match, regex @@ -315,7 +319,7 @@ fn find_validators_for_field(field: &syn::Field, field_types: &HashMap<String, S assert_has_range(rust_ident.clone(), field_type); validators.push(extract_range_validation(rust_ident.clone(), meta_items)); }, - "email" | "url" | "phone" => { + "email" | "url" | "phone" | "credit_card" => { validators.push(extract_argless_validation(name.to_string(), rust_ident.clone(), meta_items)); }, "custom" => { diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs index 24c50d8..7e2fe48 100644 --- a/validator_derive/src/quoting.rs +++ b/validator_derive/src/quoting.rs @@ -158,6 +158,22 @@ pub fn quote_range_validation(field_quoter: &FieldQuoter, validation: &FieldVali unreachable!() } +pub fn quote_credit_card_validation(field_quoter: &FieldQuoter, validation: &FieldValidation) -> quote::Tokens { + let field_name = &field_quoter.name; + let validator_param = field_quoter.quote_validator_param(); + + let quoted_error = quote_error(&validation); + let quoted = quote!( + if !::validator::validate_credit_card(#validator_param) { + #quoted_error + err.add_param(::std::borrow::Cow::from("value"), &#validator_param); + errors.add(#field_name, err); + } + ); + + field_quoter.wrap_if_option(quoted) +} + pub fn quote_phone_validation(field_quoter: &FieldQuoter, validation: &FieldValidation) -> quote::Tokens { let field_name = &field_quoter.name; let validator_param = field_quoter.quote_validator_param(); @@ -309,6 +325,7 @@ pub fn quote_field_validation(field_quoter: &FieldQuoter, validation: &FieldVali Validator::Custom(_) => quote_custom_validation(&field_quoter, validation), Validator::Contains(_) => quote_contains_validation(&field_quoter, validation), Validator::Regex(_) => quote_regex_validation(&field_quoter, validation), + Validator::CreditCard => quote_credit_card_validation(&field_quoter, validation), Validator::Phone => quote_phone_validation(&field_quoter, validation), } } diff --git a/validator_derive/tests/compile_test.rs b/validator_derive/tests/compile_test.rs index b6fe8e5..6c2b251 100644 --- a/validator_derive/tests/compile_test.rs +++ b/validator_derive/tests/compile_test.rs @@ -3,7 +3,7 @@ extern crate compiletest_rs as compiletest; use std::path::PathBuf; fn run_mode(mode: &'static str) { - let mut config = compiletest::default_config(); + let mut config = compiletest::Config::default(); let cfg_mode = mode.parse().expect("Invalid mode"); config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_string()); diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs index b9363e4..db62600 100644 --- a/validator_derive/tests/complex.rs +++ b/validator_derive/tests/complex.rs @@ -170,6 +170,7 @@ fn test_works_with_question_mark_operator() { let signup = SignupData { mail: "invalid_email".to_string(), site: "http://hello.com".to_string(), + phone: "+14152370800".to_string(), first_name: "Bob".to_string(), age: 18, }; diff --git a/validator_derive/tests/credit_card.rs b/validator_derive/tests/credit_card.rs new file mode 100644 index 0000000..2f1d720 --- /dev/null +++ b/validator_derive/tests/credit_card.rs @@ -0,0 +1,77 @@ +#[macro_use] +extern crate validator_derive; +extern crate validator; + +use validator::Validate; + + +#[test] +fn can_validate_valid_card_number() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(credit_card)] + val: String, + } + + let s = TestStruct { + val: "5236313877109142".to_string(), + }; + + assert!(s.validate().is_ok()); +} + +#[test] +fn bad_credit_card_fails_validation() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(email)] + 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, "email"); + assert_eq!(errs["val"][0].params["value"], "bob"); +} + +#[test] +fn can_specify_code_for_credit_card() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(credit_card(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"); +} + +#[test] +fn can_specify_message_for_credit_card() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(credit_card(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"); +} |
