com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.

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.

  1. Install the MySQL server.
  2. 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'.

  3. Create a database named EmployeeDatabse.

    CREATE DATABASE EmployeeDatabase;
  4. Create the Employee table inside the EmployeeDatabase:

    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:

  1. Start the WSO2 ESB profile.

  2. Open the ESB profile's Management Console using https://localhost:9443/carbon, and log in using admin as the username and the password.
  3. Click Create under Data Service.
  4. Add a name for the data service.

  5. Click Next and then click Add New Datasource.
  6. Connect the datasourceto the Company database that you defined above.

    Datasource IDDatasource
    Datasource TypeRDBMS
    Datasource Type (Default/External)Leave Default selected.
    Database EngineMySQL
    Driver Classcom.mysql.jdbc.Driver
    URLjdbc:mysql://localhost:3306/EmployeeDatabase
    User Name

    Enter your MySQL server's username.
    Example: root

    PasswordEnter your MySQL server's password.
    If you have not assigned a password, keep this field empty.
  7. 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.

  1. Click Add New Query to specify the query details:

    1. Enter addEmployeeQuery as the query ID.

    2. Enter the following SQL dialect:

      insert into Employees (EmployeeNumber, FirstName, LastName, Email,Salary) values(:EmployeeNumber,:FirstName,:LastName,:Email,:Salary)
    3. Click Generate Input Mapping and input mappings will be generated automatically.

    4. Click Edit that is next to FirstName.
    5. Update the input mapping:

      SQL TypeSelect String.
      IN/OUT TypeSelect IN.
      ValidatorSelect 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.

    6. Click Save and click Main Configurations to move to the query configurations.

    7. Click Save.

  2. Click Next and then click Add New Operation.

    1. Enter the following details to create the addEmployeeOp operation that adds new employees to the database.

      Operation NameaddEmployeeOp
      Query IDaddEmployeeQuery
    2. Save the operation.

  3. 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.  

  1. Click Try this service to open the TryIt tool.
  2. 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
  3. 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.
  4. 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
    The employee details are added to the database table.
com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links' is unknown.