简体   繁体   中英

Validate HL7 with C# and nHapi for .NET

I'm looking to validate an HL7 2.3 standard message using C# and .NET version of nHapi project:

https://github.com/duaneedwards/nHapi

I've downloaded the dll's and added to my project both NHapi.Base.dll and NHapi.Model.V23.dll.

I know I should use:

NHapi.Base.validation.MessageValidator

But I can't figure out how IValidationContext theContext should be configured in order to check 2.3 version.

In addition, I can't find any appropriate API docs for it.

Can someone assist?

Methods to validate the message are embedded into the parser. The Implementation of specific rules was intentionally left to implementers (to improve the flexibility). What you need to do is to create the new context:

public class CustomContext : DefaultValidationContext //:IValidationContext
{
    //Define the rules and rule Bindings
}

public class Rule1 : IMessageRule
{
    //Check whatever you want in the fully parsed message
    //For example, check for the mandatory segment, groups cardinalities etc.
}

then

PipeParser p = new PipeParser();
CustomContext myContext = new CustomContext();
p.ValidationContext = myContext;

This is a good starting point: NHapi Documentation

Even I was looking for some solution to validate HL7 V2 messages using NHapi and could not find any good articles. So I decided to go through the NHapi object module to see any helpful information to validate the structure and I found something.

The NHapi HL7 v2 IMessage is implemented using IType interface and it has a property called ExtraComponent . NHapi parser does not throw any exceptions on invalid structure but populates the ExtraComponent property. So if you find ExtraComponent.numComponents() to be more than 0 then you have structural issues on the message.

I have written a validator code in C#. You can download it from github.

https://github.com/shivkumarhaldikar/NHapiValidatator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM