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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
mod builder;
mod coercers;
pub mod errors;
mod param;
#[macro_use]
pub mod validators;

use super::json_schema;

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

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

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

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

pub fn object() -> Box<dyn 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,
            state: json_schema::ValidationState::new(),
        }
    }

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

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

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