Validate 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.

Please refer to the SDK for full usage and samples.

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 po in purchaseOrders)
{
    //  Validate
    MessageErrorContext errorContext;
    if (!po.IsValid(out errorContext))
    {
        //  Report it back to the sender, log, etc.
        var errors = errorContext.Flatten();
    }
    else
    {
        //  po is valid, handle it downstream
    }
} 
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 po in purchaseOrders)
{
    //  Validate
    MessageErrorContext errorContext;
    if (!po.IsValid(out errorContext, true))
    {
        //  Report it back to the sender, log, etc.
        var errors = errorContext.Flatten();
    }
    else
    {
        //  po is valid, handle it downstream
    }
} 

The following attributes are used to enable validation:

All mandatory items are annotated with RequiredAttribute

[Required]
[Pos(2)]
public BIG BIG { get; set; }

This attribute can be applied to any property.

To control the number of repetitions annotate lists with ListCountAttribute

[ListCount(100)]
[Pos(3)]
public List<NTE> NTE { get; set; }

The first parameter is the upper limit of how many items are allowed in the list.

You can also set the minimum limit if needed by using the constructor with two parameters. The first one is the minimum and the second one is the maximum.

This attribute should only be applied to properties of type List otherwise it will be discarded.

To control the length of data elements annotate them with StringLengthAttribute

[StringLength(1, 10)]
[Pos(1)]
public string NumberofIncludedSegments_01 { get; set; }

The first parameter is the lower limit of the string length.

The second parameter is the upper limit of the string length.

This attribute should only be applied to properties of type string otherwise it will be discarded.

To set the data type of data elements annotate them with DataElementAttribute

[DataElement("96", typeof(X12_AN))]
[Pos(1)]
public string NumberofIncludedSegments_01 { get; set; }

The first parameter is the EDI identifier of the data element.

The second parameter is the type of the data element.

This attribute should only be applied to properties of type string otherwise it will be discarded. If the referred type is annotated with EdiCodesAttribute, the data element value will be validated against the list of allowed EDI codes.

Combine the attributes

[Required]
[StringLength(1, 10)]
[DataElement("96", typeof(X12_AN))]
[Pos(1)]
public string NumberofIncludedSegments_01 { get; set; }

Combine attributes to apply multi validation to items.