diff options
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | validator/src/types.rs | 4 | ||||
| -rw-r--r-- | validator/src/validation/email.rs | 2 | ||||
| -rw-r--r-- | validator_derive/src/lib.rs | 63 | ||||
| -rw-r--r-- | validator_derive/src/lit.rs | 2 | ||||
| -rw-r--r-- | validator_derive/src/quoting.rs | 2 | ||||
| -rw-r--r-- | validator_derive/src/validation.rs | 42 | ||||
| -rw-r--r-- | validator_derive/tests/regex.rs | 8 | ||||
| -rw-r--r-- | validator_derive/tests/run-pass/custom.rs | 4 | ||||
| -rw-r--r-- | validator_derive/tests/run-pass/regex.rs | 4 |
10 files changed, 79 insertions, 62 deletions
@@ -302,6 +302,10 @@ For example, the following attributes all work: ### validator +#### 0.10.0 (2019/10/18) + +- Add `non_control_characters` validation + #### 0.9.0 (2019/05/xx) - `ValidationErrors::errors` and `ValidationErrors::field_errors` now use `&self` instead of `self` @@ -333,6 +337,12 @@ For example, the following attributes all work: ### validator_derive + +#### 0.10.0 (2019/10/18) + +- Update syn & quote +- Move to edition 2018 + #### 0.9.0 (2019/05/xx) - Use literals in macros now that it's stable -> bumping minimum Rust version to 1.30 diff --git a/validator/src/types.rs b/validator/src/types.rs index f72308c..5d90d04 100644 --- a/validator/src/types.rs +++ b/validator/src/types.rs @@ -3,7 +3,7 @@ use std::collections::{hash_map::Entry::Vacant, BTreeMap, HashMap}; use std::{self, fmt}; use serde::ser::Serialize; -use serde_derive::{Serialize, Deserialize}; +use serde_derive::{Deserialize, Serialize}; use serde_json::{to_value, Value}; #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] @@ -169,7 +169,7 @@ impl std::error::Error for ValidationErrors { fn description(&self) -> &str { "Validation failed" } - fn cause(&self) -> Option<& dyn std::error::Error> { + fn cause(&self) -> Option<&dyn std::error::Error> { None } } diff --git a/validator/src/validation/email.rs b/validator/src/validation/email.rs index 3f71e22..7a48f19 100644 --- a/validator/src/validation/email.rs +++ b/validator/src/validation/email.rs @@ -1,7 +1,7 @@ use idna::domain_to_ascii; +use lazy_static::lazy_static; use regex::Regex; use std::borrow::Cow; -use lazy_static::lazy_static; use crate::validation::ip::validate_ip; 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, } |
