1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use params::{Map, Value};
pub fn validate_required_if(values: &Map,
field: &[&str],
other: &[&str],
condition: &Value)
-> Result<Option<Value>, String> {
match values.find(other) {
Some(value) if *value == *condition => {
match values.find(field) {
Some(&Value::String(ref value)) if value.is_empty() => {
Err(format!("The {} field is required.",
field.last()
.unwrap()
.to_lowercase()
.replace("_", " ")))
}
Some(&Value::Array(ref value)) if value.is_empty() => {
Err(format!("The {} field is required.",
field.last()
.unwrap()
.to_lowercase()
.replace("_", " ")))
}
Some(&Value::Map(ref value)) if value.is_empty() => {
Err(format!("The {} field is required.",
field.last()
.unwrap()
.to_lowercase()
.replace("_", " ")))
}
Some(&Value::Null) |
None => {
Err(format!("The {} field is required.",
field.last()
.unwrap()
.to_lowercase()
.replace("_", " ")))
}
_ => Ok(None),
}
}
_ => Ok(None),
}
}