Different types of model validation So we have a RESTful webservice that implements CRUD operations for a model, like this one:

class MyModel {
    private long id;
    private String name;
    // other fields
    private boolean completed;
    private boolean active;
    // getter and setters
}


In this webservice there is also a path like /service/{id}/complete that updates just the field completed to true. This indicates a final step for the creation.

The save and the complete operation are pretty simple and similar (they both end up calling a saveOrUpdate ). Where they differ is the validation. The complete validation is stricter.

The one before me declared an interface:

interface Validator&lt;T&gt; {
    void validate(T t);
}


And then to check that all mandatory fields where there before setting the complete attribute to true we have something like (writing in pseudocode):

MandatoryFieldsValidator implements Validator&lt;MyModel&gt; {

    list = {'id', 'active'}

    public void validate(MyModel model) {
        foreach(element in list) {
            if(myModel.get(element) is null) throw ValidationException(element + " is mandatory");
        }
    }
}


So then I needed another type of validator and started doing something like this because I needed specific controls over the name field:

NameValidator implements Validator&lt;MyModel&gt; {
    public void validate(MyModel model) {
        if(model.name is null or empty) 
        throw ValidationException(element + " is mandatory");
        if(model.name.startswith("something"))
            throw ValidationException("something not valid prefix");
        if(model.name.length &gt; 10)
        throw ValidationException("too long");      
    }
}


The controller had an attribute of type Validator that instantiated a MandatoryFieldsValidator so I added another attribute of the same type instantiating a NameValidator.

Now what I don't like about this:


The fact that I need to add a new attribute to the class as soon as I have a new validator to use;
The fact that I'm repeating checks in different validators because even if name is not included in the MandatoryFieldValidator I'm applying the same logic inside NameValidator (is null or empty);
I will also add a new type of model and a MandatoryFieldValidator&lt;NewModel&gt; with the same type of control logic is a non-sense.


Can you give me some advice? I'm looking into a general pattern to organize this kind of validation but I'm using Java so if you have some Java specific solution is ok.
You could use the composite pattern to create a tree of Validator instances. Each node would validate its with its own Validator before delegating to children (if any). This way children don't need to check again what was already validated in parent nodes

public class ValidatorTree&lt;T&gt; implements Validator&lt;T&gt; {

    private Validator&lt;? super T&gt; node;
    private List&lt;Validator&lt;? super T&gt;&gt; children;

    // constructor etc. snipped for brevity

    @Override
    public void validate(T t) {
        node.validate(t);
        for (Validator&lt;? super T&gt; child : children) {
            child.validate(t);
        }
    }
}


This way you can bundle several validators into one (so only one root validator needs to be referenced). And validation order is guaranteed, so no need to perform duplicate checks.

Edit (in response to comment)

A Validator for Model1 may very well be different from a Validator for Model2. If Validators are to be reused for different models you can define interfaces that define the common parts to be validated.

e.g.

public interface Named {

    String getName();
}


and a matching NameNotNullValidator : 

public class NameNotNullValidator implements Validator&lt;Named&gt; {
    @Override
    public void validate(Named named) {
        if (named.getName() == null) {
            throw new ValidationException(/* appropriate message here */);
        }
    }
}


And this Validator can be reused in validator trees for all models that implement Named. Note that I adapted the generics in ValidatorTree for this.