43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
use crate::validator::context::ValidationContext;
|
|
use crate::validator::error::ValidationError;
|
|
use crate::validator::result::ValidationResult;
|
|
|
|
impl<'a> ValidationContext<'a> {
|
|
pub(crate) fn validate_format(
|
|
&self,
|
|
result: &mut ValidationResult,
|
|
) -> Result<bool, ValidationError> {
|
|
let current = self.instance;
|
|
if let Some(ref compiled_fmt) = self.schema.compiled_format {
|
|
match compiled_fmt {
|
|
crate::database::schema::CompiledFormat::Func(f) => {
|
|
let should = if let Some(s) = current.as_str() {
|
|
!s.is_empty()
|
|
} else {
|
|
true
|
|
};
|
|
if should && let Err(e) = f(current) {
|
|
result.errors.push(ValidationError {
|
|
code: "FORMAT_MISMATCH".to_string(),
|
|
message: format!("Format error: {}", e),
|
|
path: self.path.to_string(),
|
|
});
|
|
}
|
|
}
|
|
crate::database::schema::CompiledFormat::Regex(re) => {
|
|
if let Some(s) = current.as_str()
|
|
&& !re.is_match(s)
|
|
{
|
|
result.errors.push(ValidationError {
|
|
code: "FORMAT_MISMATCH".to_string(),
|
|
message: "Format regex mismatch".to_string(),
|
|
path: self.path.to_string(),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(true)
|
|
}
|
|
}
|