validator refactor progress
This commit is contained in:
83
src/validator/rules/combinators.rs
Normal file
83
src/validator/rules/combinators.rs
Normal file
@ -0,0 +1,83 @@
|
||||
use crate::validator::context::ValidationContext;
|
||||
use crate::validator::error::ValidationError;
|
||||
use crate::validator::result::ValidationResult;
|
||||
|
||||
impl<'a> ValidationContext<'a> {
|
||||
pub(crate) fn validate_combinators(
|
||||
&self,
|
||||
result: &mut ValidationResult,
|
||||
) -> Result<bool, ValidationError> {
|
||||
if let Some(ref all_of) = self.schema.all_of {
|
||||
for sub in all_of {
|
||||
let derived = self.derive_for_schema(sub, true);
|
||||
let res = derived.validate()?;
|
||||
result.merge(res);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref any_of) = self.schema.any_of {
|
||||
let mut valid = false;
|
||||
|
||||
for sub in any_of {
|
||||
let derived = self.derive_for_schema(sub, true);
|
||||
let sub_res = derived.validate()?;
|
||||
if sub_res.is_valid() {
|
||||
valid = true;
|
||||
result.merge(sub_res);
|
||||
}
|
||||
}
|
||||
|
||||
if !valid {
|
||||
result.errors.push(ValidationError {
|
||||
code: "ANY_OF_VIOLATED".to_string(),
|
||||
message: "Matches none of anyOf schemas".to_string(),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref one_of) = self.schema.one_of {
|
||||
let mut valid_count = 0;
|
||||
let mut valid_res = ValidationResult::new();
|
||||
|
||||
for sub in one_of {
|
||||
let derived = self.derive_for_schema(sub, true);
|
||||
let sub_res = derived.validate()?;
|
||||
if sub_res.is_valid() {
|
||||
valid_count += 1;
|
||||
valid_res = sub_res;
|
||||
}
|
||||
}
|
||||
|
||||
if valid_count == 1 {
|
||||
result.merge(valid_res);
|
||||
} else if valid_count == 0 {
|
||||
result.errors.push(ValidationError {
|
||||
code: "ONE_OF_VIOLATED".to_string(),
|
||||
message: "Matches none of oneOf schemas".to_string(),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
} else {
|
||||
result.errors.push(ValidationError {
|
||||
code: "ONE_OF_VIOLATED".to_string(),
|
||||
message: format!("Matches {} of oneOf schemas (expected 1)", valid_count),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref not_schema) = self.schema.not {
|
||||
let derived = self.derive_for_schema(not_schema, true);
|
||||
let sub_res = derived.validate()?;
|
||||
if sub_res.is_valid() {
|
||||
result.errors.push(ValidationError {
|
||||
code: "NOT_VIOLATED".to_string(),
|
||||
message: "Matched 'not' schema".to_string(),
|
||||
path: self.path.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user