Skip to main content

valico/json_dsl/
mod.rs

1mod builder;
2mod coercers;
3pub mod errors;
4mod param;
5#[macro_use]
6pub mod validators;
7
8use super::json_schema;
9
10pub use self::builder::Builder;
11pub use self::coercers::{
12    ArrayCoercer, BooleanCoercer, Coercer, F64Coercer, I64Coercer, NullCoercer, ObjectCoercer,
13    PrimitiveType, StringCoercer, U64Coercer,
14};
15pub use self::param::Param;
16
17pub fn i64() -> Box<dyn coercers::Coercer + Send + Sync> {
18    Box::new(coercers::I64Coercer)
19}
20pub fn u64() -> Box<dyn coercers::Coercer + Send + Sync> {
21    Box::new(coercers::U64Coercer)
22}
23pub fn f64() -> Box<dyn coercers::Coercer + Send + Sync> {
24    Box::new(coercers::F64Coercer)
25}
26pub fn string() -> Box<dyn coercers::Coercer + Send + Sync> {
27    Box::new(coercers::StringCoercer)
28}
29pub fn boolean() -> Box<dyn coercers::Coercer + Send + Sync> {
30    Box::new(coercers::BooleanCoercer)
31}
32pub fn null() -> Box<dyn coercers::Coercer + Send + Sync> {
33    Box::new(coercers::NullCoercer)
34}
35pub fn array() -> Box<dyn coercers::Coercer + Send + Sync> {
36    Box::new(coercers::ArrayCoercer::new())
37}
38pub fn array_of(
39    coercer: Box<dyn coercers::Coercer + Send + Sync>,
40) -> Box<dyn coercers::Coercer + Send + Sync> {
41    Box::new(coercers::ArrayCoercer::of_type(coercer))
42}
43
44pub fn encoded_array(separator: &str) -> Box<dyn coercers::Coercer + Send + Sync> {
45    Box::new(coercers::ArrayCoercer::encoded(separator.to_string()))
46}
47
48pub fn encoded_array_of(
49    separator: &str,
50    coercer: Box<dyn coercers::Coercer + Send + Sync>,
51) -> Box<dyn coercers::Coercer + Send + Sync> {
52    Box::new(coercers::ArrayCoercer::encoded_of(
53        separator.to_string(),
54        coercer,
55    ))
56}
57
58pub fn object() -> Box<dyn coercers::Coercer + Send + Sync> {
59    Box::new(coercers::ObjectCoercer)
60}
61
62pub struct ExtendedResult<T> {
63    value: T,
64    state: json_schema::ValidationState,
65}
66
67impl<T> ExtendedResult<T> {
68    pub fn new(value: T) -> ExtendedResult<T> {
69        ExtendedResult {
70            value,
71            state: json_schema::ValidationState::new(),
72        }
73    }
74
75    pub fn with_errors(value: T, errors: super::ValicoErrors) -> ExtendedResult<T> {
76        ExtendedResult {
77            value,
78            state: json_schema::ValidationState {
79                errors,
80                missing: vec![],
81                replacement: None,
82                evaluated: Default::default(),
83            },
84        }
85    }
86
87    pub fn is_valid(&self) -> bool {
88        self.state.is_valid()
89    }
90
91    pub fn append(&mut self, second: json_schema::ValidationState) {
92        self.state.append(second);
93    }
94}