diff options
Diffstat (limited to 'validator_derive/tests/credit_card.rs')
| -rw-r--r-- | validator_derive/tests/credit_card.rs | 77 |
1 files changed, 77 insertions, 0 deletions
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"); +} |
