EDI Validator for validating EDI files

It is essential that valid documents are exchanged between trading partners. Accuracy is one of the benefits that EDI provides over paper document processing and estimates suggest that providing accurate data results in 30% faster delivery time.

EdiFabric allows you to quickly establish if a message is valid or not, and provides you with the exact position and reason of any inaccuracies. To determine this it uses our custom validation attributes to check for mandatory items, too many or too few repetitions, the correct length, data type or EDI code of a data element, etc.

The EDI Validator would first check for structural issues such as missing trailer segments or if the reference number in the header matches that in the trailer. Then it will iterate through all items in the DOM tree and will execute the validation logic for each attribute.

All errors are reported back in the form of a MessageErrorContext with detailed information for the position of the error within the original EDI document, and the reason for the validation failure.

This is when an EDI document was received from a trading partner. Full validation must be applied before processing the message downstream. Should the message is invalid then the MessageErrorContext can be used to report back the list of errors or log it. To extract a user-friendly errors list use the Flatten() method.

var ediStream = File.OpenRead(@"C:\edi.txt");
List<EdiItem> ediItems;
using(var reader = new X12Reader(ediStream, "EdiFabric.Rules.X12004010"))
    ediItems = reader.ReadToEnd().ToList(); 
                                 
var purchaseOrders = ediItems.OfType<TS850>();

foreach (var purchaseOrder in purchaseOrders)
{
    //  Validate
    MessageErrorContext errorContext;
    if (!purchaseOrder.IsValid(out errorContext))
    {
        //  Report it back to the sender, log, etc.
        var errors = errorContext.Flatten();
    }
    else
    {
        //  purchaseOrder is valid, handle it downstream
    }
} 

This is when an EDI document was created internally and is about to be sent to a trading partner. EdiFabric will automatically apply all trailers and therefore they are either not there at all or may contain incorrect data. For this reason validation at this stage must skip all trailer-related validation. To do so use the extra flag for skipping trailer validation.

var ediStream = File.OpenRead(@"C:\edi.txt");
List<EdiItem> ediItems;
using(var reader = new X12Reader(ediStream, "EdiFabric.Rules.X12004010"))
    ediItems = reader.ReadToEnd().ToList(); 
                                 
var purchaseOrders = ediItems.OfType<TS850>();

foreach (var purchaseOrder in purchaseOrders)
{
    //  Validate
    MessageErrorContext errorContext;
    if (!purchaseOrder.IsValid(out errorContext, true))
    {
        //  Report it back to the sender, log, etc.
        var errors = errorContext.Flatten();
    }
    else
    {
        //  purchaseOrder is valid, handle it downstream
    }
}