aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/src
diff options
context:
space:
mode:
authorVincent Prouillet2017-01-18 01:26:44 +0900
committerVincent Prouillet2017-01-18 01:26:44 +0900
commit3de07e80ed2d961bc0b07d7971a04c590f0a8290 (patch)
tree2f7c51f7bb88191078bde2cce0a0e772f6370452 /validator_derive/src
parent32ada012eb1de7da60d979b65844d008fc75b458 (diff)
downloadvalidator-3de07e80ed2d961bc0b07d7971a04c590f0a8290.tar.bz2
Add regex validator
Diffstat (limited to 'validator_derive/src')
-rw-r--r--validator_derive/src/lib.rs14
1 files changed, 14 insertions, 0 deletions
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<String, S
None => 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) => {