diff options
| author | Vincent Prouillet | 2018-04-14 12:30:47 +0200 |
|---|---|---|
| committer | GitHub | 2018-04-14 12:30:47 +0200 |
| commit | 5158f40f29b5feb420207c73fef18d320f38857a (patch) | |
| tree | 86ecd495aef1ce9ad6fd70d4a10d405a971561b1 | |
| parent | 40247814ca59cbbc6cb9cb60e861f4c19c3f5127 (diff) | |
| parent | 56a5a34c8653f6475d00ca99c22f50d255a0669d (diff) | |
| download | validator-5158f40f29b5feb420207c73fef18d320f38857a.tar.bz2 | |
Merge pull request #46 from rustodon/fix_path_parsing_regex
Fix path parsing for regex validators
| -rw-r--r-- | validator_derive/src/quoting.rs | 2 | ||||
| -rw-r--r-- | validator_derive/tests/run-pass/regex.rs | 27 |
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() {} |
