valico/json_dsl/validators/
allowed_values.rs1use super::super::errors;
2use serde_json::Value;
3
4pub struct AllowedValues {
5 allowed_values: Vec<Value>,
6}
7
8impl AllowedValues {
9 pub fn new(values: Vec<Value>) -> AllowedValues {
10 AllowedValues {
11 allowed_values: values,
12 }
13 }
14}
15
16impl super::Validator for AllowedValues {
17 fn validate(&self, val: &Value, path: &str) -> super::ValidatorResult {
18 let mut matched = false;
19 for allowed_value in self.allowed_values.iter() {
20 if val == allowed_value {
21 matched = true;
22 }
23 }
24
25 if matched {
26 Ok(())
27 } else {
28 Err(vec![Box::new(errors::WrongValue {
29 path: path.to_string(),
30 detail: Some("Value is not among allowed list".to_string()),
31 })])
32 }
33 }
34}