bluejay_validator/definition/
validator.rs1use crate::definition::{BuiltinRules, Rule};
2use bluejay_core::definition::{SchemaDefinition, TypeDefinitionReference};
3
4pub struct Validator<'a, S: SchemaDefinition, R: Rule<'a, S>> {
5 schema_definition: &'a S,
6 rule: R,
7}
8
9pub type BuiltinRulesValidator<'a, S> = Validator<'a, S, BuiltinRules<'a, S>>;
10
11impl<'a, S: SchemaDefinition, R: Rule<'a, S>> Validator<'a, S, R> {
12 fn new(schema_definition: &'a S) -> Self {
13 Self {
14 schema_definition,
15 rule: Rule::new(schema_definition),
16 }
17 }
18
19 fn visit(&mut self) {
20 self.schema_definition.type_definitions().for_each(
21 |type_definition| match type_definition {
22 TypeDefinitionReference::InputObject(iotd) => {
23 self.visit_input_object_type_definition(iotd)
24 }
25 TypeDefinitionReference::Enum(etd) => self.visit_enum_type_definition(etd),
26 _ => {}
27 },
28 )
29 }
30
31 fn visit_input_object_type_definition(
32 &mut self,
33 input_object_type_definition: &'a S::InputObjectTypeDefinition,
34 ) {
35 self.rule
36 .visit_input_object_type_definition(input_object_type_definition);
37 }
38
39 fn visit_enum_type_definition(&mut self, enum_type_definition: &'a S::EnumTypeDefinition) {
40 self.rule.visit_enum_type_definition(enum_type_definition);
41 }
42
43 pub fn validate(schema_definition: &'a S) -> <Self as IntoIterator>::IntoIter {
44 let mut instance = Self::new(schema_definition);
45 instance.visit();
46 instance.into_iter()
47 }
48}
49
50impl<'a, S: SchemaDefinition, R: Rule<'a, S>> IntoIterator for Validator<'a, S, R> {
51 type Item = R::Error;
52 type IntoIter = <R as IntoIterator>::IntoIter;
53
54 fn into_iter(self) -> Self::IntoIter {
55 self.rule.into_iter()
56 }
57}