Great presentation! I was looking for a validation framework that was both easy to use (no code bloat) and reusable in different layers of our application. Hibernate annotations seems to be the way to go indeed, even though we intend to use OpenJPA.
(We chose OpenJPA for its better detachment support with fetchplans/fetch groups, which we think is almost a necessity if you have a remote rich client which will not use lazy loading for performance reasons and you don’t want to use unnecessary DTO’s).
My journey to use a validation framework - Presentation Transcript
My Journey to Validate Muhammad Saqib Sarwar [email_address]
1.To share my experience(thought process) of choosing a Validator framework. 2.To deliver basic knowledge of my chosen tool. Basic Goals Muhammad Saqib Sarwar [email_address]
How I felt the need ? Simply Means a huge collection of if-else statements Muhammad Saqib Sarwar [email_address]
1 st I written required if-else statements.
BUT Soon
I Found
Myself
In huge
Mess ..
How I felt the need ? Muhammad Saqib Sarwar [email_address]
Asked the BOSS
Google “how to avoid too many if-else”
Placed Question on JavaRanch
Consulted with Colleagues
Surprised !
Business layer validation frameworks
exist in java world
I only know them as presentation
layer validator.
What I did next … Muhammad Saqib Sarwar [email_address]
There are many available like
Spring’s own validation
Commons Validator
JaValid
Hibernate Validator (My Chosed One)
And many more …
Now the Question is
Which one to use ?
So I Sorted Few Priorities
It should Solves the problem
It should have Short learning curve
Which one to use ..? Muhammad Saqib Sarwar [email_address]
First I looked at Validation in Spring 2.0.8 (as we already using it)
Two Parts in the Validation Package
Validator Interface Implementation
DataBinder
Primarily used in Spring-MVC framework
Have to implement complete validation logic in validate() method
Separate Validator for each entity
Not fallow any standard (like few emerging in the market)
Validation in Spring 2.0.8 Muhammad Saqib Sarwar [email_address]
Validation of JavaBeans based on an xml file
Provides two distinct sets of functionality
A configurable (typically XML) validation engine
Reusable "primitive" validation methods
Steps
Create XML file
Instantiate Validator class (org.apache.commons.validator.Validator)
Add Resources (JavaBean,Locale)
Call Validator’s validate method
Evaluate Results
Commons Validator Muhammad Saqib Sarwar [email_address]
Heavily depends on XML
Manual evaluation of errors [ ]
No default error messages
Lengthy validation code
Fallow No Standard
Commons Validator Muhammad Saqib Sarwar [email_address]
I want something easier than XML so searched for Annotation based bean validation frameworks and found JaValid.org
It is an Annotations based bean validation framework
Could be used standalone
Provides full integration with Spring, JSF, Facelets and DB
Customizable and Extensible
Built in annotations for basic validations
Well elaborated documentation
Well documented source code
Muhammad Saqib Sarwar [email_address]
Annotate Bean’s Properties/(getter Methods)
Instantiate Validator implementation
Pass bean instance to Validator
Validator returns List<ValidationMessage>
Steps to use JaValid Muhammad Saqib Sarwar [email_address]
only few built in annotation constraints
building new constraints are little lengthier
use XML configuration file to know about annotation classes and their related validator classes.
error messages are hard coded
ValidationMessage object is not customizable
does not fallow any standard for validation
Few disadvantages of JaValid ! Good Solution but not as simple and easy as hibernate validator Muhammad Saqib Sarwar [email_address]
Annotations based bean validation framework
Aims to provide implementation of the emerging standard (JSR - 303 Bean Validation)
Purely intended for multi-layered data validation
Constraints are expressed in a single place (model)
Not limited to use with hibernate
Can be used any where in application
Can be used with any java persistence provider
Hibernate Validator Muhammad Saqib Sarwar [email_address]
Decent Collection of Built in Constraints
@Length (min=,max=)
@Max(value=)
@Min(value=)
@NotNull
@NotEmpty
@Past
@Future
@Size(min=, max=)
Hibernate Validator
@Pattern(regex="regexp”, flag=)
@Range(min=, max=)
@AssertFalse
@AssertTrue
@Valid
@Email
@CreditCardNumber
@Digits
@EAN
Few more …
Muhammad Saqib Sarwar [email_address]
Error Messages
Validator returns array of InvalidValue
InvalidValue contains constraint violation information (bean, name, path, value etc)
It also contains Error Description Message
we can override Error Messages
By Creating Own ValidatorMessages.properties
we can embed constraint parameters in messages
Hibernate Validator Muhammad Saqib Sarwar [email_address]
Sample Code [Annotated Account Bean] /*--------- Simple Bean ---------------*/ public class Account { @NotEmpty @Length(min=10,max=100) private String accountTitle; @NotEmpty private String processingCode; // regular getters and setters methods } Hibernate Validator Muhammad Saqib Sarwar [email_address]
Validation Code: Account account = new Account(); account.setAccountTitle("Mobex Ltd"); ClassValidator<Account> accountValidator = new ClassValidator<Account>(Account.class); InvalidValue[] validationMessages = accountValidator. getInvalidValues (account); for(InvalidValue msg:validationMessages) System.out.println(msg.getPropertyPath()+"-"+msg.getMessage()); OUTPUT accountTitle-length must be between 10 and 100 processingCode-may not be null or empty Hibernate Validator Muhammad Saqib Sarwar [email_address]
public class Account { @NotEmpty @Length(min=10,max=100) private String accountTitle; @NotEmpty private String processingCode; // regular getters and setters methods } Hibernate Validator Muhammad Saqib Sarwar [email_address]
public class Account { @NotEmpty @Length(min=10,max=100) private String accountTitle; @NotEmpty @ProcessingCode(code=IProcessingCode. CHECK_BALANCE) private String processingCode; // regular getters and setters methods } Hibernate Validator Muhammad Saqib Sarwar [email_address]
Steps For Writing Our Own Constraints
Constraint Descriptor (annotation)
Constraint Validator (implementation class)
Hibernate Validator Muhammad Saqib Sarwar [email_address]
1 comments
Comments 1 - 1 of 1 previous next Post a comment