use crate::error::OpenSamlError;
use std::sync::RwLock;
pub type SchemaValidator = fn(&str) -> Result<(), String>;
static SCHEMA_VALIDATOR: RwLock<Option<SchemaValidator>> = RwLock::new(None);
pub fn set_schema_validator(validator: SchemaValidator) {
if let Ok(mut guard) = SCHEMA_VALIDATOR.write() {
*guard = Some(validator);
}
}
pub fn is_valid_xml(xml: &str) -> Result<(), OpenSamlError> {
crate::xml::dom::parse(xml)?;
if let Ok(guard) = SCHEMA_VALIDATOR.read() {
if let Some(validate) = *guard {
validate(xml).map_err(OpenSamlError::Invalid)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn baseline_rejects_doctype_without_validator() {
assert!(is_valid_xml("<!DOCTYPE x><x/>").is_err());
assert!(is_valid_xml("<x>ok</x>").is_ok());
}
}