-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.rs
64 lines (58 loc) · 1.91 KB
/
builder.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
use std::{collections::HashMap, sync::Arc};
use jsonlike::Json;
use crate::{
compiler,
drafts::{draft_from_schema, Draft},
format::FormatFactory,
resolver::DefaultResolver,
vocabulary::KeywordFactory,
BuildResult, ReferenceResolver, Validator,
};
pub async fn validator_for<J: Json>(schema: &J) -> BuildResult<Validator<J>> {
let draft = draft_from_schema(schema);
ValidatorBuilder::default().draft(draft).build(schema).await
}
pub struct ValidatorBuilder<'a, J: Json> {
pub(crate) draft: Draft,
pub(crate) resolver: Arc<dyn ReferenceResolver<J>>,
pub(crate) formats: HashMap<String, Arc<dyn FormatFactory<'a, J>>>,
pub(crate) keywords: HashMap<String, Arc<dyn KeywordFactory<'a, J>>>,
}
impl<'a, J: Json> Default for ValidatorBuilder<'a, J> {
fn default() -> Self {
ValidatorBuilder {
draft: Draft::latest(),
resolver: Arc::new(DefaultResolver),
formats: HashMap::default(),
keywords: HashMap::default(),
}
}
}
impl<'a, J: Json> ValidatorBuilder<'a, J> {
pub async fn build(&self, schema: &J) -> BuildResult<Validator<J>> {
// TODO: Resolve references
compiler::compile::<J>(schema, self.draft)
}
pub fn draft(&mut self, draft: Draft) -> &mut ValidatorBuilder<'a, J> {
self.draft = draft;
self
}
pub fn resolver(&mut self, resolver: impl ReferenceResolver<J> + 'static) -> &mut Self {
self.resolver = Arc::new(resolver);
self
}
pub fn format<F>(&mut self, name: impl Into<String>, factory: F) -> &mut Self
where
F: FormatFactory<'a, J>,
{
self.formats.insert(name.into(), Arc::new(factory));
self
}
pub fn keyword<F>(&mut self, name: impl Into<String>, factory: F) -> &mut Self
where
F: KeywordFactory<'a, J>,
{
self.keywords.insert(name.into(), Arc::new(factory));
self
}
}