validate.lua

Verified
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
Usage Details

The conformance module is designed to help with HL7 conformance processing. It contains the function validate.CheckAdt() that checks the PID segment for required fields, and checks that correct codes are used. This is a template module with sample checks, you will need to extend it to match your requirements.

It is intended for use when creating an ACK in the script of a From LLP component.

How to use conformance.lua:

  • Add the module to a From LLP component script
  • Write code to generate a Custom ACK
  • Use validate.CheckAdt() to check the message structure
  • Write any errors returned to NTE segments in the ACK
  • Send the ACK using ack.send()

See HL7 Conformance for more information, here is the sample code for main:

local validate = require 'validate'
 
-- this module follows the best practice of using local functions
-- this requires main() function to be placed at the end after those functions
 
local function GenerateNACK(Msg, Errs)
   local N = hl7.message{vmd='conformance_ack.vmd', name='Acknowledgement'}  
   N.MSH[7] = os.date('%Y%m%d%H%M%S')
   N.MSH[3][1] = Msg.MSH[5][1]
   N.MSH[4][1] = Msg.MSH[6][1]
   N.MSH[5][1] = Msg.MSH[3][1]
   N.MSH[6][1] = Msg.MSH[4][1]
   N.MSH[11][1] = 'P'
   N.MSH[9][1] = 'ACK'
 
   N.MSA[2] = Msg.MSH[10]
   trace(N)
   
 
   for i = 1, #Errs do
      N.NTE[i][3][1] = Errs[i]
   end
   return N:S()
end
 
local function ProcessADT(Msg)
   local Errs = validate.CheckAdt(Msg) 
   if (#Errs > 0) then
      return GenerateNACK(Msg, Errs)   
   end
end
 
function main(Data)
   local Msg = hl7.parse{vmd='example/demo.vmd', data=Data}
   
   local R = ProcessADT(Msg)
 
   ack.send(R)
end