Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

My journey to use a validation framework

9,396 views

Published on

Presentation about how i evaluated different validation frameworks and chosen one for my project.

Published in: Technology, Education

My journey to use a validation framework

  1. 1. My Journey to Validate Muhammad Saqib Sarwar [email_address]
  2. 2. 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]
  3. 3. <ul><li>New Change Arrived in current project </li></ul><ul><ul><li>DB Fields From 11 to 137 </li></ul></ul><ul><ul><li>Account [54 Fields] Customer [74 Fields] Customer_account[9 Fields] </li></ul></ul><ul><ul><li>So Big Change on Message Validations </li></ul></ul><ul><ul><li>For: Create an Account Message </li></ul></ul><ul><ul><ul><li>Total: 46 Fields </li></ul></ul></ul><ul><ul><ul><ul><li>14 Mandatory Fields </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>13 have length checks </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>5 have pattern checks </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>others if exist should be validated too …. </li></ul></ul></ul></ul>How I felt the need ? Simply Means a huge collection of if-else statements Muhammad Saqib Sarwar [email_address]
  4. 4. <ul><li>1 st I written required if-else statements. </li></ul><ul><li>BUT Soon </li></ul><ul><li>I Found </li></ul><ul><li>Myself </li></ul><ul><li>In huge </li></ul><ul><li>Mess ..  </li></ul>How I felt the need ? Muhammad Saqib Sarwar [email_address]
  5. 5. <ul><li>Asked the BOSS </li></ul><ul><li>Google “how to avoid too many if-else” </li></ul><ul><li>Placed Question on JavaRanch </li></ul><ul><li>Consulted with Colleagues </li></ul><ul><li>Surprised ! </li></ul><ul><li>Business layer validation frameworks </li></ul><ul><li>exist in java world </li></ul><ul><li>I only know them as presentation </li></ul><ul><li>layer validator. </li></ul>What I did next … Muhammad Saqib Sarwar [email_address]
  6. 6. <ul><li>There are many available like </li></ul><ul><ul><li>Spring’s own validation </li></ul></ul><ul><ul><li>Commons Validator </li></ul></ul><ul><ul><li>JaValid </li></ul></ul><ul><ul><li>Hibernate Validator (My Chosed One) </li></ul></ul><ul><ul><li>And many more … </li></ul></ul><ul><li>Now the Question is </li></ul><ul><li>Which one to use ? </li></ul><ul><li>So I Sorted Few Priorities </li></ul><ul><ul><li>It should Solves the problem </li></ul></ul><ul><ul><li>It should have Short learning curve </li></ul></ul>Which one to use ..? Muhammad Saqib Sarwar [email_address]
  7. 7. <ul><li>First I looked at Validation in Spring 2.0.8 (as we already using it) </li></ul><ul><li>Two Parts in the Validation Package </li></ul><ul><ul><li>Validator Interface Implementation </li></ul></ul><ul><ul><li>DataBinder </li></ul></ul><ul><li>Primarily used in Spring-MVC framework </li></ul><ul><li>Have to implement complete validation logic in validate() method </li></ul><ul><li>Separate Validator for each entity </li></ul><ul><li>Not fallow any standard (like few emerging in the market) </li></ul>Validation in Spring 2.0.8 Muhammad Saqib Sarwar [email_address]
  8. 8. <ul><li>Validation of JavaBeans based on an xml file </li></ul><ul><li>Provides two distinct sets of functionality </li></ul><ul><ul><li>A configurable (typically XML) validation engine </li></ul></ul><ul><ul><li>Reusable &quot;primitive&quot; validation methods </li></ul></ul><ul><li>Steps </li></ul><ul><ul><li>Create XML file </li></ul></ul><ul><ul><li>Instantiate Validator class (org.apache.commons.validator.Validator) </li></ul></ul><ul><ul><li>Add Resources (JavaBean,Locale) </li></ul></ul><ul><ul><li>Call Validator’s validate method </li></ul></ul><ul><ul><li>Evaluate Results </li></ul></ul>Commons Validator Muhammad Saqib Sarwar [email_address]
  9. 9. <ul><li>Heavily depends on XML </li></ul><ul><li>Manual evaluation of errors [  ] </li></ul><ul><li>No default error messages </li></ul><ul><li>Lengthy validation code </li></ul><ul><li>Fallow No Standard </li></ul>Commons Validator Muhammad Saqib Sarwar [email_address]
  10. 10. <ul><li>I want something easier than XML so searched for Annotation based bean validation frameworks and found JaValid.org </li></ul><ul><li>It is an Annotations based bean validation framework </li></ul><ul><li>Could be used standalone </li></ul><ul><li>Provides full integration with Spring, JSF, Facelets and DB </li></ul><ul><li>Customizable and Extensible </li></ul><ul><li>Built in annotations for basic validations </li></ul><ul><li>Well elaborated documentation </li></ul><ul><li>Well documented source code </li></ul>Muhammad Saqib Sarwar [email_address]
  11. 11. <ul><li>Annotate Bean’s Properties/(getter Methods) </li></ul><ul><li>Instantiate Validator implementation </li></ul><ul><li>Pass bean instance to Validator </li></ul><ul><li>Validator returns List<ValidationMessage> </li></ul>Steps to use JaValid Muhammad Saqib Sarwar [email_address]
  12. 12. <ul><li>only few built in annotation constraints </li></ul><ul><li>building new constraints are little lengthier </li></ul><ul><li>use XML configuration file to know about annotation classes and their related validator classes. </li></ul><ul><li>error messages are hard coded </li></ul><ul><li>ValidationMessage object is not customizable </li></ul><ul><li>does not fallow any standard for validation </li></ul>Few disadvantages of JaValid ! Good Solution but not as simple and easy as hibernate validator Muhammad Saqib Sarwar [email_address]
  13. 13. <ul><li>Annotations based bean validation framework </li></ul><ul><li>Aims to provide implementation of the emerging standard (JSR - 303 Bean Validation) </li></ul><ul><li>Purely intended for multi-layered data validation </li></ul><ul><li>Constraints are expressed in a single place (model) </li></ul><ul><li>Not limited to use with hibernate </li></ul><ul><ul><li>Can be used any where in application </li></ul></ul><ul><ul><li>Can be used with any java persistence provider </li></ul></ul>Hibernate Validator Muhammad Saqib Sarwar [email_address]
  14. 14. <ul><li>Decent Collection of Built in Constraints </li></ul><ul><ul><li>@Length (min=,max=) </li></ul></ul><ul><ul><li>@Max(value=) </li></ul></ul><ul><ul><li>@Min(value=) </li></ul></ul><ul><ul><li>@NotNull </li></ul></ul><ul><ul><li>@NotEmpty </li></ul></ul><ul><ul><li>@Past </li></ul></ul><ul><ul><li>@Future </li></ul></ul><ul><ul><li>@Size(min=, max=) </li></ul></ul>Hibernate Validator <ul><li>@Pattern(regex=&quot;regexp”, flag=) </li></ul><ul><li>@Range(min=, max=) </li></ul><ul><li>@AssertFalse </li></ul><ul><li>@AssertTrue </li></ul><ul><li>@Valid </li></ul><ul><li>@Email </li></ul><ul><li>@CreditCardNumber </li></ul><ul><li>@Digits </li></ul><ul><li>@EAN </li></ul><ul><li>Few more … </li></ul>Muhammad Saqib Sarwar [email_address]
  15. 15. <ul><li>Error Messages </li></ul><ul><ul><li>Validator returns array of InvalidValue </li></ul></ul><ul><ul><li>InvalidValue contains constraint violation information (bean, name, path, value etc) </li></ul></ul><ul><ul><li>It also contains Error Description Message </li></ul></ul><ul><ul><li>we can override Error Messages </li></ul></ul><ul><ul><ul><ul><li>By Creating Own ValidatorMessages.properties </li></ul></ul></ul></ul><ul><ul><ul><ul><li>we can embed constraint parameters in messages </li></ul></ul></ul></ul>Hibernate Validator Muhammad Saqib Sarwar [email_address]
  16. 16. 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]
  17. 17. Validation Code: Account account = new Account(); account.setAccountTitle(&quot;Mobex Ltd&quot;); ClassValidator<Account> accountValidator = new ClassValidator<Account>(Account.class); InvalidValue[] validationMessages = accountValidator. getInvalidValues (account); for(InvalidValue msg:validationMessages) System.out.println(msg.getPropertyPath()+&quot;-&quot;+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]
  18. 18. 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]
  19. 19. 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]
  20. 20. <ul><li>Steps For Writing Our Own Constraints </li></ul><ul><ul><li>Constraint Descriptor (annotation) </li></ul></ul><ul><ul><li>Constraint Validator (implementation class) </li></ul></ul>Hibernate Validator Muhammad Saqib Sarwar [email_address]
  21. 21. <ul><ul><li>Constraint Descriptor (annotation) </li></ul></ul><ul><li>@Documented </li></ul><ul><li>@ValidatorClass(ProcessingCodeValidator. class) </li></ul><ul><li>@Target({METHOD, FIELD}) </li></ul><ul><li>@Retention(RUNTIME) </li></ul><ul><li>public @interface ProcessingCode { </li></ul><ul><li>String code(); </li></ul><ul><li>String message() default &quot;Invalid Processing Code&quot;; </li></ul><ul><li>} </li></ul>Hibernate Validator org.hibernate.validator.ValidatorClass Muhammad Saqib Sarwar [email_address]
  22. 22. 2. Constraint Validator Implementation Class public class ProcessingCodeValidator implements Serializable, Validator<ProcessingCode> { private static final long serialVersionUID = 1146464976464879846L; private String code; public void initialize (ProcessingCode parameter) { code = parameter.code(); } public boolean isValid (Object value) { if ( value == null )return false; if (!( value instanceof String)) return false; String str = (String) value; if(str.equals(code)) return true; return false; } } Muhammad Saqib Sarwar [email_address]
  23. 23. Account account = new Account(); account.setAccountTitle(&quot;Mobex Ltd&quot;); account.setProcessingCode(IProcessingCode. CREDIT_ACCOUNT); ClassValidator<Account> accountValidator = new ClassValidator<Account>(Account.class); InvalidValue[] validationMessages = accountValidator.getInvalidValues(account); for(InvalidValue msg:validationMessages) System. out.println(msg.getPropertyPath()+&quot;-&quot;+msg.getMessage()); OUTPUT accountTitle-length must be between 10 and 100 processingCode-Invalid Processing Code Hibernate Validator Muhammad Saqib Sarwar [email_address]
  24. 24. <ul><li>Using the Validator </li></ul><ul><ul><li>Database Schema-Level Validation </li></ul></ul><ul><ul><li>ORM Integration </li></ul></ul><ul><ul><ul><li>Hibernate event-based validation </li></ul></ul></ul><ul><ul><ul><ul><li>PreInsertEvent </li></ul></ul></ul></ul><ul><ul><ul><ul><li>PreUpdateEvent </li></ul></ul></ul></ul><ul><ul><ul><li>Java Persistence event-based validation </li></ul></ul></ul><ul><ul><li>Application-level validation </li></ul></ul><ul><ul><li>Presentation layer validation </li></ul></ul>Hibernate Validator Muhammad Saqib Sarwar [email_address]
  25. 25. <ul><li>Benifits </li></ul><ul><ul><li>simplicity </li></ul></ul><ul><ul><ul><li>Avoid XML based configuration </li></ul></ul></ul><ul><ul><ul><li>Could be learned in short time </li></ul></ul></ul><ul><ul><ul><li>Could be implemented in minutes </li></ul></ul></ul><ul><ul><li>Constraints annotation could be applied to </li></ul></ul><ul><ul><ul><li>Property </li></ul></ul></ul><ul><ul><ul><li>Getter method </li></ul></ul></ul><ul><ul><ul><li>Bean Class </li></ul></ul></ul><ul><ul><li>Aims to Fallows a developing standard (JSR-303 Bean Validation) </li></ul></ul><ul><ul><li>Fallows DRY Principle [multi-layered validation] </li></ul></ul><ul><ul><li>etc [many more to explore] </li></ul></ul>Muhammad Saqib Sarwar [email_address]
  26. 26. <ul><li>We Use Validation Framework </li></ul><ul><ul><li>To avoid series of messy if-else statements </li></ul></ul><ul><ul><li>To Increase code readability </li></ul></ul><ul><ul><li>To make code easily maintainable </li></ul></ul><ul><ul><li>To avoid writing validation on multiple layers </li></ul></ul><ul><ul><li>To increase productivity </li></ul></ul>Summary Muhammad Saqib Sarwar [email_address]

×