aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive
diff options
context:
space:
mode:
authorVincent Prouillet2019-10-17 23:02:51 +0200
committerVincent Prouillet2019-10-17 23:02:51 +0200
commit52cd62998b8f45be56512ce1d89aef8bf1a9f670 (patch)
tree7469738d72627423c5b99df7dfae099951f4d322 /validator_derive
parentf57048e58c59317ec18a37a86e7fd848670fff85 (diff)
downloadvalidator-52cd62998b8f45be56512ce1d89aef8bf1a9f670.tar.bz2
Fix bug + cargo fmt
Diffstat (limited to 'validator_derive')
-rw-r--r--validator_derive/src/lib.rs63
-rw-r--r--validator_derive/src/lit.rs2
-rw-r--r--validator_derive/src/quoting.rs2
-rw-r--r--validator_derive/src/validation.rs42
-rw-r--r--validator_derive/tests/regex.rs8
-rw-r--r--validator_derive/tests/run-pass/custom.rs4
-rw-r--r--validator_derive/tests/run-pass/regex.rs4
7 files changed, 66 insertions, 59 deletions
diff --git a/validator_derive/src/lib.rs b/validator_derive/src/lib.rs
index 18472a9..6f74202 100644
--- a/validator_derive/src/lib.rs
+++ b/validator_derive/src/lib.rs
@@ -3,9 +3,9 @@ extern crate proc_macro;
use if_chain::if_chain;
use quote::quote;
-use syn::parse_quote;
use quote::ToTokens;
use std::collections::HashMap;
+use syn::parse_quote;
use validator::Validator;
mod asserts;
@@ -259,32 +259,37 @@ fn find_validators_for_field(
match *meta_item {
syn::NestedMeta::Meta(ref item) => match *item {
// email, url, phone, credit_card, non_control_character
- syn::Meta::Path(ref name) => match name.get_ident().unwrap().to_string().as_ref() {
- "email" => {
- assert_string_type("email", field_type);
- validators.push(FieldValidation::new(Validator::Email));
- }
- "url" => {
- 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));
- }
- #[cfg(feature = "card")]
- "credit_card" => {
- assert_string_type("credit_card", field_type);
- validators.push(FieldValidation::new(Validator::CreditCard));
- }
- #[cfg(feature = "unic")]
- "non_control_character" => {
- assert_string_type("non_control_character", field_type);
- validators.push(FieldValidation::new(Validator::NonControlCharacter));
+ syn::Meta::Path(ref name) => {
+ match name.get_ident().unwrap().to_string().as_ref() {
+ "email" => {
+ assert_string_type("email", field_type);
+ validators.push(FieldValidation::new(Validator::Email));
+ }
+ "url" => {
+ 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));
+ }
+ #[cfg(feature = "card")]
+ "credit_card" => {
+ assert_string_type("credit_card", field_type);
+ validators
+ .push(FieldValidation::new(Validator::CreditCard));
+ }
+ #[cfg(feature = "unic")]
+ "non_control_character" => {
+ assert_string_type("non_control_character", field_type);
+ validators.push(FieldValidation::new(
+ Validator::NonControlCharacter,
+ ));
+ }
+ _ => panic!("Unexpected validator: {:?}", name.get_ident()),
}
- _ => panic!("Unexpected validator: {:?}", name.get_ident()),
- },
+ }
// custom, contains, must_match, regex
syn::Meta::NameValue(syn::MetaNameValue {
ref path, ref lit, ..
@@ -340,7 +345,11 @@ fn find_validators_for_field(
&meta_items,
));
}
- "email" | "url" | "phone" | "credit_card" | "non_control_character" => {
+ "email"
+ | "url"
+ | "phone"
+ | "credit_card"
+ | "non_control_character" => {
validators.push(extract_argless_validation(
ident.to_string(),
rust_ident.clone(),
diff --git a/validator_derive/src/lit.rs b/validator_derive/src/lit.rs
index f5a9386..f11cdd6 100644
--- a/validator_derive/src/lit.rs
+++ b/validator_derive/src/lit.rs
@@ -1,6 +1,6 @@
use proc_macro2;
-use syn;
use quote::quote;
+use syn;
pub fn lit_to_string(lit: &syn::Lit) -> Option<String> {
match *lit {
diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs
index 0ba6af2..dc466ac 100644
--- a/validator_derive/src/quoting.rs
+++ b/validator_derive/src/quoting.rs
@@ -1,7 +1,7 @@
use proc_macro2::{self, Span};
+use quote::quote;
use syn;
use validator::Validator;
-use quote::quote;
use crate::asserts::{COW_TYPE, NUMBER_TYPES};
use crate::lit::{option_f64_to_tokens, option_u64_to_tokens};
diff --git a/validator_derive/src/validation.rs b/validator_derive/src/validation.rs
index 37a2d5a..f0657ef 100644
--- a/validator_derive/src/validation.rs
+++ b/validator_derive/src/validation.rs
@@ -42,8 +42,8 @@ pub fn extract_length_validation(
for meta_item in meta_items {
if let syn::NestedMeta::Meta(ref item) = *meta_item {
if let syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) = *item {
- let ident = path.get_ident().unwrap();
- match ident.to_string().as_ref() {
+ let ident = path.get_ident().unwrap();
+ match ident.to_string().as_ref() {
"message" | "code" => continue,
"min" => {
min = match lit_to_int(lit) {
@@ -160,15 +160,15 @@ pub fn extract_argless_validation(
match *meta_item {
syn::NestedMeta::Meta(ref item) => match *item {
syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) => {
- let ident = path.get_ident().unwrap();
- match ident.to_string().as_ref() {
- "message" | "code" => continue,
- v => panic!(
- "Unknown argument `{}` for validator `{}` on field `{}`",
- v, validator_name, field
- ),
- }
+ let ident = path.get_ident().unwrap();
+ match ident.to_string().as_ref() {
+ "message" | "code" => continue,
+ v => panic!(
+ "Unknown argument `{}` for validator `{}` on field `{}`",
+ v, validator_name, field
+ ),
}
+ }
_ => panic!("unexpected item {:?} while parsing `range` validator", item),
},
_ => unreachable!(),
@@ -207,7 +207,6 @@ pub fn extract_one_arg_validation(
match *meta_item {
syn::NestedMeta::Meta(ref item) => match *item {
syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) => {
-
let ident = path.get_ident().unwrap();
match ident.to_string().as_ref() {
"message" | "code" => continue,
@@ -269,29 +268,28 @@ fn extract_message_and_code(
..
})) = *meta_item
{
- let ident = path.get_ident().unwrap();
- match ident.to_string().as_ref() {
- "code" => {
- code = match lit_to_string(lit) {
+ let ident = path.get_ident().unwrap();
+ match ident.to_string().as_ref() {
+ "code" => {
+ code = match lit_to_string(lit) {
Some(s) => Some(s),
None => panic!(
"Invalid argument type for `code` for validator `{}` on field `{}`: only a string is allowed",
validator_name, field
),
};
- }
- "message" => {
- message = match lit_to_string(lit) {
+ }
+ "message" => {
+ message = match lit_to_string(lit) {
Some(s) => Some(s),
None => panic!(
"Invalid argument type for `message` for validator `{}` on field `{}`: only a string is allowed",
validator_name, field
),
};
- }
- _ => continue,
- }
-
+ }
+ _ => continue,
+ }
}
}
diff --git a/validator_derive/tests/regex.rs b/validator_derive/tests/regex.rs
index 7e3dfc5..d3e3c1e 100644
--- a/validator_derive/tests/regex.rs
+++ b/validator_derive/tests/regex.rs
@@ -16,7 +16,7 @@ lazy_static! {
fn can_validate_valid_regex() {
#[derive(Debug, Validate)]
struct TestStruct {
- #[validate(regex = "RE2")]
+ #[validate(regex = "crate::RE2")]
val: String,
}
@@ -29,7 +29,7 @@ fn can_validate_valid_regex() {
fn bad_value_for_regex_fails_validation() {
#[derive(Debug, Validate)]
struct TestStruct {
- #[validate(regex = "RE2")]
+ #[validate(regex = "crate::RE2")]
val: String,
}
@@ -48,7 +48,7 @@ fn bad_value_for_regex_fails_validation() {
fn can_specify_code_for_regex() {
#[derive(Debug, Validate)]
struct TestStruct {
- #[validate(regex(path = "RE2", code = "oops"))]
+ #[validate(regex(path = "crate::RE2", code = "oops"))]
val: String,
}
let s = TestStruct { val: "2".to_string() };
@@ -65,7 +65,7 @@ fn can_specify_code_for_regex() {
fn can_specify_message_for_regex() {
#[derive(Debug, Validate)]
struct TestStruct {
- #[validate(regex(path = "RE2", message = "oops"))]
+ #[validate(regex(path = "crate::RE2", message = "oops"))]
val: String,
}
let s = TestStruct { val: "2".to_string() };
diff --git a/validator_derive/tests/run-pass/custom.rs b/validator_derive/tests/run-pass/custom.rs
index dff6375..f745de6 100644
--- a/validator_derive/tests/run-pass/custom.rs
+++ b/validator_derive/tests/run-pass/custom.rs
@@ -6,13 +6,13 @@ use validator::{Validate, ValidationError};
#[derive(Validate)]
struct Test {
- #[validate(custom = "validate_something")]
+ #[validate(custom = "crate::validate_something")]
s: String,
}
#[derive(Validate)]
struct TestPath {
- #[validate(custom = "::validate_something")]
+ #[validate(custom = "crate::validate_something")]
s: String,
}
diff --git a/validator_derive/tests/run-pass/regex.rs b/validator_derive/tests/run-pass/regex.rs
index dcac3c0..e15b3a6 100644
--- a/validator_derive/tests/run-pass/regex.rs
+++ b/validator_derive/tests/run-pass/regex.rs
@@ -14,13 +14,13 @@ lazy_static! {
#[derive(Validate)]
struct Test {
- #[validate(regex = "RE2")]
+ #[validate(regex = "crate::RE2")]
s: String,
}
#[derive(Validate)]
struct TestPath {
- #[validate(regex = "::RE2")]
+ #[validate(regex = "crate::RE2")]
s: String,
}