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 'Data Service Hosting' feature provides a set of built-in validators for some of the most common use cases. It also provides an extension mechanism where custom validators can be written.
Validators have to be added to individual input mappings in a query. Follow the instructions below to add built-in/custom input validators.
1) Click Add New Query link in the Queries page.
The Queries page appears when creating a Data Service. For steps upto opening the Queries page, refer to sub sections in "Create a Data Service Using Various Data Sources" section.
2) In the Add New Query page that appears, provide the QueryID and DataSource.
The Input Mappings section opens.
3) Click the Add New Input Mapping link.
4) The Add Input Mapping page opens. The Validator drop-down menu contains four built-in validators and the custom validator option.
Built-In Input Validators
Explained below are the pre-defined, standard validators available in the 'Data Service Hosting' feature.
Long Range Validator
Validates an integer value and checks if it is in the specified range. The validator requires minimum and the maximum values of the range to be validated. For example,
Double Range Validator
Validates a floating point value and checks if it is in the specified range. The validator requires minimum and the maximum values of the range to be validated. For example,
Validates the string length of a given parameter. The length should be given for validation. For example,
Validates the string value of the parameter against a given regular expression. If the regular expression matches the target parameter, the validator succeeds; else it fails. The validator requires the regular expression to be passed in for validation.
The user can also introduce custom validation logic by adding Custom Validators. To define a custom validator, the user must implement the interface "org.wso2.carbon.dataservices.core.validation.Validator".
The definition of the interface is as follows:
When defining a custom validator, this interface should be implemented. The validate method in the interface is defined to throw an exception of type ValidationException if the validation fails. If the validation is successful, nothing happens.
- context : This parameter is of type "ValidationContext", which contains information about the full set of parameters passed into the request. When the validation logic depends on other parameters, the validation context can be used to check the names/values of the rest of the parameters.
- name : This is a string value which represents the name of the parameter to be validated.
- value : This parameter is of type "ParamType", which represents the value of the parameter to be validated. This can be either a SCALAR or an ARRAY.
After a custom validator class is created, it must be packaged in a jar archive file and put in the server's classpath location for external libraries which is <PRODUCT_HOME>/repository/component/lib.