EDI Cross-Segment and Custom Validation

There are two options to extend the existing validation - by inheriting from either ValidationAttribute or IEdiValidator. The inbuilt validation process will always invoke the validation of any attribute inheriting from ValidationAttribute whilst iterating through the items of EdiMessage. It will then invoke any custom validation for any item implementing IEdiValidator.

Add any custom validation logic in the Validate method. The InstanceContext class contains the current instance, its PropertyInfo, and its parent InstanceContext. All of the indexes are relative to the current instance.

 

 

 


 

 

 

Create a custom validation attribute for simple data elements

 public class ExtraValidation : ValidationAttribute
 {
    public ExtraValidation(int priority) : base(priority)
    {
    }
    public override List<SegmentErrorContext> ValidateEdi(ValidationContext context)
    {
        // Custom validation goes here
    }
 }

 

 

 


 

 

 

Implement IEdiValidator for any EDI item (loop, segment or complex element)

 public partial class Loop_2000A : IEdiValidator
 {
    public List<SegmentErrorContext> ValidateEdi(ValidationContext validationContext)
    {
        // Custom validation goes here, example below
        var result = new List<SegmentErrorContext>();
        
        if (N1 != null && N2 == null)
            result.Add(new SegmentErrorContext("N2", 
                validationContext.SegmentIndex + 2, GetType().GetTypeInfo(), 
                SegmentErrorCode.RequiredSegmentMissing,
                "N2 segment is missing."));

        return result;
    }
 }
Was this article helpful?
4 out of 4 found this helpful