File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Validation pattern. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Validation pattern." Watch "Validation pattern." New topic
Author

Validation pattern.

Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
Hi I was wondering if someone can help me to deal with a design problem. I have a big set of composite transfer objects I want to process. A part of this process is a validation. I have a set of rules that represent this validation, each rule can result in a set of states (say passed or a collection of errors). At the end of the validation I have to know either if the composite passed all the validations or if it failed. If the validation failed I have to know all the causes of the failure, lets say it didn�t fulfill one o more of the rules resulting in one or more error messages.

Does anyone knows a pattern that deals with this kind of problem. I think it�s almost the same kind of pattern you can apply to a web form validation.

Thanks Jim


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think your "collection of errors" is a good starting point. You could make an error reporting object with information about which object had what error, maybe even enough for the user to navigate back to the object and fix it up.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
Well the error collection seems a good idea. But I have a problem there. I�m trying to find/create a patter for the validation stuff because I have an application with two interfaces. One is a web interface and the other is a batch loading data from a plain text file. As far as I see the same validation rules applies to both of the interfaces. The thing that varies is the way I have to report the results. The web interface needs to know specifically which field had the error, the batch process only needs to know the summary of the errors. So the collection of errors is applicable only as far as I have a relation between the error and the field that has the wrong data.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If the collection of errors has all the data needed for both scenarios then each (batch or web) can pull out just what it needs, can't it?

See the Apache Commons Validator Framework. It externalizes a lot of rules into XML and builds larger validations out of small building blocks. We used an adaptation of it, so I don't know if the collection of errors was part of the original or something one of our guys added.
Scott Ambler
author
Ranch Hand

Joined: Dec 12, 2003
Posts: 608
Look into the "Filters and Pipes", or maybe it's called "Pipes and Filters" architectural pattern. It was in the Gang of Five book (Buschmann et. al., mid 90s). The basic idea is that data/objects are flowing along a pipe, pass through a filter, and something happens. In this case it would be a validation filter which "filters out" the errors.

The primary decision you'll need to make is whether the filter fixes the errors, if possible, or if it simply reports them (or both as the case may be).

- Scott


<a href="http://www-306.ibm.com/software/rational/bios/ambler.html" target="_blank" rel="nofollow">Scott W. Ambler</a><br />Practice Leader Agile Development, IBM Rational<br /> <br />Now available: <a href="http://www.ambysoft.com/books/refactoringDatabases.html" target="_blank" rel="nofollow">Refactoring Databases: Evolutionary Database Design</a>
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
Thanks Stan, do you know if the jakarta validator project has uml documentation online? Just to save me a lot of work by looking directly to the design and not searching through all the code.

The filters and pipes design is created to filter the information. Like giving a preprocessing. It seems good but it does not have a context to store the information that regards with the validation. I mean it will be difficult to know which validations failed because of which causes.

I�m giving a look to the sapia - vlad project� Does any one knows anything about it?
bparanj
Greenhorn

Joined: May 27, 2002
Posts: 27
Originally posted by Jim Tovar:
Thanks Stan, do you know if the jakarta validator project has uml documentation online? Just to save me a lot of work by looking directly to the design and not searching through all the code.

I�m giving a look to the sapia - vlad project� Does any one knows anything about it?


I think Filters and Pipes is an overkill for your problem. Commons Validator is good enough. Just look at the example that comes along with the documentation. It's pretty straightforward. I would define an interface for retrieving the error related information for both the web interface and the batch loader.


Moderator : <a href="http://groups.yahoo.com/group/OOAD_UML/" target="_blank" rel="nofollow">http://groups.yahoo.com/group/OOAD_UML/</a> <br />Home : <a href="http://www.zepho.com" target="_blank" rel="nofollow">http://www.zepho.com</a>
Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
I had somewhat similar requirement for validations and couldn't leverage on commons validator for other reasons. My approach was this:

1) Validation rule was expressed as expression - Each expression was composed from a bunch of basic-building-block validator components.
Consider a field on the UI, orderItemQty. The value must be between 1-100. This can be expressed as a mandatoryNumberRange rule composed of a mandatory, Number and range validator components. Once a rule is expressed is this way, they can be expressed recursively.

Implementation: A composite with individual sub classes representing each of the building-block validator component. The super class will act as a template to create validation expressions and will know how to execute each one of them.

public interface Validator
{
public void validate(Object aValue)
}

abstract class AbstractValidator implements Validator
{
/**
* The next validator in a validation expression. A null value for a
* childValidator means this is last validator or the only validator in a
* validation expression.
*/
private final Validator childValidator;

// template method
public final void validate(Object value)
{
doValidation(value);
processChildValidator(value);
}

protected abstract void doValidation(String aValue)
{
subclass to implement...
}

private final void processChildValidator(Object value)
{
if(this.childValidator == null)
return;
this.childValidator.validate(value);
}
} // end of abstract class

// This is a building-block validator component
class NumberRange extends AbstractValidator
{
protected void doValidation(Object value)
{
// implement validation here
}
}

2) Type conversion: Values from http client will have to coverted from string based values to its appropriate object type before triggering validation. Therefore, you might want some simple converter to do this before (1).

3) Error reporting: Similar to what was discussed above.


- Sathvathsan Sampath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation pattern.
 
Similar Threads
Passed
Builder and Composite Patterns?
Passed
Passed SCWD
Passed!