aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--validator_derive/src/quoting.rs2
-rw-r--r--validator_derive/tests/run-pass/regex.rs27
2 files changed, 28 insertions, 1 deletions
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() {}