Sanity check EDI data

Verified
Added by iNTERFACEWARE

This code checks EDI (XML) data for some common errors, you can use this approach for pre-processing messages to clean them up

Source Code
-- use the path to where you saved the file "270BlueCrossEdited.edi"
local MY_FILE = '/Users/<some-user>/Downloads/temp/270BlueCrossEdited.edi'

function trace (a,b,c,d) return end

function EDIformatSanity(d)
   
   local function ISAL()
      if d:find('GS') - #'ISA' - 1 < 103 then          return false       end       return true    end        local function chunkedEqually()             local a = {}       a = d:split('r')                    if #a > 1 then 
         if #a[1] == #a[2] and #a[1] == #a[#a-1] then
            return true      
         end
         return false
      end
      
      return false
   end
   
   local errid, reason = {}, {}
   
   if not ISAL() then
      errid[#errid+1], reason[#errid+1] = 1, 'ISA segment too short.'
   end
   
   if chunkedEqually() then
      errid[#errid+1], reason[#errid+1] = 2, 'Message is in equal length lines.'
   end
   
   return errid, reason
end

function consolidateMessage()
   --TBD
end

function returnNack()
   --TBD
end

function main(Data)  
   
   local function readMyFile(fn)
      local f = assert(io.open(fn, 'rb'))
      local d = f:read('*a')
      f:close()      
      return d
   end
   
   local Data = readMyFile(MY_FILE)
   
   -- Sanity test ISA segment
   -- Sanity test general appearance of Data
   
   local errid, reason = EDIformatSanity(Data)
   
   if #errid > 0 then
      
      for i = 1, #errid do
         
         if errid[i] == 1 then -- example of error which cannot be recovered from
            iguana.logDebug(reason[i])
            trace(reason[i])
            returnNack()  -- return Nack ? 
         end
         
         if errid[i] == 2 then -- example of error which we can recover from
            iguana.logDebug(reason[i])
            trace(reason[i])
            consolidateMessage(Data) -- fix Data
         end 
         
      end
   end     
end
Description
This code checks EDI (XML) data for some common errors, you can use this approach for pre-processing messages to clean them up
Usage Details

Before processing EDI content, we need to validate whether data is in Standard format and suitable for processing. This code performs some common tests.

How to use the code:

  • Download the 270BlueCrossEdited.edi file
    Note: You will need to remove the “.txt” extension
  • Modify the MY_FILE variable (at the start of the code) to point to where you saved 270BlueCrossEdited.edi
  • Paste the code into a Filter or To Translator script
  • Examine the code and annotations to see how it works