Validation Framework in HDI

Q) What is validation framework?
Ans) This framework helps to validate the form data in the server side.

 

Q) Which data is validated?
Ans) Every request to the server (GET or POST method) have some parameters which is generally known as formData. The formData is generally a JSON(JavaScript Object Notation) Object. If the formData is not a JSON Object then it is either a key value pair, which we generally refer as query parameters.

Example of request.

 

GET Method

http://www.helicaltech.com/addUser.html?name=Arnold&age=28&mobile=98883888&dateOfRegistration=12/08/2013&action=addUser&qualification=matricualtion 

 

POST Method

http://www.helicaltech.com/adduser.html
           name=Arnold
           age=28
           mobile=98883888
           dateOfRegistration=12/08/2013
           qualification=matricualtion
           action=add

 

POST Method using formData as JSON object

http://www.helicaltech.com/adduser.html

formData{
   "name":"Arnold",
   "Age":28,
   "mobile":"98883888",
   "dateOfRegistration":"12/08/2013",
   "action":"add",
   "qualification":"matricualtion"}

 

Q) How to configure the validation?
 Ans) Basically two files are to be considered for configuring the validation

  1. regex.properties
  2. validation.xml

 

Q) What is regex.properties?
Ans) regex.properties file is a property file. Any data validation regex can be configured here. See more about regular expression here
Example of regex.properties

email=^[A-Za-z0-9._%'-]+@[A-Za-z0-9.-]+\\.[a-zA-Z0-9]+$
IPAddress=^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
text=^[a-zA-Z0-9\\s\\._%\\{\\}\\(\\)%\\?"':@,/]*$
fileName=^[a-zA-Z0-9][a-zA-Z0-9 \\-\\.\\_\\~\\&\\(\\)\\[\\]\\:]+$

here email is the regex key ^[A-Za-z0-9._%’-]+@[A-Za-z0-9.-]+\\.[a-zA-Z0-9]+$ is regex value.

 

Q) What is validation.xml?
Ans) This file we have to map the url which we are going to validate. The url mapping has to be configured in the <mappings> tag

<mappings>
   <url pattern="/addUser.html" definition-file="createUsers.xml" />
</mappings>

 

This means the “/addUser.html” is the url whose request parameters needs to be validated. The definition-file attribute holds the xml file-name where the actual rules are configured.

 

Q) How rules are configured?
Ans)The rules are pretty simple. They are configured in the xml file with the request paramters key as the element and rules as the attribute.

 

For example the createUser.xml is configured as following

<?xml version="1.0" encoding="UTF-8"?>
<formData>
   <action type="text" required="true"/>
   <name type="userName" />
   <age type="number" requiredIf="action" condition="=" value="add" minLength="6" maxLength="60"/>
    <qualification type="text"/>
    <dateOfRegistration type="date" maxLength="60" requiredIf="qualification" condition="=" value="engineer" />
</formData>

 

Q) What is type attribute?
Ans) The type attribute holds the key defined in regex.properties. This attribute is not mandatory. If the request parameter is not empty then this regex is validated against the value of the request parameter.

The type attribute can have multiple key name separated by ,
eg type=”text,notNumber,notSpecialCharaters” here each key regex is validated and, if any of the key is found to be invalid the data is considered to be invalid.

 

Q) What is required attribute?
Ans) The required attribute is used when the request parameter should not be empty. In the above example the action and the name is required.
Q) What is minLength attribute?
Ans) The minLength attribute defines the minimum length (minimum characters) of the value for the request parameter.

 

Q) What is maxLength attribute?
Ans) The maxLength attribute defines the maximum length (maximum characters) of the value for the request parameter.

 

Q)What is length attribute?
Ans) When we know that the request parameter has to have the exact length then we can use this attribute.

 

Q) What is requiredIf attribute?
Ans) Sometime one parameter is dependent on another parameter . For example the dateOfRegistration is required if qualification is engineer.

With requiredIf attribute two more attribute condition and value need to be set

The condition attribute can have any operator eg. =, <= , >=, ==, != etc

The value attribute should have the value which is a rule.

<qualification type="text"/>
    <dateOfRegistration type="date" maxLength="60" requiredIf="qualification" condition="=" value="engineer" />

Leave a Reply