valico/json_dsl/validators/
at_least_one_of.rs1use serde_json::Value;
2
3use super::super::errors;
4
5pub struct AtLeastOneOf {
6 params: Vec<String>,
7}
8
9impl AtLeastOneOf {
10 pub fn new(params: &[&str]) -> AtLeastOneOf {
11 AtLeastOneOf {
12 params: params.iter().map(|s| (*s).to_string()).collect(),
13 }
14 }
15}
16
17impl super::Validator for AtLeastOneOf {
18 fn validate(&self, val: &Value, path: &str) -> super::ValidatorResult {
19 let object = strict_process!(val.as_object(), path, "The value must be an object");
20
21 let mut matched = vec![];
22 for param in self.params.iter() {
23 if object.contains_key(param) {
24 matched.push(param.clone());
25 }
26 }
27
28 let len = matched.len();
29 if len >= 1 {
30 Ok(())
31 } else {
32 Err(vec![Box::new(errors::AtLeastOne {
33 path: path.to_string(),
34 detail: Some("At least one must be present".to_string()),
35 params: self.params.clone(),
36 })])
37 }
38 }
39}