valico/json_dsl/validators/
rejected_values.rs

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

use super::super::errors;

pub struct RejectedValues {
    rejected_values: Vec<Value>
}

impl RejectedValues {
    pub fn new(values: Vec<Value>) -> RejectedValues {
        RejectedValues {
            rejected_values: values
        }
    }
}

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

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