validator reorg
This commit is contained in:
118
src/validator/context.rs
Normal file
118
src/validator/context.rs
Normal file
@ -0,0 +1,118 @@
|
||||
use crate::validator::schema::Schema;
|
||||
use crate::validator::Validator;
|
||||
use crate::validator::error::ValidationError;
|
||||
use crate::validator::instance::ValidationInstance;
|
||||
use crate::validator::result::ValidationResult;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub struct ValidationContext<'a, I: ValidationInstance<'a>> {
|
||||
pub validator: &'a Validator,
|
||||
pub root: &'a Schema,
|
||||
pub schema: &'a Schema,
|
||||
pub instance: I,
|
||||
pub path: String,
|
||||
pub depth: usize,
|
||||
pub scope: Vec<String>,
|
||||
pub overrides: HashSet<String>,
|
||||
pub extensible: bool,
|
||||
pub reporter: bool,
|
||||
}
|
||||
|
||||
impl<'a, I: ValidationInstance<'a>> ValidationContext<'a, I> {
|
||||
pub fn new(
|
||||
validator: &'a Validator,
|
||||
root: &'a Schema,
|
||||
schema: &'a Schema,
|
||||
instance: I,
|
||||
scope: Vec<String>,
|
||||
overrides: HashSet<String>,
|
||||
extensible: bool,
|
||||
reporter: bool,
|
||||
) -> Self {
|
||||
let effective_extensible = schema.extensible.unwrap_or(extensible);
|
||||
Self {
|
||||
validator,
|
||||
root,
|
||||
schema,
|
||||
instance,
|
||||
path: String::new(),
|
||||
depth: 0,
|
||||
scope,
|
||||
overrides,
|
||||
extensible: effective_extensible,
|
||||
reporter,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn derive(
|
||||
&self,
|
||||
schema: &'a Schema,
|
||||
instance: I,
|
||||
path: &str,
|
||||
scope: Vec<String>,
|
||||
overrides: HashSet<String>,
|
||||
extensible: bool,
|
||||
reporter: bool,
|
||||
) -> Self {
|
||||
let effective_extensible = schema.extensible.unwrap_or(extensible);
|
||||
|
||||
Self {
|
||||
validator: self.validator,
|
||||
root: self.root,
|
||||
schema,
|
||||
instance,
|
||||
path: path.to_string(),
|
||||
depth: self.depth + 1,
|
||||
scope,
|
||||
overrides,
|
||||
extensible: effective_extensible,
|
||||
reporter,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn derive_for_schema(&self, schema: &'a Schema, reporter: bool) -> Self {
|
||||
self.derive(
|
||||
schema,
|
||||
self.instance,
|
||||
&self.path,
|
||||
self.scope.clone(),
|
||||
HashSet::new(),
|
||||
self.extensible,
|
||||
reporter,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> Result<ValidationResult, ValidationError> {
|
||||
let mut effective_scope = self.scope.clone();
|
||||
|
||||
if let Some(id) = &self.schema.obj.id {
|
||||
let current_base = self.scope.last().map(|s| s.as_str()).unwrap_or("");
|
||||
let mut new_base = id.clone().to_string();
|
||||
if !current_base.is_empty() {
|
||||
if let Ok(base_url) = url::Url::parse(current_base) {
|
||||
if let Ok(joined) = base_url.join(id) {
|
||||
new_base = joined.to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
effective_scope.push(new_base);
|
||||
|
||||
let shadow = ValidationContext {
|
||||
validator: self.validator,
|
||||
root: self.root,
|
||||
schema: self.schema,
|
||||
instance: self.instance,
|
||||
path: self.path.clone(),
|
||||
depth: self.depth,
|
||||
scope: effective_scope,
|
||||
overrides: self.overrides.clone(),
|
||||
extensible: self.extensible,
|
||||
reporter: self.reporter,
|
||||
};
|
||||
return shadow.validate_scoped();
|
||||
}
|
||||
|
||||
self.validate_scoped()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user