From 56a5a34c8653f6475d00ca99c22f50d255a0669d Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 13 Apr 2018 13:48:12 -0500 Subject: fix path parsing for regex validators see also: 8ca1fe94d779dcccf4b3f4c23a08f3ede7e10956 --- validator_derive/src/quoting.rs | 2 +- validator_derive/tests/run-pass/regex.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 validator_derive/tests/run-pass/regex.rs diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs index b6053f4..a3e25cb 100644 --- a/validator_derive/src/quoting.rs +++ b/validator_derive/src/quoting.rs @@ -307,7 +307,7 @@ pub fn quote_regex_validation(field_quoter: &FieldQuoter, validation: &FieldVali let validator_param = field_quoter.quote_validator_param(); if let Validator::Regex(ref re) = validation.validator { - let re_ident = syn::Ident::from(re.clone()); + let re_ident: syn::Path = syn::parse_str(re).unwrap(); let quoted_error = quote_error(&validation); let quoted = quote!( if !#re_ident.is_match(#validator_param) { diff --git a/validator_derive/tests/run-pass/regex.rs b/validator_derive/tests/run-pass/regex.rs new file mode 100644 index 0000000..dcac3c0 --- /dev/null +++ b/validator_derive/tests/run-pass/regex.rs @@ -0,0 +1,27 @@ +extern crate regex; +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate validator_derive; +extern crate validator; + +use validator::Validate; +use regex::Regex; + +lazy_static! { + static ref RE2: Regex = Regex::new(r"^[a-z]{2}$").unwrap(); +} + +#[derive(Validate)] +struct Test { + #[validate(regex = "RE2")] + s: String, +} + +#[derive(Validate)] +struct TestPath { + #[validate(regex = "::RE2")] + s: String, +} + +fn main() {} -- cgit v1.2.3