Builder Pattern For Complex Validations

I’ve written a small piece of code for common validations. The code can be extended to introduce more validations. Highlights –

  1. Builder Pattern
  2. Extensible code
  3. Non-instantiable Validation class
  4. Used a variety of concepts I discussed in the past month

Here is the link to the github respository. Do check it out.

https://github.com/73ddy/poc_trunk

Advertisements

5 responses to “Builder Pattern For Complex Validations

  • Rentius

    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

    • Gaurav

      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

      • Rentius

        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

  • Rentius

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: