Documentation

How to validate X12 data elements

Article author
Admin
  • Updated

X12 data elements

All data elements in the EDI templates are represented as System.String. This allows the underlying type to be conveniently discarded during parsing or generation, and to practically read/write any text data, regardless.

Being type-agnostic, EDI Tools for .NET is able to easily recover corrupt files, broken data, or incorrectly set encoding. Data and type validation is pushed to a follow-up operation such as calling the IsValid() method on the POCO.

 

DataElement attribute and EdiCodes attribute

EDI Tools for .NET validate the type of data elements using the type configured on the DataElement attribute.

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

EDI Tools for .NET supports the following data element types for the X12 standard:

  • X12_N - for numeric values
  • X12_R - for decimal values
  • X12_AN - for alphanumeric values
  • X12_DT - for date values
  • X12_TM- for time values
  • X12_ID - for code sets

If the data type is annotated with EdiCodesAttribute, the data element value will be validated against the list of specified EDI codes.

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

EDI code type X12_ID_1006 is defined as:

[EdiCodes(",1,10,11,2,5,6,7,8,9,")]
public class X12_ID_1006
{
}

EdiCodes supports partition codes, as defined by the SEF format. 

 

List of X12 data types

Numeric - N, N0, N1, N2, N3, N4, N5, N6

Data element can include digits only.

Numeric data with implied decimal. If the decimal part is included, n shows the number of digits to the right of the implied decimal.

Leading zeros are suppressed unless needed to satisfy the minimum length of the element. If the value is negative, include a minus sign, which does not count toward the length.)

N and N0 are equivalent (it is not necessary to include the zero). This means the implied decimal is at the end of the number.

N1 means there is one digit to the right of the implied decimal. Example: The element contains the value -123, which is to be interpreted as -12.3.

N2 means there are two digits to the right of the implied decimal. Example: The element contains the value -123, which is to be interpreted as -1.23.

The DOM C# class representing numeric X12 data type is:

 [Serializable()]
 public class X12_N0
 {
 }

 

Decimal - R

Data element can include decimal point and digits only. Decimal point is optional for integers.

The number after R shows the maximum number of digits to the right of the decimal. R0 means there should be no digits to the right of the decimal.

Example: The element contains the value 150.25 and the type is R. The value being represented is also 150.25.

Example: The element contains the value 150.23 but the type is R1. The value being represented would be 150.2

Signs and decimal points do not count toward length.

The DOM C# class representing decimal X12 data type is:

 [Serializable()]
 public class X12_R
 {
 }

 

Alphanumeric - AN

Data element can include any letters, digits, special characters, and control characters.

X12 notation combines the type and length as in the following examples:

an5 exactly 5 alphanumeric characters

an..5 up to 5 alphanumeric characters.

The DOM C# class representing alphanumeric X12 data type is:

 [Serializable()]
 public class X12_AN
 {
 }

 

Date - DT

Date, in YYMMDD or CCYYMMDD format. CC is century.

The DOM C# class representing date X12 data type is:

 [Serializable()]
 public class X12_DT
 {
 }

 

Time - TM

Time, in 24-hour clock time as follows: HHMM or HHMMSS.

The DOM C# class representing time X12 data type is:

 [Serializable()]
 public class X12_TM
 {
 }

 

How to validate DTP segments

Date, in CCYYMMDD or CCYYMMDD-CCYYMMDD-CCYYMMDD format. CC is century.

When a DTP segment is encountered the validator will validate all data elements with code 1251 only if there is a preceding data element with code 1250 and value D8 or RD8.

 

How to validate X12 data types

By default, calling IsValid() only validates numeric, DTP (see above), date, and time data types. Alphanumeric data types are not validated. Type validation is global, e.g. all data elements marked with the same type are validated in exactly the same way.

To validate alphanumeric X12 data elements, a SyntaxSet must be configured in the ValidationSettings.

The following syntax set implementations are available:

 

Basic

Valid characters are:

ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!&()*+,-./:;?= '""

 MessageErrorContext result;
 var validationResult = msg.IsValid(out result, 
new ValidationSettings { SyntaxSet = new Basic()});

 

Extended

Valid characters are:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!&()*+,-./:;?= '""%@[]_{}\|<>~#$

 MessageErrorContext result;
 var validationResult = msg.IsValid(out result, 
new ValidationSettings { SyntaxSet = new Extended()});

 

Custom characters

Use CustomSyntax to specify a custom set of chars for all AN data types.

 MessageErrorContext result;
 var validationResult = msg.IsValid(out result, 
new ValidationSettings {
SyntaxSet = new CustomSyntax(new Basic().GetValidChars() + "ß")});

 

Regex

Use RegexSyntax to specify a custom regex for all AN data types.

 MessageErrorContext result;
 var validationResult = msg.IsValid(out result, 
new ValidationSettings {
SyntaxSet = new RegexSyntax(new Regex("[a-zA-Z0-9 ]"))});

Examples in GitHub:

 

How to override syntax set validation

Sometimes a particular data element needs to be validated slightly differently than the syntax set specified in the ValidationSettings for its type. This can be achieved by overriding the syntax set with a regex expression directly in the DataElement attribute:

[DataElement("956", typeof(X12_AN), @"^[A-Z0-9 a-z,./%]*$")]
[Pos(5)]
public string TaxJurisdictionCode_05 { get; set; }

The overridden data element will always be validated against the regex specified in the DataElement attribute, ignoring the syntax set specified in the ValidationSettings.

 

How to validate ISA and GS

To validate the control segments ISA and GS call the Validate() method of the ContolSegment class. The X12 control segments are defined in the  EdiFabric.Core.Model.Edi.X12 namespace.

It is also possible to override the default validation for ISA and GS by deriving from the ISA and GS classes. The derived classes can define whatever data element attributes are needed and to return the derived control segments whilst reading from an X12 file, the X12ReaderBase constructor must be used, instead of X12Reader.

Examples in GitHub:

It is also possible to configure the ValidationSettings and apply custom EDI codes when only the EDI codes need to be validated differently.

For example, to validate the sender ID ISA06 data element with allowed EDI codes SENDER1 and SENDER2, you can do the following:

var codeSetMap = new Dictionary<string, List<string>>();
codeSetMap.Add("X12_ID_I06", new List<string> { "SENDER1", "SENDER2" });

isa.Validate( new ValidationSettings { DataElementCodesMap = codeSetMap });.

The available ISA and GS EDI codes that can be used for this kind of validation are:

    • ISA01 - X12_ID_I01
    • ISA02 - X12_ID_I02
    • ISA03 - X12_ID_I03
    • ISA04 - X12_ID_I04
    • ISA05 - X12_ID_I05
    • ISA06 - X12_ID_I06
    • ISA07 - X12_ID_I05
    • ISA08 - X12_ID_I07
    • ISA11 - X12_ID_I65
    • ISA12 - X12_ID_I11
    • ISA15 - X12_ID_I14
    • ISA16 - X12_ID_I15
    • GS01 - X12_ID_479
    • GS02 - X12_ID_G02
    • GS03 - X12_ID_G03
    • GS07 - X12_ID_G07
    • GS08 - X12_ID_G08
Share this:

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.