Saturday, March 16, 2019

Email Address Validation Using Dataweave Regex

Introduction

This short article is about how to use regex with Dataweave 2.0 with regard to email validation. Regex is used in Mulesoft language very wide with regard two functions, matchs(...) and match(...). It is very important to master the regular expression in order to be professional in Mulesoft integration projects. The are a lot of reference available one. Here are few:

Use Case

We expect the output of the dataweave transformation as the following depending on the validity of the email address:
[
    {
        ...
        "invalidEmail": "johndo@yahoo"
        ...
    },
    {
        ...
        "PersonEmail": "john.smith@google.com"
        ...
    },
    ...
]

Invalid Emails

The following types of emails are invalid:
  1. beginning with a dot: .gary.liu@google.com
  2. ending with a dot: gary.liu@google.com.
  3. double dots: gary.liu@google..com
  4. domain name contains underscore: gary.liu@att_rr.com
  5. domain name contains space: gayr.liu@att rr.com
  6. domain name contains and of the following: ,<>/[]
  7. no organization email: gary@google

Solution

%dw 2.0
output application/json
var regexEmail = /^[^.][a-zA-Z0-9.!#$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-](?!.*?\.\.)[^_ ; ,<>\/\\]+(?:\.[a-zA-Z0-9-]+)[^.]*$/
---
payload map using (email = $.email) {
 (validEmail: email) if (email matches regexEmail),
 (invalidEmail: email) if ( not  (email matches regexEmail))
}
The above dataweave script is self-explanatory. Few explanation is required if you are not very familiar with regular expression:
  1. negation: [^_;,\.] this expression means if the email domain contains underscore _ , semi-coma, etc. is not valid email
  2. simple ^ and $ represent the beginning and end of the line
  3. [a-zA-Z0-9] mean any charater A a, Bb ... Zz, or 0 to 9 digits are valid
  4. + sign means to match one for more
  5. * sign matches 0 or more
  6. ?! means not include, (?!.*?\.\.) --> not include double dots: ..

Saturday, March 2, 2019

How To Resolve Issue With: "General SSLEngine problem"

The Background

This happens when you enable the HTTPS with your own certificates. In my case, I have configured Anypoint runtime fabrics with self generated certification using the following command:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
The above command generates two files: cert.pem and key.pem. The purpose of them is beyond the scope of the article. The error will occur when the local mule flow call the remote application which is deployed in the Anypoint Runtime Fabrics.

Solution

To resolve this problem, we just need to import the cert.pem to cacerts file. The command is (on MacOs):
cd /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/security
sudo keytool -import -trustcacerts -keystore cacerts -storepass changeit -alias rogers-poc-cert -file /Users/gl17/anypoint/certs/poc/cert.pem
Make sure restart Anypoint Studio.

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...