Validate with regex
In this example, you will edit the Airport OpenAPI Specification so that it validates that the airport code used as a parameter A piece of information passed to a third-party API during a request. is a string of three capital letters. This will help designers provide appropriate parameters without having to specify thousands of airport codes in an enum. |
Some APIs require parameters in a specific format, such as a valid email address. It's not practical to list every possible value in an enum, so another method is needed to make sure the value is acceptable by the API A programming interface that defines how software can be interacted with by other software. before the workflow is published. Regular expressions A seqence of symbols used to identify a particular pattern or piece of text within a larger body of text. Often used to validate input, such as valid URLs or email addresses. can be used to check that a parameter value:
- Is in the correct format, or one of several possible formats
- Contains (or does not contain) a key set of characters
- Meets a length minimum or maximum
- Meets other, more complex constraints
If the parameter does not meet the requirements, the configuration field marks it as invalid.
Note: Because the value of a variable cannot be known ahead of time, the pattern validation cannot be enforced against variables used in configuration fields.
Regular expressions are specified in an OpenAPI Specification using the pattern key.
The complete OpenAPI Specification A standard, language-agnostic description of RESTful APIs that can be read by both humans and machines. Formerly known as Swagger. and icon for this example are available here.
Tip: Want the short version? Check out our OpenAPI Specification quick reference for quick definitions of parameter types, authentication, file handling and Specification Extensions.
Create the regular expression
A regular expression (regex) is a set of characters that create a pattern to be searched for in a text string.
In this example, we need a regular expression to ensure that only three capital letters are used for the airport codes, with no extra letters, numbers or special characters.
The regular expression we need is ^[A-Z]{3}$, which:
- Accepts a capital letter (a character from A to Z) exactly three times.
- Does not accept any characters before or after these three letters.
Note: This does not test that the parameter is an existing code, only that it is the right format to be a valid code.
Create your regular expression
To create your regular expression:
- Create a list of examples of all formats that the API will accept.
- Create a list of examples of formats that are similar to your acceptable examples, but that the API will reject
- Create and test a regular expression that matches your accepted examples, while excluding your rejected examples.
Tip: You can test your regular expressions at https://regex101.com/
For more information on regular expressions, see http://www.regular-expressions.info/quickstart.html
Create the Xtension
Step 1: Build the OpenAPI Specification
Use the Airport-Data OpenAPI Specification you created earlier as a starting point, or download the completed Airport-Data API example here.
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Airport Data"
},
"host": "www.airport-data.com",
"basePath": "/api",
"schemes": [ "https" ],
"produces": [ "application/json" ],
"paths": {
"/ap_info.json": {
"get": {
"summary": "Get airport info",
"description": "Get airport info",
"operationId": "getAirportInfo",
"produces": [ "application/json" ],
"parameters": [
{
"name": "iata",
"in": "query",
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"country": {
"type": "string"
}
}
}
}
}
}
}
}
}
Step 2: Add the regular expression to the parameter
The regular expression is added using the pattern key inside the parameter object you want to validate:
- Add the pattern key with a value of ^[A-Z]{3}$.
- Add the description key with a value of Three-letter code: JFK to give the designer some guidance.
"parameters": [
{
"name": "iata",
"in": "query",
"type": "string",
"description": "Three-letter code: JFK",
"pattern": "^[A-Z]{3}$"
}
]
The OpenAPI Specification
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Airport Data"
},
"host": "www.airport-data.com",
"basePath": "/api",
"schemes": [ "https" ],
"produces": [ "application/json" ],
"paths": {
"/ap_info.json": {
"get": {
"summary": "Get airport info",
"description": "Get airport info",
"operationId": "getAirportInfo",
"produces": [ "application/json" ],
"parameters": [
{
"name": "iata",
"in": "query",
"type": "string",
"description": "Three-letter code: JFK",
"pattern": "^[A-Z]{3}$"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"country": {
"type": "string"
}
}
}
}
}
}
}
}
}
Create the workflow
Step 1: Add the Xtension
Import the OpenAPI Specification you created into Nintex Automation Cloud:
- Open your Nintex Automation Cloud tenancy.
- Click Xtensions in the dashboard to open the Xtensions page.
- Click in the Private connector list.
- Click Choose a file. Navigate to the OpenAPI Specification on your computer.
- Wait for Nintex Automation Cloud to validate the file.
- Click Next.
- The Xtension does not use authorization, so click Next.
- Edit the Name of the Xtension, which becomes the name of the action group in the Workflow designer.
- Edit the Description of the Xtension. This appears in the Private connector list in the Xtensions page.
- Select or upload an icon for the Xtension. This is displayed for each action or event in the Workflow designer.
- Click Publish.
Step 2: Test the validation
Configure the new Xtension to test your validation.
To create the workflow:
- Click Create workflow in your Nintex Automation Cloud tenancy.
- Scroll down the action toolbox to the Airport Data action group.
- Drag the Find airport information action to the canvas and open the configuration.
- Type a three-letter code into the iata field.
- Try adding numbers, extra letters or punctuation.