use edifact::{
EdifactError, Segment, ValidationContext, ValidationLayer, ValidationReport,
ValidationRuleContext, Validator, validate_each,
};
struct SimpleStructureValidator;
impl Validator for SimpleStructureValidator {
fn validate_batch(
&self,
segments: &[Segment<'_>],
report: &mut ValidationReport,
_context: &ValidationRuleContext<'_>,
) {
validate_each(segments, report, |segment| {
if segment.tag == "UNH" && segment.get_element(1).is_none() {
return Err(EdifactError::MissingRequiredElement {
tag: "UNH".to_owned(),
element_index: 1,
});
}
if segment.tag.starts_with('Z') {
return Err(EdifactError::InvalidSegmentForMessage {
tag: segment.tag().to_owned(),
message_type: "GENERIC".to_owned(),
span: segment.span,
});
}
Ok(())
});
}
fn set_message_type(&mut self, _msg_type: Option<&str>) {
}
}
#[test]
fn validator_trait_passes_for_valid_segments() {
let input = b"UNH+1+ORDERS:D:11A:UN'BGM+E03+11042+9'UNT+3+1'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let validator = SimpleStructureValidator;
let mut report = ValidationReport::default();
validator.validate_batch(&segments, &mut report, &ValidationRuleContext::empty());
assert!(!report.has_errors());
}
#[test]
fn validator_trait_fails_for_segments_with_errors() {
let input = b"UNH+INCOMPLETE'ZZZ+X'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let validator = SimpleStructureValidator;
let mut report = ValidationReport::default();
validator.validate_batch(&segments, &mut report, &ValidationRuleContext::empty());
assert!(report.has_errors());
}
#[test]
fn validator_rejects_unknown_segments() {
let input = b"ZZZ+X'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let validator = SimpleStructureValidator;
let mut report = ValidationReport::default();
validator.validate_batch(&segments, &mut report, &ValidationRuleContext::empty());
assert!(
report
.errors()
.iter()
.any(|i| i.message.contains("segment"))
);
}
#[test]
fn context_can_disable_code_list_layer() {
struct MockCodeListValidator;
impl Validator for MockCodeListValidator {
fn validate_batch(
&self,
segments: &[Segment<'_>],
report: &mut ValidationReport,
_context: &ValidationRuleContext<'_>,
) {
validate_each(segments, report, |segment| {
if segment.tag == "BGM" {
return Err(EdifactError::InvalidCodeValue {
tag: "BGM".to_owned(),
element_index: 0,
value: "E3".to_owned(),
code_list: "1001".to_owned(),
span: segment.span,
suggestion: None,
});
}
Ok(())
});
}
fn set_message_type(&mut self, _msg_type: Option<&str>) {}
}
let input = b"BGM+E3+11042+9'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let ctx = ValidationContext::builder()
.with_validator(ValidationLayer::CodeList, MockCodeListValidator)
.code_list(false)
.build();
let report = ctx.validate(&segments);
assert!(report.warnings().is_empty());
}
#[test]
fn validation_context_supports_multiple_validators() {
struct ValidatorA;
struct ValidatorB;
impl Validator for ValidatorA {
fn validate_batch(
&self,
segments: &[Segment<'_>],
report: &mut ValidationReport,
_context: &ValidationRuleContext<'_>,
) {
validate_each(segments, report, |segment| {
if segment.tag.starts_with('Z') {
return Err(EdifactError::InvalidSegmentForMessage {
tag: segment.tag().to_owned(),
message_type: "GENERIC".to_owned(),
span: segment.span,
});
}
Ok(())
});
}
fn set_message_type(&mut self, _msg_type: Option<&str>) {}
}
impl Validator for ValidatorB {
fn validate_batch(
&self,
_segments: &[Segment<'_>],
_report: &mut ValidationReport,
_context: &ValidationRuleContext<'_>,
) {
}
fn set_message_type(&mut self, _msg_type: Option<&str>) {}
}
let input = b"BGM+E03+11042+9'ZZZ+X'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let ctx = ValidationContext::builder()
.with_validator(ValidationLayer::Structure, ValidatorA)
.with_validator(ValidationLayer::CodeList, ValidatorB)
.build();
let report = ctx.validate(&segments);
assert!(report.has_errors());
}
#[test]
fn validation_context_propagates_message_type() {
use std::sync::{Arc, Mutex};
#[derive(Clone)]
struct MessageTypeCapturingValidator(Arc<Mutex<Option<String>>>);
impl Validator for MessageTypeCapturingValidator {
fn validate_batch(
&self,
_segments: &[Segment<'_>],
_report: &mut ValidationReport,
_context: &ValidationRuleContext<'_>,
) {
}
fn set_message_type(&mut self, msg_type: Option<&str>) {
*self.0.lock().unwrap() = msg_type.map(|s| s.to_owned());
}
}
let captured_message_type = Arc::new(Mutex::new(None));
let input = b"UNH+1+ORDERS:D:11A:UN'UNT+2+1'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let ctx = ValidationContext::builder()
.with_message_type("ORDERS")
.with_validator(
ValidationLayer::Structure,
MessageTypeCapturingValidator(captured_message_type.clone()),
)
.build();
let report = ctx.validate(&segments);
assert!(!report.has_errors());
assert_eq!(
*captured_message_type.lock().unwrap(),
Some("ORDERS".to_owned())
);
}
#[test]
fn envelope_rejects_segment_between_unb_and_unh() {
use edifact::{EdifactError, validate_envelope};
let input = b"UNB+UNOB:1+SENDER+RECEIVER+200101:0000+1'BGM+220+1+9'UNH+1+ORDERS:D:96A:UN'UNT+2+1'UNZ+1+1'";
let segments: Vec<_> = edifact::from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("should tokenise");
let result = validate_envelope(&segments);
assert!(
matches!(result, Err(EdifactError::InvalidSegmentForMessage { ref tag, .. }) if tag == "BGM"),
"expected InvalidSegmentForMessage for BGM between UNB and UNH, got: {result:?}"
);
}
mod suppression_rules {
use edifact::{
ComponentRef, DirectoryValidator, ElementRef, Insignificant, Repr, SegmentDefinition,
Status, ValidationContext, ValidationLayer, from_bytes,
};
fn syntax_codes(input: &[u8]) -> Vec<String> {
let segments: Vec<_> = from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("parse");
ValidationContext::builder()
.with_syntax_validation()
.build()
.validate(&segments)
.iter_issues()
.filter_map(|i| i.error_code().map(str::to_owned))
.collect()
}
#[test]
fn a_segment_ending_in_an_empty_element_is_a_trailing_separator() {
assert!(syntax_codes(b"BGM+220+'").contains(&"E051".to_owned()));
assert!(!syntax_codes(b"BGM+220'").contains(&"E051".to_owned()));
}
#[test]
fn a_composite_ending_in_an_empty_component_is_a_trailing_separator() {
assert!(syntax_codes(b"DTM+137:20260101:'").contains(&"E051".to_owned()));
assert!(!syntax_codes(b"DTM+137:20260101'").contains(&"E051".to_owned()));
}
#[test]
fn an_omitted_element_in_the_middle_keeps_its_separator_and_is_not_reported() {
let codes = syntax_codes(b"BGM+220++9'");
assert!(!codes.contains(&"E051".to_owned()), "{codes:?}");
}
#[test]
fn a_single_empty_element_is_an_omitted_element_not_a_trailing_separator() {
let codes = syntax_codes(b"BGM+'");
assert!(!codes.contains(&"E051".to_owned()), "{codes:?}");
}
macro_rules! directory_report {
($input:expr, $repr:expr) => {{
static ELEMENTS: &[ElementRef] = &[$repr];
static SEG: SegmentDefinition = SegmentDefinition::new("ZZZ", "Test", ELEMENTS);
report_against($input, |tag| (tag == "ZZZ").then_some(&SEG))
}};
}
fn report_against(
input: &[u8],
lookup: fn(&str) -> Option<&'static SegmentDefinition>,
) -> Vec<String> {
let segments: Vec<_> = from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let validator =
DirectoryValidator::new("t", lookup, |_, _| true, |_, _| None, |_, _| None, None);
ValidationContext::builder()
.with_validator(ValidationLayer::Structure, validator)
.build()
.validate(&segments)
.iter_issues()
.filter_map(|i| i.error_code().map(str::to_owned))
.collect()
}
#[test]
fn leading_zeroes_in_a_variable_numeric_value_are_reported() {
assert!(
directory_report!(
b"ZZZ+007'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::n_up_to(6))
)
.contains(&"E053".to_owned())
);
assert!(
!directory_report!(
b"ZZZ+7'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::n_up_to(6))
)
.contains(&"E053".to_owned())
);
}
#[test]
fn a_single_zero_before_a_decimal_mark_is_allowed() {
assert!(
!directory_report!(
b"ZZZ+0.5'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::n_up_to(6))
)
.contains(&"E053".to_owned())
);
assert!(
directory_report!(
b"ZZZ+00.5'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::n_up_to(6))
)
.contains(&"E053".to_owned())
);
assert!(
!directory_report!(
b"ZZZ+0'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::n_up_to(6))
)
.contains(&"E053".to_owned())
);
}
#[test]
fn trailing_spaces_in_a_variable_text_value_are_reported() {
assert!(
directory_report!(
b"ZZZ+ACME '",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::an_up_to(10))
)
.contains(&"E053".to_owned())
);
assert!(
!directory_report!(
b"ZZZ+ACME'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::an_up_to(10))
)
.contains(&"E053".to_owned())
);
}
#[test]
fn a_fixed_length_value_is_exempt_from_suppression() {
assert!(
!directory_report!(
b"ZZZ+007'",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::n(3))
)
.contains(&"E053".to_owned())
);
assert!(
!directory_report!(
b"ZZZ+AC '",
ElementRef::new(1, "9999", Status::Mandatory, 1).with_repr(Repr::an(4))
)
.contains(&"E053".to_owned())
);
}
#[test]
fn suppression_findings_are_warnings_not_errors() {
let segments: Vec<_> = from_bytes(b"BGM+220+'")
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let report = ValidationContext::builder()
.with_syntax_validation()
.build()
.validate(&segments);
assert!(!report.has_errors(), "{report:#?}");
assert!(report.has_warnings());
}
#[test]
fn the_insignificant_kind_is_exposed_on_the_error() {
assert_eq!(
Insignificant::LeadingZeroes.to_string(),
"leading zeroes are not suppressed"
);
assert_eq!(
Insignificant::TrailingSpaces.to_string(),
"trailing spaces are not suppressed"
);
}
#[test]
fn a_composite_component_definition_still_drives_the_check() {
static C507: &[ComponentRef] = &[
ComponentRef::new(1, "2005", Status::Mandatory).with_repr(Repr::an_up_to(3)),
ComponentRef::new(2, "2380", Status::Conditional).with_repr(Repr::an_up_to(35)),
];
static ELEMENTS: &[ElementRef] =
&[ElementRef::composite(1, "C507", Status::Mandatory, 1, C507)];
static DTM: SegmentDefinition = SegmentDefinition::new("DTM", "Date/time", ELEMENTS);
let segments: Vec<_> = from_bytes(b"DTM+137:20260101 '")
.collect::<Result<Vec<_>, _>>()
.expect("parse");
let validator = DirectoryValidator::new(
"t",
|tag| (tag == "DTM").then_some(&DTM),
|_, _| true,
|_, _| None,
|_, _| None,
None,
);
let report = ValidationContext::builder()
.with_validator(ValidationLayer::Structure, validator)
.build()
.validate(&segments);
assert!(
report.iter_issues().any(|i| i.error_code() == Some("E053")),
"{report:#?}"
);
}
}
mod issue_model {
use edifact::{
DirectoryValidator, ElementRef, FindingKind, ProfileRulePack, SegmentDefinition, Status,
ValidationContext, ValidationIssue, ValidationLayer, ValidationReport, from_bytes, service,
};
fn parse(input: &[u8]) -> Vec<edifact::Segment<'_>> {
from_bytes(input)
.collect::<Result<Vec<_>, _>>()
.expect("fixture should parse")
}
fn codes(report: &ValidationReport) -> Vec<&str> {
report
.iter_issues()
.filter_map(ValidationIssue::error_code)
.collect()
}
fn by_code<'r>(report: &'r ValidationReport, code: &str) -> &'r ValidationIssue {
report
.iter_issues()
.find(|i| i.error_code() == Some(code))
.unwrap_or_else(|| panic!("no {code} in {report:#?}"))
}
fn assert_classified(report: ValidationReport) -> ValidationReport {
assert!(report.has_any_issues(), "fixture produced no issues");
for issue in report.iter_issues() {
assert!(issue.layer().is_some(), "no layer: {issue:#?}");
assert!(issue.kind().is_some(), "no kind: {issue:#?}");
}
report
}
#[test]
fn seventy_thousand_segment_occurrence_is_exact() {
let mut input = String::from("UNH+1+MSCONS:D:04B:UN'BGM+7+1'LIN+1'");
for _ in 0..69_999 {
input.push_str("QTY+220:1'");
}
input.push_str("QTY+999:1'UNT+70004+1'");
let segments = parse(input.as_bytes());
assert_eq!(segments.len(), 70_004);
let pack = ProfileRulePack::new("MSCONS-DEMO").require_qualifier(
"QTY",
0,
0,
"220",
"MSCONS-QTY-CODE",
);
let report = ValidationContext::builder()
.with_profile_pack(pack)
.build()
.validate(&segments);
assert_eq!(report.total_issues(), 1, "{report:#?}");
let issue = &report.errors()[0];
assert_eq!(issue.segment_occurrence(), Some(69_999));
assert_eq!(issue.segment_position(), Some(70_003));
assert_eq!(
segments[70_002].get_element(0).unwrap().get_component(0),
Some("999")
);
assert_eq!(issue.element_index(), Some(0));
assert_eq!(issue.component_index(), Some(0));
assert_eq!(issue.kind(), Some(FindingKind::Code));
assert_eq!(issue.layer(), Some(ValidationLayer::Profile));
assert_eq!(issue.pack(), Some("MSCONS-DEMO"));
}
#[test]
fn every_builtin_issue_has_layer_and_kind() {
let envelope = |input: &[u8]| {
let report = assert_classified(
ValidationContext::builder()
.with_envelope_validation()
.build()
.validate(&parse(input)),
);
for issue in report.iter_issues() {
assert_eq!(issue.layer(), Some(ValidationLayer::Envelope), "{issue:#?}");
}
report
};
let report = envelope(
b"UNB+UNOC:3+S+R+200101:0900+IC1'UNH+1+ORDERS:D:96A:UN'BGM+220'UNT+5+2'UNZ+2+IC2'",
);
for code in ["E004", "E005", "E016"] {
assert!(codes(&report).contains(&code), "{code}: {report:#?}");
}
assert_eq!(
by_code(&report, "E004").kind(),
Some(FindingKind::ControlMismatch)
);
let report = envelope(b"UNB+UNOC:3+S+R+200101:0900+IC1'UNZ+0+IC1'");
assert_eq!(by_code(&report, "E042").kind(), Some(FindingKind::Envelope));
let report =
envelope(b"UNB+UNOC:3+S+R+200101:0900+IC1'UNH+1+ORDERS:D:96A:UN'UNT+2+1'UNZ+1+IC1'");
assert_eq!(by_code(&report, "E043").kind(), Some(FindingKind::Envelope));
let report = envelope(
b"UNB+UNOC:3+S+R+200101:0900+IC1'UNG+ORDERS+S+R+200101:0900+G1+UN+D:96A'\
UNH+1+ORDERS:D:96A:UN'BGM+220'UNT+3+1'UNE+1+G1'\
UNH+2+ORDERS:D:96A:UN'BGM+220'UNT+3+2'UNZ+2+IC1'",
);
assert_eq!(by_code(&report, "E052").kind(), Some(FindingKind::Envelope));
let report = assert_classified(
ValidationContext::builder()
.with_syntax_validation()
.build()
.validate(&parse(
b"UNH+1+ORDERS:D:96A:UN'FTX+ 'DTM'NAD+BY+'UNT+5+1'",
)),
);
for (code, kind, position) in [
("E045", FindingKind::Representation, 2),
("E046", FindingKind::Syntax, 3),
("E051", FindingKind::Syntax, 4),
] {
let issue = by_code(&report, code);
assert_eq!(issue.layer(), Some(ValidationLayer::Syntax), "{code}");
assert_eq!(issue.kind(), Some(kind), "{code}");
assert_eq!(issue.segment_position(), Some(position), "{code}");
}
let report = assert_classified(
ValidationContext::builder()
.with_charset_validation()
.build()
.validate(&parse(
b"UNB+UNOA:3+S+R+200101:0900+1'NAD+BY+Acme Ltd'UNZ+0+1'",
)),
);
let issue = by_code(&report, "E038");
assert_eq!(issue.layer(), Some(ValidationLayer::Charset));
assert_eq!(issue.kind(), Some(FindingKind::Charset));
assert_eq!(issue.segment_position(), None);
static DTM_ELEMENTS: &[ElementRef] = &[
ElementRef::new(1, "C507", Status::Mandatory, 1),
ElementRef::new(2, "2380", Status::Conditional, 1),
];
static NAD_ELEMENTS: &[ElementRef] = &[
ElementRef::new(1, "3035", Status::Mandatory, 1),
ElementRef::new(2, "C082", Status::Mandatory, 1),
];
static DTM_DEF: SegmentDefinition =
SegmentDefinition::new("DTM", "Date/time/period", DTM_ELEMENTS);
static NAD_DEF: SegmentDefinition =
SegmentDefinition::new("NAD", "Name and address", NAD_ELEMENTS);
let directory = DirectoryValidator::new(
"TEST",
|tag| match tag {
"DTM" => Some(&DTM_DEF),
"NAD" => Some(&NAD_DEF),
_ => None,
},
|de, code| !(de == "3035" && code == "XX"),
|_, _| None,
|tag, element| (tag == "DTM" && element == 0).then_some(3),
None,
)
.with_required_segments(|_| &["UNH", "BGM", "DTM", "FTX", "UNT"]);
let segments =
parse(b"UNH+1+ORDERS:D:96A:UN'NAD+XX'DTM+137:20240101:102:9'BGM+220'UNT+5+1'");
let report = assert_classified(
ValidationContext::builder()
.with_validator(ValidationLayer::Structure, directory)
.build()
.validate(&segments),
);
let code_list = by_code(&report, "E014");
assert_eq!(code_list.layer(), Some(ValidationLayer::CodeList));
assert_eq!(code_list.kind(), Some(FindingKind::Code));
assert_eq!(code_list.segment_position(), Some(2));
let missing_element = by_code(&report, "E008");
assert_eq!(missing_element.layer(), Some(ValidationLayer::Structure));
assert_eq!(missing_element.kind(), Some(FindingKind::Missing));
let component_count = by_code(&report, "E013");
assert_eq!(component_count.layer(), Some(ValidationLayer::Structure));
assert_eq!(component_count.kind(), Some(FindingKind::Count));
assert_eq!(component_count.segment_position(), Some(3));
let missing_segment = report
.iter_issues()
.find(|i| i.error_code().is_none() && i.segment_tag() == Some("FTX"))
.expect("missing FTX finding");
assert_eq!(missing_segment.layer(), Some(ValidationLayer::Structure));
assert_eq!(missing_segment.kind(), Some(FindingKind::Missing));
let out_of_order = report
.iter_issues()
.find(|i| i.kind() == Some(FindingKind::Placement) && i.error_code().is_none())
.unwrap_or_else(|| panic!("out-of-order finding: {report:#?}"));
assert_eq!(out_of_order.segment_tag(), Some("DTM"));
assert_eq!(out_of_order.error_code(), None, "kind only, no code");
assert_eq!(out_of_order.segment_position(), Some(3));
let service_directory = DirectoryValidator::new(
"iso-9735-service",
service::lookup,
|_, _| true,
|_, _| None,
|_, _| None,
None,
);
let report = assert_classified(
ValidationContext::builder()
.with_validator(ValidationLayer::Structure, service_directory)
.build()
.validate(&parse(b"UNZ+abc+IC1234567890123'")),
);
assert_eq!(
by_code(&report, "E048").kind(),
Some(FindingKind::Representation)
);
assert_eq!(
by_code(&report, "E049").kind(),
Some(FindingKind::Representation)
);
assert_eq!(
by_code(&report, "E048").layer(),
Some(ValidationLayer::Structure)
);
let pack = ProfileRulePack::new("PROFILE")
.require_segment("FTX", "P-FTX-M")
.forbid_segment("DTM", "P-DTM-X")
.require_qualifier("BGM", 0, 0, "380", "P-BGM-CODE");
let report = assert_classified(
ValidationContext::builder()
.with_profile_pack(pack)
.build()
.validate(&parse(
b"UNH+1+ORDERS:D:96A:UN'BGM+220'DTM+137:20240101:102'UNT+4+1'",
)),
);
for (rule, kind, position) in [
("P-FTX-M", FindingKind::Missing, None),
("P-DTM-X", FindingKind::NotPermitted, Some(3)),
("P-BGM-CODE", FindingKind::Code, Some(2)),
] {
let issue = report.issues_for_rule_id(rule).next().expect(rule);
assert_eq!(issue.layer(), Some(ValidationLayer::Profile), "{rule}");
assert_eq!(issue.kind(), Some(kind), "{rule}");
assert_eq!(issue.pack(), Some("PROFILE"), "{rule}");
assert_eq!(issue.segment_position(), position, "{rule}");
}
}
#[test]
fn fork_exclusion_notice_has_kind_other_and_no_layer() {
struct NotForkable;
impl edifact::Validator for NotForkable {
fn validate_batch(
&self,
_segments: &[edifact::Segment<'_>],
_report: &mut ValidationReport,
_context: &edifact::ValidationRuleContext<'_>,
) {
}
}
let report = ValidationContext::builder()
.with_validator(ValidationLayer::Structure, NotForkable)
.build()
.fork_with_message_ref("1")
.validate(&parse(b"UNH+1+ORDERS:D:96A:UN'UNT+2+1'"));
let notice = &report.infos()[0];
assert_eq!(notice.rule_id(), Some("edifact::fork::excluded-validator"));
assert_eq!(notice.kind(), Some(FindingKind::Other));
assert_eq!(notice.layer(), None, "static issues are not stamped");
}
#[cfg(feature = "serde")]
#[test]
fn reads_legacy_issue_json() {
let json = include_str!("fixtures/issue_v0_17.json");
let issue: ValidationIssue = serde_json::from_str(json).expect("legacy JSON must load");
assert_eq!(issue.error_code(), Some("E014"));
assert_eq!(issue.element_index(), Some(1));
assert_eq!(issue.segment_occurrence(), Some(3));
assert_eq!(issue.component_index(), None);
assert_eq!(issue.segment_position(), None);
assert_eq!(issue.group_path(), None);
assert_eq!(issue.layer(), None);
assert_eq!(issue.kind(), None);
assert_eq!(issue.pack(), None);
}
}
mod envelope_dedupe {
use edifact::{
ComponentRef, DirectoryValidator, ElementRef, SegmentDefinition, Status, ValidationContext,
ValidationLayer, from_bytes, service,
};
const C002: &[ComponentRef] = &[
ComponentRef::new(1, "1001", Status::Mandatory),
ComponentRef::new(2, "1000", Status::Conditional),
];
const BGM_ELEMENTS: &[ElementRef] =
&[ElementRef::composite(1, "C002", Status::Mandatory, 1, C002)];
static BGM: SegmentDefinition =
SegmentDefinition::new("BGM", "Beginning of message", BGM_ELEMENTS);
fn lookup(tag: &str) -> Option<&'static SegmentDefinition> {
if tag == "BGM" {
Some(&BGM)
} else {
service::lookup(tag)
}
}
fn directory() -> DirectoryValidator {
DirectoryValidator::new(
"orders",
lookup,
|_, _| true,
|_, _| None,
|_, _| None,
None,
)
}
fn issues(input: &[u8]) -> Vec<(String, Option<String>)> {
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>().expect("parses");
ValidationContext::builder()
.with_envelope_validation()
.with_validator(ValidationLayer::Structure, directory())
.build()
.validate(&segments)
.errors()
.iter()
.map(|i| {
(
i.error_code().unwrap_or_default().to_owned(),
i.segment_tag().map(str::to_owned),
)
})
.collect()
}
#[test]
fn a_spanned_finding_both_layers_make_is_reported_once() {
let found = issues(
b"UNB+UNOC:3+S+R+260101:0900+1'UNH+ABCDEFGHIJKLMNO+ORDERS:D:96A:UN'BGM+220'UNT+3+ABCDEFGHIJKLMNO'UNZ+1+1'",
);
let e049: Vec<_> = found.iter().filter(|(code, _)| code == "E049").collect();
assert_eq!(e049.len(), 2, "one on UNH, one on UNT: {found:?}");
assert!(e049.iter().any(|(_, tag)| tag.as_deref() == Some("UNH")));
assert!(e049.iter().any(|(_, tag)| tag.as_deref() == Some("UNT")));
}
#[test]
fn a_v3_date_of_nine_digits_is_one_e049() {
let found = issues(
b"UNB+UNOC:3+S+R+202601011:0900+1'UNH+1+ORDERS:D:96A:UN'BGM+220'UNT+3+1'UNZ+1+1'",
);
assert_eq!(found, [("E049".to_owned(), Some("UNB".to_owned()))]);
}
fn located(input: &[u8]) -> Vec<(String, Option<String>, Option<u32>)> {
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>().expect("parses");
ValidationContext::builder()
.with_envelope_validation()
.with_validator(ValidationLayer::Structure, directory())
.build()
.validate(&segments)
.errors()
.iter()
.map(|i| {
(
i.error_code().unwrap_or_default().to_owned(),
i.segment_tag().map(str::to_owned),
i.segment_position(),
)
})
.collect()
}
#[test]
fn a_spanless_finding_both_layers_make_is_reported_once() {
let found = located(
b"UNB+UNOC:3+S+R+260101:0900+1'UNH+1+ORDERS:D:96A'BGM+220'UNT+3+1'\
UNH+2+ORDERS:D:96A'BGM+220'UNT+3+2'UNZ+2+1'",
);
let e021: Vec<_> = found.iter().filter(|(code, ..)| code == "E021").collect();
let unh = ("E021".to_owned(), Some("UNH".to_owned()), Some(1));
assert_eq!(e021, [&unh, &unh], "{found:?}");
let found =
located(b"UNB+UNOC:3+S++260101:0900+1'UNH+1+ORDERS:D:96A:UN'BGM+220'UNT+3+1'UNZ+1+1'");
let e008: Vec<_> = found.iter().filter(|(code, ..)| code == "E008").collect();
assert_eq!(
e008,
[&("E008".to_owned(), Some("UNB".to_owned()), None)],
"{found:?}"
);
}
#[test]
fn spanless_findings_are_never_dropped() {
let found =
issues(b"UNB+UNOC:3+S+R+260101:0900+1'UNH+1+ORDERS:D:96A'BGM+:X'UNT+3+1'UNZ+1+1'");
assert!(
found.contains(&("E021".to_owned(), Some("UNH".to_owned()))),
"{found:?}"
);
assert!(
found.contains(&("E021".to_owned(), Some("BGM".to_owned()))),
"{found:?}"
);
}
}
#[test]
fn positions_count_from_each_messages_unh() {
use edifact::{DirectoryValidator, from_bytes, service};
let input = b"UNB+UNOC:3+S+R+260101:0900+1'UNH+1+ORDERS:D:96A:UN'BGM+220'UNT+3+1'\
UNH+2+ORDERS:D:96A:UN'BGM+220'UNT+3+2'UNZ+2+1'";
let segments: Vec<_> = from_bytes(input).collect::<Result<_, _>>().expect("parses");
for envelope in [false, true] {
let mut builder = ValidationContext::builder();
if envelope {
builder = builder.with_envelope_validation();
}
let directory = DirectoryValidator::new(
"orders",
service::lookup,
|_, _| true,
|_, _| None,
|_, _| None,
None,
);
let report = builder
.with_validator(ValidationLayer::Structure, directory)
.build()
.validate(&segments);
let bgm: Vec<_> = report
.errors()
.iter()
.filter(|i| i.error_code() == Some("E011") && i.segment_tag() == Some("BGM"))
.map(|i| i.segment_position())
.collect();
assert_eq!(bgm, [Some(2), Some(2)], "envelope layer {envelope}");
}
}