mx20022_validate/
typed.rs1use mx20022_model::common::validate::{
17 ConstraintKind, ConstraintViolation, IsoMessage, Validatable,
18};
19
20use crate::error::{Severity, ValidationError, ValidationResult};
21
22pub fn validate_constraints<T: Validatable>(msg: &T, base_path: &str) -> ValidationResult {
27 let mut violations = Vec::new();
28 msg.validate_constraints(base_path, &mut violations);
29 ValidationResult::new(violations.into_iter().map(violation_to_error).collect())
30}
31
32pub fn validate_message<T: IsoMessage>(msg: &T) -> ValidationResult {
37 validate_constraints(msg, msg.root_path())
38}
39
40fn violation_to_error(v: ConstraintViolation) -> ValidationError {
42 ValidationError::new(
43 v.path,
44 Severity::Error,
45 constraint_rule_id(v.kind),
46 v.message,
47 )
48}
49
50fn constraint_rule_id(kind: ConstraintKind) -> &'static str {
52 match kind {
53 ConstraintKind::MinLength => "XSD_MIN_LENGTH",
54 ConstraintKind::MaxLength => "XSD_MAX_LENGTH",
55 ConstraintKind::Pattern => "XSD_PATTERN",
56 ConstraintKind::MinInclusive => "XSD_MIN_INCLUSIVE",
57 ConstraintKind::MaxInclusive => "XSD_MAX_INCLUSIVE",
58 ConstraintKind::TotalDigits => "XSD_TOTAL_DIGITS",
59 ConstraintKind::FractionDigits => "XSD_FRACTION_DIGITS",
60 }
61}