
Create a Regular Expression Validator for Microsoft Power Automate
I was inspired by a question from a colleague recently to try my hand at building a custom connector for Microsoft Power Automate that would allow for validating values using regular expressions. The specific scenario was validating an email address when importing records into Dynamics 365 using Power Automate.
Currently, Power Automate doesn’t support Regular Expression validation and validating emails using the ‘contains’ condition may not be sufficient. While the specific scenario was for validating emails it made sense to create a more generic connector to validate any value using a specified regex pattern.
There is a current request for Microsoft to add this ability natively in Power Automate that hasn’t been implemented yet. However, this post also shows how you could create any custom connector that hooks into an Azure function
Creating the Azure Function
The first step was to create an Azure function that could take in a value and a pattern and return a result indicating if the value matched the pattern specified.
To do this go to the Azure portal and click Create a resource from the menu and search for Function App.
Now click Create and give the App a Name and fill out the rest of the details for your App. Leave the Runtime Stack as .NET and Click Create
Once the Function App is created, navigate to the app and select Functions. Then click New Function. Select HTTP trigger as the template for your function and provide a name for your function e.g. RegExValidator. Then select the Authorization level you’d like to use. In this case we’ll make our function anonymous so anyone can call it. Click Create.
Now we need a little bit of .NET code to perform the regular expression validation. Paste the following code into the function code editor.
#r "Newtonsoft.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; public static async TaskRun(HttpRequest req, ILogger log) { string value = req.Query["value"]; string pattern = req.Query["pattern"]; log.LogInformation(pattern); return (System.Text.RegularExpressions.Regex.IsMatch(value, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) ? (ActionResult)new OkObjectResult(new { Result = true}) : (ActionResult)new OkObjectResult(new { Result = false}); }
The code above is simple enough. You’ll notice that our function is taking in two request parameters “value” and “pattern”. The value in our case will be an email address and the pattern will be the regular expression that we use to validate the email address. If the validation is successful we will return a json formatted object with result = true otherwise the result = false.
Testing the Function Using PostMan
To test our function we’ll use PostMan. In the Azure portal click the Get Function URL at the top of the function code editor and copy the URL of the function you just created.
Open PostMan and paste the URL into the Enter request URL box. In order to test the function we’ll need to supply it both a value and a pattern. You can append the following to the url in order to do this.
?value=test@test.test&pattern=%5Cw%2B%28%5B-%2B.%27%5D%5Cw%2B%29%2A%40%5Cw%2B%28%5B-.%5D%5Cw%2B%29%2A%5C.%5Cw%2B%28%5B-.%5D%5Cw%2B%29%2A
NOTE: In order to test our function in PostMan we need to URL encode our pattern. When we eventually call the function from Power Automate we will be able to send over the pattern unencoded.
Click Send and verify that the result returned is “true”. You can modify the value to provide an invalid email address and test that the results is “false” as well.
In PostMan click Save and provide a name for your request. Now click the ellipses next to the collection that contains your saved request and select Export. Select Collection v1 (deprecated) and click Export (Note: As of writing this the Power Automate custom connector Postman import expects a v1 export.)
Creating the Custom Connector
Now that we have our Azure function for validating a value based on a regular expression and our PostMan export we need to create a custom connector in Microsoft Power Automate to make use of this function.
In the Power Automate portal select the cog in the top right and click Custom Connectors.
Click the Create custom connector drop down at the top right and select Import a Postman collection. Provide a name for your Custom connector and import the file that you just exported from PostMan.
Click Continue to open the Custom connector editor. Click the Definition tab and scroll down to the Request section. You’ll see that your query parameters were automatically generated based on the PostMan export you imported. Now scroll down to the Response section and click the default response.
Since PostMan hasn’t saved the response json the custom connector doesn’t know the response format so you will need to tell it the format. Click the Import from sample button. Now head back to PostMan and copy the json response you received in your test and paste that into the body field in the Import from sample window and click import.
You’ll see that your response body changes to recognize the result property that comes back from the Azure function.
At this point you’re ready to test your custom validation connector. Click Create connector and then move over to the Test tab. Click create Connection and note the value and pattern are prefilled with the values you supplied to the PostMan test. Click Test operation and verify you receive a valid result. That’s it! You’re ready to use your new custom validation connector in your Power Automate.
Using the Custom Connector
I won’t go into how to create a Flow to use this connector in this article. However, I simply used a built in template for importing rows from an excel spreadsheet into Dynamics 365 and added my custom connector as a step in that flow to validate each of the emails in the rows that were being imported. The important part of my Flow looked something like this.
A Few Things To Note
One thing to note is that this sample doesn’t implement any security on calling your Azure function so anyone could use your function assuming they know the url. So, you may want to secure your function using Azure AD in a production scenario.
The great thing about this custom connector is it’s reusable for any number of different scenarios, not just validating emails. It could be used for even more complex scenarios like validating a street address for example.
Finally, the regular expression I used in my flow is as follows.
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
However, finding an appropriate regular expression for your situation is as easy as looking on any regex site like http://regexlib.com.