aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md10
-rw-r--r--validator/Cargo.toml1
-rw-r--r--validator/src/lib.rs2
-rw-r--r--validator/src/validation/cards.rs24
-rw-r--r--validator/src/validation/mod.rs3
5 files changed, 40 insertions, 0 deletions
diff --git a/README.md b/README.md
index 3a8aa0c..bbbe4de 100644
--- a/README.md
+++ b/README.md
@@ -151,6 +151,16 @@ Examples:
#[validate(regex = "ALLOWED_USERNAMES_RE"))]
```
+### credit\_card
+Test whetever the string is a valid credit card number. To use this validator,
+you must enable the `credit_cards` feature for the `validator` crate.
+
+Examples:
+
+```rust
+#[validate(credit_card)]
+```
+
### custom
Calls one of your function to do a custom validation.
The field will be given as parameter and it should return a `Option<String>` representing the error code,
diff --git a/validator/Cargo.toml b/validator/Cargo.toml
index 7cad55b..b138982 100644
--- a/validator/Cargo.toml
+++ b/validator/Cargo.toml
@@ -16,3 +16,4 @@ idna = "0.1"
serde = "1"
serde_derive = "1"
serde_json = "1"
+card-validate = "2.1"
diff --git a/validator/src/lib.rs b/validator/src/lib.rs
index 35afbe4..9bdb716 100644
--- a/validator/src/lib.rs
+++ b/validator/src/lib.rs
@@ -7,6 +7,7 @@ extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
+extern crate card_validate;
mod types;
mod validation;
@@ -19,6 +20,7 @@ pub use validation::range::{validate_range};
pub use validation::urls::{validate_url};
pub use validation::must_match::{validate_must_match};
pub use validation::contains::{validate_contains};
+pub use validation::cards::{validate_credit_card};
pub use validation::Validator;
pub use types::{ValidationErrors, ValidationError};
diff --git a/validator/src/validation/cards.rs b/validator/src/validation/cards.rs
new file mode 100644
index 0000000..b72663d
--- /dev/null
+++ b/validator/src/validation/cards.rs
@@ -0,0 +1,24 @@
+use card_validate::{Validate as CardValidate};
+
+
+pub fn validate_credit_card(card: &str) -> bool {
+ CardValidate::from(card).is_ok()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::validate_credit_card;
+ #[test]
+ fn test_credit_card() {
+ let tests = vec![
+ ("4539571147647251", true),
+ ("343380440754432", true),
+ ("zduhefljsdfKJKJZHUI", false),
+ ("5236313877109141", false),
+ ];
+
+ for (input, expected) in tests {
+ assert_eq!(validate_credit_card(input), expected);
+ }
+ }
+}
diff --git a/validator/src/validation/mod.rs b/validator/src/validation/mod.rs
index 939cacf..b7d9a1d 100644
--- a/validator/src/validation/mod.rs
+++ b/validator/src/validation/mod.rs
@@ -5,6 +5,7 @@ pub mod range;
pub mod urls;
pub mod must_match;
pub mod contains;
+pub mod cards;
/// Contains all the validators that can be used
///
@@ -32,6 +33,7 @@ pub enum Validator {
max: Option<u64>,
equal: Option<u64>,
},
+ CreditCard(String),
}
impl Validator {
@@ -45,6 +47,7 @@ impl Validator {
Validator::Regex(_) => "regex",
Validator::Range {..} => "range",
Validator::Length {..} => "length",
+ Validator::CreditCard(_) => "credit_card",
}
}
}