aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/src/validation.rs
diff options
context:
space:
mode:
authorCaroline Glassberg-Powell2019-04-28 18:45:40 +0100
committerCaroline Glassberg-Powell2019-04-28 19:01:08 +0100
commit6e8effdaa2b47f4056a00d301a9e7a5a1a2fd8dc (patch)
tree781b99ec57192adb777681243b8161fd37a10bae /validator_derive/src/validation.rs
parent4b9fe3939b106c151bff11e490a41212559f9a4a (diff)
downloadvalidator-6e8effdaa2b47f4056a00d301a9e7a5a1a2fd8dc.tar.bz2
Issue #69: Change `range` to require only one arg
Currently range is hard-coded to take both max and min. This is unhelpful in cases where we may only want to check one of the bounds. Update so that it behaves more like the `length` validator and can take either max or min, as well as both. Also ensure that at least one of them is supplied.
Diffstat (limited to 'validator_derive/src/validation.rs')
-rw-r--r--validator_derive/src/validation.rs43
1 files changed, 18 insertions, 25 deletions
diff --git a/validator_derive/src/validation.rs b/validator_derive/src/validation.rs
index a9762db..d9ad5a5 100644
--- a/validator_derive/src/validation.rs
+++ b/validator_derive/src/validation.rs
@@ -95,8 +95,8 @@ pub fn extract_range_validation(
field: String,
meta_items: &Vec<syn::NestedMeta>,
) -> FieldValidation {
- let mut min = 0.0;
- let mut max = 0.0;
+ let mut min = None;
+ let mut max = None;
let (message, code) = extract_message_and_code("range", &field, meta_items);
@@ -104,45 +104,38 @@ pub fn extract_range_validation(
panic!("Invalid attribute #[validate] on field `{}`: {}", field, msg);
};
- // whether it has both `min` and `max`
- let mut has_min = false;
- let mut has_max = false;
-
for meta_item in meta_items {
match *meta_item {
syn::NestedMeta::Meta(ref item) => match *item {
- syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => match ident
- .to_string()
- .as_ref()
- {
- "message" | "code" => continue,
- "min" => {
- min = match lit_to_float(lit) {
- Some(s) => s,
+ syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => {
+ match ident.to_string().as_ref() {
+ "message" | "code" => continue,
+ "min" => {
+ min = match lit_to_float(lit) {
+ Some(s) => Some(s),
None => error("invalid argument type for `min` of `range` validator: only integers are allowed")
};
- has_min = true;
- }
- "max" => {
- max = match lit_to_float(lit) {
- Some(s) => s,
+ }
+ "max" => {
+ max = match lit_to_float(lit) {
+ Some(s) => Some(s),
None => error("invalid argument type for `max` of `range` validator: only integers are allowed")
};
- has_max = true;
- }
- v => error(&format!(
+ }
+ v => error(&format!(
"unknown argument `{}` for validator `range` (it only has `min`, `max`)",
v
)),
- },
+ }
+ }
_ => panic!("unexpected item {:?} while parsing `range` validator", item),
},
_ => unreachable!(),
}
}
- if !has_min || !has_max {
- error("Validator `range` requires 2 arguments: `min` and `max`");
+ if !min.is_some() && !max.is_some() {
+ error("Validator `range` requires at least 1 argument out of `min` and `max`");
}
let validator = Validator::Range { min, max };