92 lines
2.8 KiB
Rust
92 lines
2.8 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::validator::Validator;
|
|
use crate::validator::context::ValidationContext;
|
|
use crate::validator::error::ValidationError;
|
|
use crate::validator::result::ValidationResult;
|
|
use crate::validator::rules::util::equals;
|
|
|
|
impl<'a> ValidationContext<'a> {
|
|
pub(crate) fn validate_core(
|
|
&self,
|
|
result: &mut ValidationResult,
|
|
) -> Result<bool, ValidationError> {
|
|
let current = self.instance;
|
|
|
|
if let Some(ref type_) = self.schema.type_ {
|
|
match type_ {
|
|
crate::database::object::SchemaTypeOrArray::Single(t) => {
|
|
if !Validator::check_type(t, current) {
|
|
result.errors.push(ValidationError {
|
|
code: "INVALID_TYPE".to_string(),
|
|
values: Some(HashMap::from([
|
|
("expected".to_string(), t.to_string()),
|
|
])),
|
|
path: self.path.to_string(),
|
|
});
|
|
}
|
|
}
|
|
crate::database::object::SchemaTypeOrArray::Multiple(types) => {
|
|
let mut valid = false;
|
|
for t in types {
|
|
if Validator::check_type(t, current) {
|
|
valid = true;
|
|
break;
|
|
}
|
|
}
|
|
if !valid {
|
|
result.errors.push(ValidationError {
|
|
code: "INVALID_TYPE".to_string(),
|
|
values: Some(HashMap::from([
|
|
("expected".to_string(), format!("{:?}", types)),
|
|
])),
|
|
path: self.path.to_string(),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(ref const_val) = self.schema.const_ {
|
|
if !equals(current, const_val) {
|
|
result.errors.push(ValidationError {
|
|
code: "CONST_VIOLATED".to_string(),
|
|
values: Some(HashMap::from([
|
|
("expected".to_string(), format!("{:?}", const_val)),
|
|
])),
|
|
path: self.path.to_string(),
|
|
});
|
|
} else if let Some(obj) = current.as_object() {
|
|
result.evaluated_keys.extend(obj.keys().cloned());
|
|
} else if let Some(arr) = current.as_array() {
|
|
result.evaluated_indices.extend(0..arr.len());
|
|
}
|
|
}
|
|
|
|
if let Some(ref enum_vals) = self.schema.enum_ {
|
|
let mut found = false;
|
|
for val in enum_vals {
|
|
if equals(current, val) {
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if !found {
|
|
result.errors.push(ValidationError {
|
|
code: "ENUM_MISMATCH".to_string(),
|
|
values: Some(HashMap::from([
|
|
("expected".to_string(), format!("{:?}", enum_vals)),
|
|
])),
|
|
path: self.path.to_string(),
|
|
});
|
|
} else if let Some(obj) = current.as_object() {
|
|
result.evaluated_keys.extend(obj.keys().cloned());
|
|
} else if let Some(arr) = current.as_array() {
|
|
result.evaluated_indices.extend(0..arr.len());
|
|
}
|
|
}
|
|
|
|
Ok(true)
|
|
}
|
|
}
|