Sanity check EDI data

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