Repository: xlate/staedi (java)

Issue:
Elements of improperly sequenced elements are not reset
If a segment appears out of sequence in a loop and the segment has already appeared earlier in the loop, the elements of the additional occurrence are not reset before validation. This results in element occurrence errors in addition to the (correct) segment occurrence error for the improperly sequenced segment.

Pull request:
Reset improperly positioned segment's elements in validation
Fixes #120 

- Verified in existing test case by allowing ELEMENT_OCCURRENCE_ERROR
through filter.

Patch:
diff --git a/src/main/java/io/xlate/edi/internal/stream/validation/Validator.java b/src/main/java/io/xlate/edi/internal/stream/validation/Validator.java
index 2814798..c7bd684 100644
--- a/src/main/java/io/xlate/edi/internal/stream/validation/Validator.java
+++ b/src/main/java/io/xlate/edi/internal/stream/validation/Validator.java
@@ -638,6 +638,7 @@ public class Validator {
                 handler.segmentError(next.getId(), next.getLink(), SEGMENT_NOT_IN_PROPER_SEQUENCE);
 
                 next.incrementUsage();
+                next.resetChildren();
 
                 if (next.exceedsMaximumUsage(SEGMENT_VERSION)) {
                     handler.segmentError(next.getId(), next.getLink(), SEGMENT_EXCEEDS_MAXIMUM_USE);
@@ -928,6 +929,7 @@ public class Validator {
         this.composite = this.element;
         this.element = null;
         this.composite.incrementUsage();
+        // resetChildren?
 
         if (this.composite.exceedsMaximumUsage(version)) {
             elementErrors.add(new UsageError(this.composite, TOO_MANY_REPETITIONS));
@@ -943,6 +945,7 @@ public class Validator {
 
         if (implSegmentSelected) {
             this.implComposite.incrementUsage();
+            // resetChildren?
         }
 
         return elementErrors.isEmpty();


Test patch:
diff --git a/src/test/java/io/xlate/edi/internal/stream/SegmentValidationTest.java b/src/test/java/io/xlate/edi/internal/stream/SegmentValidationTest.java
index 192a287..fd3b940 100644
--- a/src/test/java/io/xlate/edi/internal/stream/SegmentValidationTest.java
+++ b/src/test/java/io/xlate/edi/internal/stream/SegmentValidationTest.java
@@ -46,6 +46,7 @@ class SegmentValidationTest {
             switch (reader.getEventType()) {
             case START_TRANSACTION:
             case SEGMENT_ERROR:
+            case ELEMENT_OCCURRENCE_ERROR:
                 return true;
             default:
                 return false;