validate.lua

Added by iNTERFACEWARE

A template module for testing HL7 message conformance, you will need to extend it to match your requirements

Source Code
local codemap = require 'codemap' 
 
local validate={}
 
local function Add(List, Msg)
   List[#List+1] = Msg
end
 
local function EmptyList()
   local List = {}
   List.add = Add
   return List
end
 
local function RequiredField(Errs, F, Message)
   if F:nodeValue() == '' then 
      Errs:add(Message..' not present.')
      return Errs[#Errs]
   end
end
 
local function PrintSet(Set)
   local R =''
   for K in pairs(Set) do
      R = R..",'"..K.."'"
   end
   R = '['..R:sub(2, #R)..']'
   return R
end
 
local function FieldMatchCode(Errs, Set, F, Description)
   if not Set[F] then
      Errs:add(Description.." had value '"..F:nodeValue()..
         "' instead of acceptable values "..PrintSet(Set))
      return Errs[#Errs]
   end
end
 
local SexSet = codemap.set{'Female', 'Male'}
 
local function CheckPID(PID, Errs)
   RequiredField(Errs, PID[17][1], 'PID 17.1 religion code')
   RequiredField(Errs, PID[3][1][1], 'PID 3.1.1 patient ID')
   FieldMatchCode(Errs, SexSet, PID[8], 'PID.8, patient sex')
end
 
function validate.CheckAdt(Msg)
   local ErrList = EmptyList()   
   CheckPID(Msg.PID, ErrList)
   ErrList.add = nil
   return ErrList
end

return validate
Description
A template module for testing HL7 message conformance, you will need to extend it to match your requirements