JSON Schema

JSON Schema

What is JSON?
JSON Stands for JavaScript Object Notation. This is a structure that can be used to store data as in key value pair separated by : . JSON can be used to transfer data to and fro any network. It is a simple plain text, it can be consumed by any programming language.

For example the address information can be stored in json format

"street":"Station Road"
"city" : "Hyderabad",

The value may be simple text or it may be an array, or json object as well.

What is JSON Schema?
To validate the values against a given json we can have schema. The schema is a template that can be used to check whether a given JSON is valid or invalid.

For example the above address json the key “city” is supposed to have some values in string/text if the “city” has some arbitary number “3445545” or a decimal values “34.34344” then it can lead to wrong information. The application consuming this JSON will have wrong data. Thus JSON schema help us to define some rules that can be used to validate a JSON.
How to define a JSON Schema?
Consider the following schema for the json structure person

"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
"lastName": {
"type": "string"
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
"required": ["firstName", "lastName"]

The key “title” is the title of the shema.
The key “type” defines the type of json. PersonJson It may be object, array etc.
The key “properties” has the actual definition of the PersonJson.
The firstName of the Person should be of type string
The lastName of the PersonJson should be type string.
The age of the PersonJson should be of type integer. The minimum value is 0
The key “required” in the schema tells which keys are mandatory and should be part of the PersonJson.

The Json schema does not restrict us to define native type but also we can have custom definition.

How to validate the Json against the Schema?
There are various libraries for different programming language to validate the schema against the Json.
Programming Language and popular schema library in json

"C": "WJElement (LGPLv3).",
"Java": "json-schema-validator (LGPLv3).",
"Ruby": "autoparse (ASL 2.0); ruby-jsonschema (MIT).",
"PHP": "php-json-schema (MIT). json-schema (Berkeley).",
"JavaScript" : "Orderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or AFL 2.0); schema.js"

More information can be obtained at http://json-schema.org/

Somen                                                                                                                    Sources: internet

Validation Framework in HDI

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



POST Method



POST Method using formData as JSON object




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._%'-][email protected][A-Za-z0-9.-]+\\.[a-zA-Z0-9]+$
fileName=^[a-zA-Z0-9][a-zA-Z0-9 \\-\\.\\_\\~\\&\\(\\)\\[\\]\\:]+$

here email is the regex key ^[A-Za-z0-9._%’-][email protected][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

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


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"?>
   <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" />


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" />