From 3de07e80ed2d961bc0b07d7971a04c590f0a8290 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 18 Jan 2017 01:26:44 +0900 Subject: Add regex validator --- validator_derive/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'validator_derive/src') diff --git a/validator_derive/src/lib.rs b/validator_derive/src/lib.rs index 7a21ac8..68a4b2e 100644 --- a/validator_derive/src/lib.rs +++ b/validator_derive/src/lib.rs @@ -129,6 +129,14 @@ fn expand_validation(ast: &syn::MacroInput) -> quote::Tokens { } ) }, + &Validator::Regex(ref re) => { + let re_ident = syn::Ident::new(re.clone()); + quote!( + if !#re_ident.is_match(&self.#field_ident) { + errors.add(#name, "regex"); + } + ) + }, }); } } @@ -437,6 +445,12 @@ fn find_validators_for_field(field: &syn::Field, field_types: &HashMap error("invalid argument for `contains` validator: only strings are allowed"), }; }, + "regex" => { + match lit_to_string(val) { + Some(s) => validators.push(Validator::Regex(s)), + None => error("invalid argument for `regex` validator: only strings are allowed"), + }; + } "must_match" => { match lit_to_string(val) { Some(s) => { -- cgit v1.2.3