use crate::error::SamlError;
use crate::xml::XmlLimits;
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<(), SamlError> {
is_valid_xml_with_limits(xml, XmlLimits::default())
}
pub fn is_valid_xml_with_limits(xml: &str, limits: XmlLimits) -> Result<(), SamlError> {
crate::xml::dom::parse_with_limits(xml, limits)?;
if let Ok(guard) = SCHEMA_VALIDATOR.read() {
if let Some(validate) = *guard {
validate(xml).map_err(SamlError::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());
}
}