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
use serde_json::{Value};
use super::super::errors;

pub struct AllowedValues {
    allowed_values: Vec<Value>
}

impl AllowedValues {
    pub fn new(values: Vec<Value>) -> AllowedValues {
        AllowedValues {
            allowed_values: values
        }
    }
}

impl super::Validator for AllowedValues {
    fn validate(&self, val: &Value, path: &str) -> super::ValidatorResult {
        let mut matched = false;
        for allowed_value in self.allowed_values.iter() {
            if val == allowed_value { matched = true; }
        }

        if matched {
            Ok(())
        } else {
            Err(vec![
                Box::new(errors::WrongValue {
                    path: path.to_string(),
                    detail: Some("Value is not among allowed list".to_string())
                })
            ])
        }
    }
}