aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Prouillet2017-11-08 17:47:59 +0100
committerVincent Prouillet2017-11-08 17:47:59 +0100
commitdcc3b0a42f74bedc6cf57feea0f441e7166aa027 (patch)
treea62db24bd3d35349870810a2d7974ce15add8e92
parent14a5b7a695de67f4bc2e38d5eeae2eea11fed743 (diff)
downloadvalidator-dcc3b0a42f74bedc6cf57feea0f441e7166aa027.tar.bz2
Feature gate phone feature in validator_derive as well
-rw-r--r--.travis.yml2
-rw-r--r--README.md2
-rw-r--r--validator_derive/Cargo.toml7
-rw-r--r--validator_derive/src/lib.rs1
-rw-r--r--validator_derive/src/quoting.rs2
-rw-r--r--validator_derive/src/validation.rs2
-rw-r--r--validator_derive/tests/complex.rs11
-rw-r--r--validator_derive/tests/credit_card.rs4
8 files changed, 12 insertions, 19 deletions
diff --git a/.travis.yml b/.travis.yml
index 7f04152..c286b5a 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,6 +6,6 @@ rust:
script:
- (cd validator && cargo test --all-features)
- - (cd validator_derive && cargo test)
+ - (cd validator_derive && cargo test --all-features)
notifications:
email: false
diff --git a/README.md b/README.md
index c034c70..a7d57d5 100644
--- a/README.md
+++ b/README.md
@@ -166,7 +166,7 @@ Examples:
Tests whether the String is a valid phone number (in international format, ie.
containing the country indicator like `+14152370800` for an US number — where `4152370800`
is the national number equivalent, which is seen as invalid).
-To use this validator, you must enable the `phone` feature for the `validator` crate.
+To use this validator, you must enable the `phone` feature for the `validator_derive` crate.
This validator doesn't take any arguments: `#[validate(phone)]`;
### custom
diff --git a/validator_derive/Cargo.toml b/validator_derive/Cargo.toml
index 2e5f31f..49560a0 100644
--- a/validator_derive/Cargo.toml
+++ b/validator_derive/Cargo.toml
@@ -11,6 +11,9 @@ keywords = ["validation", "api", "validator"]
[lib]
proc-macro = true
+[features]
+phone = ["validator/phone"]
+
[dependencies]
syn = "0.11"
quote = "0.3"
@@ -28,7 +31,3 @@ lazy_static = "0.2"
# 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 adb5515..1678883 100644
--- a/validator_derive/src/lib.rs
+++ b/validator_derive/src/lib.rs
@@ -266,6 +266,7 @@ fn find_validators_for_field(field: &syn::Field, field_types: &HashMap<String, S
assert_string_type("url", field_type);
validators.push(FieldValidation::new(Validator::Url));
},
+ #[cfg(feature = "phone")]
"phone" => {
assert_string_type("phone", field_type);
validators.push(FieldValidation::new(Validator::Phone));
diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs
index 7e2fe48..f7968e2 100644
--- a/validator_derive/src/quoting.rs
+++ b/validator_derive/src/quoting.rs
@@ -174,6 +174,7 @@ pub fn quote_credit_card_validation(field_quoter: &FieldQuoter, validation: &Fie
field_quoter.wrap_if_option(quoted)
}
+#[cfg(feature = "phone")]
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();
@@ -326,6 +327,7 @@ pub fn quote_field_validation(field_quoter: &FieldQuoter, validation: &FieldVali
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),
+ #[cfg(feature = "phone")]
Validator::Phone => quote_phone_validation(&field_quoter, validation),
}
}
diff --git a/validator_derive/src/validation.rs b/validator_derive/src/validation.rs
index 259db70..7598d79 100644
--- a/validator_derive/src/validation.rs
+++ b/validator_derive/src/validation.rs
@@ -215,6 +215,8 @@ pub fn extract_argless_validation(validator_name: String, field: String, meta_it
let validator = match validator_name.as_ref() {
"email" => Validator::Email,
+ "credit_card" => Validator::CreditCard,
+ #[cfg(feature = "phone")]
"phone" => Validator::Phone,
_ => Validator::Url
};
diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs
index db62600..0bd74fa 100644
--- a/validator_derive/tests/complex.rs
+++ b/validator_derive/tests/complex.rs
@@ -33,8 +33,6 @@ fn validate_signup(data: &SignupData) -> Result<(), ValidationError> {
struct SignupData {
#[validate(email)]
mail: String,
- #[validate(phone)]
- phone: String,
#[validate(url)]
site: String,
#[validate(length(min = "1"), custom = "validate_unique_username")]
@@ -49,7 +47,6 @@ struct SignupData {
fn is_fine_with_many_valid_validations() {
let signup = SignupData {
mail: "bob@bob.com".to_string(),
- phone: "+14152370800".to_string(),
site: "http://hello.com".to_string(),
first_name: "Bob".to_string(),
age: 18,
@@ -62,7 +59,6 @@ fn is_fine_with_many_valid_validations() {
fn failed_validation_points_to_original_field_name() {
let signup = SignupData {
mail: "bob@bob.com".to_string(),
- phone: "+14152370800".to_string(),
site: "http://hello.com".to_string(),
first_name: "".to_string(),
age: 18,
@@ -89,8 +85,6 @@ fn test_can_validate_option_fields_with_lifetime() {
range: Option<usize>,
#[validate(email)]
email: Option<&'a str>,
- #[validate(phone)]
- phone: Option<&'a str>,
#[validate(url)]
url: Option<&'a str>,
#[validate(contains = "@")]
@@ -109,7 +103,6 @@ fn test_can_validate_option_fields_with_lifetime() {
name: Some("al"),
range: Some(2),
email: Some("hi@gmail.com"),
- phone: Some("+14152370800"),
url: Some("http://google.com"),
text: Some("@someone"),
re: Some("hi"),
@@ -134,8 +127,6 @@ fn test_can_validate_option_fields_without_lifetime() {
range: Option<usize>,
#[validate(email)]
email: Option<String>,
- #[validate(phone)]
- phone: Option<String>,
#[validate(url)]
url: Option<String>,
#[validate(contains = "@")]
@@ -155,7 +146,6 @@ fn test_can_validate_option_fields_without_lifetime() {
ids: Some(vec![1, 2, 3]),
range: Some(2),
email: Some("hi@gmail.com".to_string()),
- phone: Some("+14152370800".to_string()),
url: Some("http://google.com".to_string()),
text: Some("@someone".to_string()),
re: Some("hi".to_string()),
@@ -170,7 +160,6 @@ 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
index 2f1d720..fcb30a1 100644
--- a/validator_derive/tests/credit_card.rs
+++ b/validator_derive/tests/credit_card.rs
@@ -24,7 +24,7 @@ fn can_validate_valid_card_number() {
fn bad_credit_card_fails_validation() {
#[derive(Debug, Validate)]
struct TestStruct {
- #[validate(email)]
+ #[validate(credit_card)]
val: String,
}
@@ -36,7 +36,7 @@ fn bad_credit_card_fails_validation() {
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].code, "credit_card");
assert_eq!(errs["val"][0].params["value"], "bob");
}