use eure_document::parse::{DocumentParser, ParseContext};
use crate::{SchemaNodeId, TypeReference};
use super::SchemaValidator;
use super::context::ValidationContext;
use super::error::{ValidationError, ValidatorError};
pub struct ReferenceValidator<'a, 'doc, 's> {
pub ctx: &'a ValidationContext<'doc>,
pub type_ref: &'s TypeReference,
pub schema_node_id: SchemaNodeId,
}
impl<'a, 'doc, 's> DocumentParser<'doc> for ReferenceValidator<'a, 'doc, 's> {
type Output = ();
type Error = ValidatorError;
fn parse(&mut self, parse_ctx: &ParseContext<'doc>) -> Result<(), ValidatorError> {
let node_id = parse_ctx.node_id();
if let Some(namespace) = &self.type_ref.namespace {
self.ctx
.record_error(ValidationError::UndefinedTypeReference {
name: format!("{}.{}", namespace, self.type_ref.name),
path: self.ctx.path(),
node_id,
schema_node_id: self.schema_node_id,
});
return Ok(());
}
if let Some(&resolved_id) = self.ctx.schema.types.get(&self.type_ref.name) {
let child_validator = SchemaValidator {
ctx: self.ctx,
schema_node_id: resolved_id,
};
parse_ctx.parse_with(child_validator)
} else {
self.ctx
.record_error(ValidationError::UndefinedTypeReference {
name: self.type_ref.name.to_string(),
path: self.ctx.path(),
node_id,
schema_node_id: self.schema_node_id,
});
Ok(())
}
}
}