[Download] | [Documentation Home] | [Release Note]

Input Validation Support

Introduction

Input validation allows data services to validate the input parameter values that are presented in a request. This allows to stops the execution of the request, if the input has not met the required criteria. WSO2 Data Services Server provides a set of built-in validators which will be sufficient for the mostly used use cases. And also it provides an extension mechanism, where custom validators can be written.

The validators can be set at the "Edit Input Mapping" page for each individual input mappings, which is at "Edit Query" -> "Input Mappings" -> "Edit".

Built-in Validators

The following sections explains the pre-defined standard validators that are available, the validators have to be added to individual input mappings in a query.

Long Range Validator

This validator validates an integer value, to check if it is in the specified range. The validator requires the minimum and the maximum value of the range to be validated. Figure 1 shows the addition of a Long Range Validator.


Figure 1: Adding a Long Range Validator.

Double Range Validator

This validator validates an floating point value, to check if it is in the specified range. The validator requires the minimum and the maximum value of the range to be validated. Figure 2 shows the addition of a Double Range Validator.


Figure 2: Adding a Double Range Validator.

Length Validator

This validator validates the string length of the given parameter. The validator requires the length to be passed in for validation. Figure 3 shows the step in adding a Length Validator.


Figure 3: Adding a Length Validator.

Pattern Validator

The pattern validator validates the string value of the parameter against a given regular expression. If the regular expression matches the target parameter, the validator succeeds, or else fails. The validator requires the regular expression to be passed in for validation. Figure 4 shows the step used in adding a Pattern Validator.


Figure 4: Adding a Pattern Validator.

Custom Validators

Validators feature in WSO2 Data Services Server allows the user to instroduce custom validation logic by adding Custom Validators. For defining a custom validator, the user must implement the interface "org.wso2.carbon.dataservices.core.validation.Validator". Below contains the definition of the interface.

 
public interface Validator {

	public void validate(ValidationContext context, String name, ParamValue value) throws ValidationException;
	
}
When defining a custom validator, the class must implement this interface. It contains a single method "validate", where if the validation is successful, nothing should happen, and if the validation fails, the "validate" method should throw an exception of type "ValidationException". The following explains the parameters passed into the "validator" method.
  • 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 a ARRAY parameter type.
NOTE: After a custom validator class is created, it must be packaged in a jar archive file, and must be put in the server's classpath location for external libraries, which is "CARBON_HOME/repository/component/lib/".

Demo

For a demonstration of the usage of validators, refer to the validators demo in the samples.