Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify HAPI validation rules for phone numbers?

The following dependencies are being used from the maven central repository in this example:

<!-- provides HAPI library -->
<dependency>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-base</artifactId>
  <version>2.2</version>
</dependency>
<!-- provides HAPI library message version -->
<dependency>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-structures-v22</artifactId>
  <version>2.2</version>
</dependency>

<!-- provides ByteString -->
<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-actor_2.10</artifactId>
  <version>2.3.3</version>
</dependency>

Here is an example of my parsing code, written in scala:

  import akka.util.ByteString
  import ca.uhn.hl7v2.model.Message
  import ca.uhn.hl7v2.model.v22.datatype.{CM_PAT_ID, ST, TN, TSComponentOne}
  import ca.uhn.hl7v2.model.v22.segment.{EVN, MRG, PID}
  import ca.uhn.hl7v2.parser.CanonicalModelClassFactory
  import ca.uhn.hl7v2.{DefaultHapiContext, ErrorCode, HL7Exception}

  lazy val parser = {
    val context = new DefaultHapiContext()
    context.setModelClassFactory(new CanonicalModelClassFactory("2.2"))
    context.getGenericParser
  }

  def parseHL7Message(message: ByteString) = Try[Message] { 
    val msg: String = message.utf8String.trim
    parser.parse(msg) 
  }

This code can successfully parse the following HL7 message.

"MSH|^~\\&|XXXX|S|XXXXXX|S|201410280931||ADT^A31|123456|P|2.2\r" +
"EVN|A31|201410280930\r" +
"PID|||9999999^^^S^MR~88888888^^^^PI||xxxx^xxxxxxxxx||11111111||||||(123)456-7890\r" +
"PV1\r"

However, when a phone number with an extension is supplied in the message, the hapi parser fails to parse the message. Here is an example of the input message I am trying to parse with an extension in the phone number:

"MSH|^~\\&|XXXX|S|XXXXXX|S|201410280931||ADT^A31|123456|P|2.2\r" +
"EVN|A31|201410280930\r" +
"PID|||9999999^^^S^MR~88888888^^^^PI||xxxx^xxxxxxxxx||11111111||||||(123)456-7890 1\r" +
"PV1\r"

Trying to parse this message fails with the following error message:

ca.uhn.hl7v2.validation.ValidationException: Validation failed: Primitive value '(123)456-7890 1' requires to be empty or a US phone number at PID-13

I read everything I could find at http://hl7api.sourceforge.net/index.html to look for documentation on how to modify the validation rules but have not found anything useful.

An example would be most appreciated, but even pointing to the proper documentation, or a simple working example project will be sufficient.

How can the validation rules used by the HAPI parser be configured to allow a phone number extension to be included in a valid US phone number in the PID-13 field?

EDIT

With a little more searching, through this hapi developer mailing list thread, I figured out how to disable validation altogether. Here is an example:

  lazy val parser = {
    val context = new DefaultHapiContext()
    context.setModelClassFactory(new CanonicalModelClassFactory("2.2"))
    context.setValidationContext(new NoValidation)
    context.getGenericParser
  }

But if possible, I would like to continue validating the messages. If I have to disable validation I guess that will have to work, but I'd prefer to specify that validation remain turned on, but that phone numbers can include extensions.

like image 527
axiopisty Avatar asked Oct 31 '14 20:10

axiopisty


People also ask

How do I validate data in Hapi?

Validating data can be very helpful in making sure that your application is stable and secure. hapi allows this functionality by using the module Joi, which allows you to create your validations with a simple and clear object syntax. Joi is an object schema description language and validator for JavaScript objects.

How to write validation rules for phone number in below format?

how to write validation rules for phone number in below format When end user entering value for phone number it should only allow mentioned special characters + () - and numeric numbers. IF ( ISBLANK (End_User_Contact_Phone__c) , false, NOT (REGEX (End_User_Contact_Phone__c, "\D*? (\d\D*?) {10}")))

What is HL7 validation in Hapi?

Overview of HL7 Message Validation Mechanisms in HAPI At a very high level, the HAPI library enables us to build three "levels" of message validation functionality. They are basic message validation, custom message validationand validation by the use of conformance profiles.

How to validate a phone number with +91 code?

The number must start with +91 code. The number must begin with 7 or 8 or 9. Create castype called Validation and enter the step as mentioned below, Open the created property Phone number from app explorer .


2 Answers

I must work with 3rd party service, and this service send to me invalid phones. Unfortunatly, I can't understand, how to do it as 'best practice'. But I found one hack:

@PostConstruct
public void postConstruct() {  
    List<RuleBinding<PrimitiveTypeRule>> rules = ((ValidationContextImpl)applicationRouter.getParser().getHapiContext().getValidationContext()).getPrimitiveRuleBindings();
    //initially was published with this line, but think it was mistake
    //for(int i = rules.size() - 1; i > 0; i--) {
    for(int i = rules.size() - 1; i >= 0; i--) {
        RuleBinding<PrimitiveTypeRule> item = rules.get(i);
        if("TN".equals(item.getScope())){
            rules.remove(i);
        }
    }
}

If somebody will know more good way to resolve it, please write.

like image 140
degr Avatar answered Nov 16 '22 01:11

degr


Phone numbers can include extensions. The problem is that you have the extension in the wrong format. See the HL7 Messaging Standard Version 2.2, section 2.8.10.9. Telephone numbers must be in the following format.

[NN] [(999)]999-9999[X99999][B99999][C any text]

Put the extension after the 'X'.

like image 36
Nick Radov Avatar answered Nov 16 '22 03:11

Nick Radov