Easily add validation rules to the NHapi parser

NHapi has the functionality to add custom validation rules that the parser. How to configure NHapi to do this isn't that obvious. See a previous post on how to implement custom validation rules. The NHapiTools offer two Context classes that will make adding validation rules much simpler. The first one is the AutomatedContext.

The AutomatedContext is a parser context that will automatically add any custom rule implementatiosn that are found within a namespace. In other words: you can just implement the custom rules that you need and the AutomatedContext will do the rest for you.

 

To do this the NHapiTools offer three interfaces for validation rules: ISpecificEncodingRule, ISpecificMessageRule and the ISpecificPrimitiveRule. These interfaces implement the NHapi interfaces and add a few properties that are used to determing if the implemented rule has to be applied to the message that is currently parsed. In other words, let's say you need a rule that applies to all ADT messages ofHL7 version 2.3 and 2.4. You can just implement the following rule.

public class MessageRule : ISpecificMessageRule
{
    public string[] GetVersions()
    {
        return new string[] {"2.3", "2.4"};
    }
        
    public string[] GetMessageTypes()
    {
        return new string[] { "ADT" };
    }

    public string[] GetTriggerEvents()
    {
        return new string[] { "*" };
    }

    public NHapi.Base.validation.ValidationException[] test(NHapi.Base.Model.IMessage msg)
    {
        // Add validation logic
        return new NHapi.Base.validation.ValidationException[0];
    }

    public string Description
    {
        get { return string.Empty; }
    }

    public string SectionReference
    {
        get { return string.Empty; }
    }
}

Now you have to configure and add the context to the parser. To add the parser use the following code.

PipeParser parser = new PipeParser();
// The default context is reused by this context if you
// don't want to add the default rules you can use the default constructor
AutomatedContext context = new AutomatedContext(parser.ValidationContext);
parser.ValidationContext = context;

Now the AutomatedContext is being used by the parser. The following configuration is needed to tell the AutomatedContext where to look for custom rule implementations.

<appSettings>
	<add key="NHapiRulesNamespace" value="TestApp, version=1.0.0.0" />
</appSettings>

 And you're done!

Add comment


Security code
Refresh

Share this post
FaceBook  Twitter  

Archives

Latest comments