use mx20022_model::common::validate::{
ConstraintKind, ConstraintViolation, IsoMessage, Validatable,
};
use crate::error::{Severity, ValidationError, ValidationResult};
pub fn validate_constraints<T: Validatable>(msg: &T, base_path: &str) -> ValidationResult {
let mut violations = Vec::new();
msg.validate_constraints(base_path, &mut violations);
ValidationResult::new(violations.into_iter().map(violation_to_error).collect())
}
pub fn validate_message<T: IsoMessage>(msg: &T) -> ValidationResult {
validate_constraints(msg, msg.root_path())
}
fn violation_to_error(v: ConstraintViolation) -> ValidationError {
ValidationError::new(
v.path,
Severity::Error,
constraint_rule_id(v.kind),
v.message,
)
}
fn constraint_rule_id(kind: ConstraintKind) -> &'static str {
match kind {
ConstraintKind::MinLength => "XSD_MIN_LENGTH",
ConstraintKind::MaxLength => "XSD_MAX_LENGTH",
ConstraintKind::Pattern => "XSD_PATTERN",
ConstraintKind::MinInclusive => "XSD_MIN_INCLUSIVE",
ConstraintKind::MaxInclusive => "XSD_MAX_INCLUSIVE",
ConstraintKind::TotalDigits => "XSD_TOTAL_DIGITS",
ConstraintKind::FractionDigits => "XSD_FRACTION_DIGITS",
}
}