valico/json_dsl/validators/
mod.rs

1use serde_json::{Value};
2use std::fmt;
3
4use common::error;
5
6pub use self::allowed_values::{AllowedValues};
7pub use self::at_least_one_of::{AtLeastOneOf};
8pub use self::exactly_one_of::{ExactlyOneOf};
9pub use self::mutually_exclusive::{MutuallyExclusive};
10pub use self::rejected_values::{RejectedValues};
11
12macro_rules! strict_process {
13    ($val:expr, $path:ident, $err:expr) => {{
14        let maybe_val = $val;
15        if maybe_val.is_none() {
16            return Err(vec![
17                Box::new($crate::json_dsl::errors::WrongType {
18                    path: $path.to_string(),
19                    detail: $err.to_string()
20                })
21            ])
22        }
23
24        maybe_val.unwrap()
25    }}
26}
27
28mod allowed_values;
29mod at_least_one_of;
30mod exactly_one_of;
31mod mutually_exclusive;
32mod regex;
33mod rejected_values;
34
35pub type ValidatorResult = Result<(), error::ValicoErrors>;
36
37pub trait Validator {
38    fn validate(&self, item: &Value, &str) -> ValidatorResult;
39}
40
41impl fmt::Debug for Validator + 'static {
42    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
43        fmt.write_str("[validator]")
44    }
45}
46
47pub type BoxedValidator = Box<Validator + 'static + Send + Sync>;
48pub type Validators = Vec<BoxedValidator>;
49
50impl<T> Validator for T where T: Fn(&Value, &str) -> ValidatorResult {
51    fn validate(&self, val: &Value, path: &str) -> ValidatorResult {
52        self(val, path)
53    }
54}