Validating Input Values in a Data Request
Validators are added to individual input mappings in a query. Input validation allows data services to validate the input parameters in a request and stop the execution of the request if the input doesn’t meet the required criteria. The ESB profile of WSO2 Enterprise Integrator (WSO2 EI) provides a set of built-in validators for some of the most common use cases. It also provides an extension mechanism to write custom validators.
Before you begin!
Follow the steps given below to set up a MySQL database for this tutorial.
- Download the product installer from here, and run the installer.
Let's call the installation location of your product the <EI_HOME> directory. This is located in a place specific to your OS as shown below:OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.4.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.4.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.4.0
CentOS /usr/lib64/EnterpriseIntegrator/6.4.0
- Install the MySQL server.
Download the JDBC driver for MySQL from here and copy it to your
<EI_HOME>/lib
directory.If the driver class does not exist in the relevant folders when you create the datasource, you will get an exception, such as '
Cannot load JDBC driver class com.mysql.jdbc.Driver
'.Create a database named
EmployeeDatabse
.CREATE DATABASE EmployeeDatabase;
Create the Employee table inside the Employees database:
USE EmployeeDatabase; CREATE TABLE Employees (EmployeeNumber int(11) NOT NULL, FirstName varchar(255) NOT NULL, LastName varchar(255) DEFAULT NULL, Email varchar(255) DEFAULT NULL, Salary varchar(255));
Let's get started!
Define a data service
Let's create a data service using the Create Data Service wizard:
Start the WSO2 ESB profile.
- Open the ESB profile's Management Console using
https://localhost:9443/carbon
, and log in usingadmin
as the username and the password. - Click Create under Data Service.
Add a name for the data service.
- Click Next and then click Add New Datasource.
Connect the datasource to the Company database that you defined above.
Datasource ID Datasource Datasource Type RDBMS Datasource Type (Default/External) Leave Default selected. Database Engine MySQL Driver Class com.mysql.jdbc.Driver
URL jdbc:mysql://localhost:3306/EmployeeDatabase
User Name Enter your MySQL server's username.
Example: rootPassword Enter your MySQL server's password.
If you have not assigned a password, keep this field empty.- Click Save and then click Next to go to the Queries screen.
Define a query with an input validator
Let's add a validator for the first name. If the user enters a name that is less than 3 characters for the first name an error needs to be thrown due to the validation.
You can set up different validators based on the type of the parameter or you can create your own custom validator. For more information, see Input Validators.
Follow the steps given below.
Click Add New Query to specify the query details:
Enter addEmployeeQuery as the query ID.
Enter the following SQL dialect:
insert into Employees (EmployeeNumber, FirstName, LastName, Email,Salary) values(:EmployeeNumber,:FirstName,:LastName,:Email,:Salary)
Click Generate Input Mapping and input mappings will be generated automatically.
- Click Edit that is next to FirstName.
Update the input mapping:
SQL Type Select String. IN/OUT Type Select IN. Validator Select Length Validator. - Maximum Value: Enter 30 as the maximum number of characters that can be entered for the first name.
Minimum Value: Enter 3 as the minimum number of characters that need to be there for the first name.
Click Add Validator.
Click Save and click Main Configurations to move to the query configurations.
Click Save.
Click Next and then click Add New Operation.
Enter the following details to create the addEmployeeOp operation that adds new employees to the database.
Operation Name addEmployeeOp
Query ID addEmployeeQuery
Save the operation.
Click Finish.
Try it out
The Deployed Services window allows you to manage data services. You can try the data service you created by using the TryIt tool in this screen, which is in your product by default.
- Click Try this service to open the TryIt tool.
- Click the addEmployeeOp operation and enter the following parameter values to the request:
- EmployeeNumber: 6001
- FirstName: AB
- LastName: Nick
- Email: test@test.com
- Salary: 1500
- Click Send to execute the operation.
A validation error is thrown as the response because the addEmployeeOp operation has failed. This is because the FirstName only has 2 characters. - Now, change the lastName and the email address in the request as shown below and execute the operation again.
- EmployeeNumber: 6001
- FirstName: ABC
- lastName: Nick
- Email: test@test.com
- Salary: 1500