mod error;
#[cfg(feature = "purchase-order")]
pub mod purchase_order;
mod xml;
pub use error::DataValidationError;
use xml::{validate_xml, Schema};
pub fn validate_order_xml_3_4(
data: &str,
is_path: bool,
schema_dir: &str,
) -> Result<(), DataValidationError> {
validate_xml(data, is_path, Schema::OrderXmlV3_4, schema_dir)?;
Ok(())
}
pub fn validate_gdsn_3_1(
data: &str,
is_path: bool,
schema_dir: &str,
) -> Result<(), DataValidationError> {
validate_xml(data, is_path, Schema::GdsnXmlV3_1, schema_dir)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Read;
use std::path::PathBuf;
use crate::error::InvalidArgumentError;
#[test]
fn test_validate_gdsn_3_1() {
let mut test_gdsn_xml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_gdsn_xml_path.push("src/data_validation/test_files/gdsn_product.xml");
let path_str = test_gdsn_xml_path.to_str().unwrap();
let mut data = String::new();
std::fs::File::open(path_str)
.unwrap()
.read_to_string(&mut data)
.unwrap();
let mut schema_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
schema_dir.push("src/data_validation/xml/xsd/product");
let schema_dir = schema_dir
.into_os_string()
.into_string()
.expect("Unable to convert product schema dir to string");
let result = validate_gdsn_3_1(&data, false, &schema_dir);
assert!(result.is_ok());
}
#[test]
fn test_validate_gdsn_3_1_path() {
let mut test_gdsn_xml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_gdsn_xml_path.push("src/data_validation/test_files/gdsn_product.xml");
let path_str = test_gdsn_xml_path.to_str().unwrap();
let mut schema_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
schema_dir.push("src/data_validation/xml/xsd/product");
let schema_dir = schema_dir
.into_os_string()
.into_string()
.expect("Unable to convert product schema dir to string");
let result = validate_gdsn_3_1(path_str, true, &schema_dir);
assert!(result.is_ok());
}
#[test]
fn test_validate_gdsn_3_1_invalid() {
let mut test_gdsn_xml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_gdsn_xml_path.push("src/data_validation/test_files/gdsn_product_invalid.xml");
let path_str = test_gdsn_xml_path.to_str().unwrap();
let mut data = String::new();
std::fs::File::open(path_str)
.unwrap()
.read_to_string(&mut data)
.unwrap();
let mut schema_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
schema_dir.push("src/data_validation/xml/xsd/product");
let schema_dir = schema_dir
.into_os_string()
.into_string()
.expect("Unable to convert product schema dir to string");
let result = validate_gdsn_3_1(&data, false, &schema_dir);
assert!(result.is_err());
let expected_error = InvalidArgumentError::new(data, "file fails to validate".to_string());
assert_eq!(result.unwrap_err().to_string(), expected_error.to_string());
}
#[test]
fn test_validate_gdsn_3_1_path_invalid() {
let mut test_gdsn_xml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_gdsn_xml_path.push("src/data_validation/test_files/gdsn_product_invalid.xml");
let path_str = test_gdsn_xml_path.to_str().unwrap();
let mut schema_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
schema_dir.push("src/data_validation/xml/xsd/product");
let schema_dir = schema_dir
.into_os_string()
.into_string()
.expect("Unable to convert product schema dir to string");
let result = validate_gdsn_3_1(path_str, true, &schema_dir);
assert!(result.is_err());
let expected_error =
InvalidArgumentError::new(path_str.to_string(), "file fails to validate".to_string());
assert_eq!(result.unwrap_err().to_string(), expected_error.to_string());
}
}