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
use serde_json::Value;
use crate::context::Context;
use crate::error::{ErrorIterator, ValidationError};
use crate::format::FormatChecker;
use crate::resolver::Resolver;
use crate::schemas;
use crate::validators;
use crate::validators::Validator;
pub struct Config<'a> {
schema: &'a Value,
resolver: Resolver<'a>,
draft: &'a dyn schemas::Draft,
}
impl<'a> Config<'a> {
pub fn get_validator(&self, key: &str) -> Option<Validator> {
self.draft.get_validator(key)
}
pub fn get_format_checker(&self, key: &str) -> Option<FormatChecker> {
self.draft.get_format_checker(key)
}
pub fn get_draft_number(&self) -> u8 {
self.draft.get_draft_number()
}
pub fn get_metaschema(&self) -> &Value {
self.draft.get_schema()
}
pub fn get_resolver(&self) -> &Resolver<'a> {
&self.resolver
}
pub fn get_schema(&self) -> &Value {
&self.schema
}
pub fn from_schema(
schema: &'a Value,
draft: Option<&'a dyn schemas::Draft>,
) -> Result<Config<'a>, ValidationError> {
Ok(Config {
schema,
resolver: Resolver::from_schema(schema)?,
draft: draft.unwrap_or_else(|| {
schemas::draft_from_schema(schema).unwrap_or_else(|| &schemas::Draft7)
}),
})
}
pub fn validate(&'a self, instance: &'a Value) -> Result<(), ErrorIterator<'a>> {
crate::validate(self, instance)
}
pub fn validate_schema(&'a self) -> Result<(), ErrorIterator<'a>> {
let mut errors = validators::descend(
self,
self.get_schema(),
self.get_metaschema(),
None,
Context::new_from(self.get_metaschema()),
)
.peekable();
if errors.peek().is_none() {
Ok(())
} else {
Err(Box::new(errors))
}
}
}