valico/json_dsl/
mod.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mod builder;
mod coercers;
mod param;
pub mod errors;
#[macro_use] pub mod validators;

use super::json_schema;

pub use self::param::Param;
pub use self::builder::Builder;
pub use self::coercers::{
    PrimitiveType,
    Coercer,
    StringCoercer,
    I64Coercer,
    U64Coercer,
    F64Coercer,
    BooleanCoercer,
    NullCoercer,
    ArrayCoercer,
    ObjectCoercer,
};

pub fn i64() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::I64Coercer) }
pub fn u64() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::U64Coercer) }
pub fn f64() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::F64Coercer) }
pub fn string() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::StringCoercer) }
pub fn boolean() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::BooleanCoercer) }
pub fn null() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::NullCoercer) }
pub fn array() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::ArrayCoercer::new()) }
pub fn array_of(coercer: Box<coercers::Coercer + Send + Sync>) -> Box<coercers::Coercer + Send + Sync> {
    Box::new(coercers::ArrayCoercer::of_type(coercer))
}

pub fn encoded_array(separator: &str) -> Box<coercers::Coercer + Send + Sync> {
    Box::new(coercers::ArrayCoercer::encoded(separator.to_string()))
}

pub fn encoded_array_of(separator: &str, coercer: Box<coercers::Coercer + Send + Sync>) -> Box<coercers::Coercer + Send + Sync> {
    Box::new(coercers::ArrayCoercer::encoded_of(separator.to_string(), coercer))
}

pub fn object() -> Box<coercers::Coercer + Send + Sync> { Box::new(coercers::ObjectCoercer) }

pub struct ExtendedResult<T> {
    value: T,
    state: json_schema::ValidationState
}

impl<T> ExtendedResult<T> {
    pub fn new(value: T) -> ExtendedResult<T> {
        ExtendedResult {
            value: value,
            state: json_schema::ValidationState::new()
        }
    }

    pub fn with_errors(value: T, errors: super::ValicoErrors) -> ExtendedResult<T> {
        ExtendedResult {
            value: value,
            state: json_schema::ValidationState {
                errors: errors,
                missing: vec![]
            }
        }
    }

    pub fn is_valid(&self) -> bool {
        self.state.is_valid()
    }

    pub fn append(&mut self, second: json_schema::ValidationState) {
        self.state.append(second);
    }
}