I’ve written a small piece of code for common validations. The code can be extended to introduce more validations. Highlights –
- Builder Pattern
- Extensible code
- Non-instantiable Validation class
- Used a variety of concepts I discussed in the past month
Here is the link to the github respository. Do check it out.
April 17th, 2013 at 6:56 am
Hi Gaurav
I like the framework you wrote for validation.
Can you use the framework for validating against the db say for Login validation? I was thinking of writing isLoginValid which returns a LoginValidator and it throws an exception with invalid username password when the user cannot be found in the db.
Or would you only use the framework for validating null, Object instances etc.?
Kind regards
Rentius
April 17th, 2013 at 7:33 am
Hi Rentius
I recently used this technique of validation in a small project. And that was the time I realized the potential of this validation technique. And of course one can use it for various validations like login.
You’ll be glad to know, that I found some design problems with this technique and fixed it before implementing it in another project. Which has actually expanded it possibilities and scope.
Your question has inspired me to upload the new version. I’ll make sure I cut a branch in git for the updated version soon. đŸ™‚
Thnx
Gaurav
April 17th, 2013 at 7:43 am
Thank you Gaurav
Can you drop me a mail when you uploaded the new version.
i’m also playing around using generics.
When passing in a specific object and getting one back from the method call. Bit more type safe and no need to do instanceof.
Kind regards
Rentius
April 17th, 2013 at 7:48 am
Sure.
April 17th, 2013 at 10:14 am
Hi Gaurav
I went ahead and implemented a validation method for login.
i’ve implemented a Validation class called RequestValidation which uses generics to validate a certain request coming in. The calling method looks the same.
RequestValidation.checkThat(new Login(“rentius”,”testPassword”)).is(RequestValidation.isLoginValid());
Login implements the Request interface and isLoginValid() returns LoginValidator which implements Validator.
The validate method on Validator “validate(final T value)” and the validate method on LoginValidator “validate(final Login login)”. So I can take away instanceof in the validate method.
My next step is to return a certain response. A boolean for now when I do not want to throw an Exception and later change to an Object.
Kind regards
Rentius