Thursday, December 4, 2008

Schematron - validating web-forms and java objects

Whilst developing web applications, more than often we are confronted with the need to validate user inputs. On the client-side (browsers) we generally do that with scripting languages; and for those users who have perhaps deliberately disabled their browser script support; we perform the same set validation on the server side, and this time we write some lines-of-code to extract the user input from the HTTP request and validate the same. Well, although ironic; yet it seems to be a de-facto that - we have to specify the validation rules separately on the client and server side, for eventually validating something that is semantically same.

A technical analysis into the core reason reveals that – although the semantics of the herein discussed "thing" (i.e. the user input) are the same, yet the representations of the semantic on the client and server side are different; which enforces us to implement the extraction and validation logic differently. So going by that theory - if we can represent the semantics of the user input in a single unified format, we can potentially have a single definition and implementation of the validation rules too.

So, what should our choice of a single unified data representation format? Well, there are a plethora of data representation formats out there; and you may be attracted to choose one that best suits your industry. However, as this article intends to project Schematron as a common validation solution across the client and server; I limited my choice to XML with intent to convey the concept with simplicity.

Dear JSON lovers, here is an excellent library by Stefan Goessner, which provides helper function for converting JSON to XML and visa-versa; So, that means - Schematron can be potentially used to validate JSON representations too.

So, let’s apply the concept to practice -

Before we apply Schematron to validate the user inputs on the client side; one must first convert the web form input to XML. The following archive illustrates an approach for converting a web form to XML and validating the same against a standard Schematron based schema –
Once we have validated the user input on the client, we forward the same to the server; wherein we should re-use our validation rules and this time execute the same on the server side. For many of you out there this is perhaps a no-brainer. Nevertheless, we will discuss this aspect in similar details with the next post in this series.

Until then wish you a merry Christmas and a safe holiday season.

6 comments:

Anonymous said...

very interesting.. what about the next post relating this architecture ? thank you

Rahul Roy said...

Apologies for not being able to post lately, the postings will resume in a couple of weeks.

Surya Chinta said...

This is inline to what you have proposed of using Rhino for client side and server side validations.

-Surya

Rahul Roy said...

Yes Ravi, it is definitely the same same concept which i just illustrated a bit more herein.

Thanks for being a regular reader of my posts. :)

Surya Chinta said...

hi rahul,

that was me 'surya chinta' who worked with you in FlexApp.. Ravi is my nick name :-)..

cheers,
surya

Rahul Roy said...

Hey Surya,

Please to see you visiting the blog.

-- Rahul