I think there is a problem with the error messages a type (class level) constraint can generate.
The field/property constraint usually has a single error message template, but a type constraint can validate more than one thing (multiple sub-constraints), so it will need to generate more then one error message.
With the current API this seems to be impossible.
I think I have seen how this can be done from the agimatec guys (
http://code.google.com/p/agimatec-validation/ ).
A constraint can implement:
Code:
package com.agimatec.utility.validation;
/**
* Description: Interface for a single validation <br/>
* User: roman.stumm <br/>
* Date: 06.07.2007 <br/>
* Time: 10:04:39 <br/>
*
*/
public interface Validation {
/**
* Perform a single validation routine
* Validate the object or property according to the current ValidationContext.
* @param context - to access the property, value, constraints
* @param listener - to write results to
*/
void validate(ValidationContext context, ValidationListener listener);
}
And then it can generate arbitrary error message (reason) calling the listener:
Code:
package com.agimatec.utility.validation;
/**
* Description: The interface to collect errors found during validation<br/>
* User: roman.stumm <br/>
* Date: 06.07.2007 <br/>
* Time: 13:18:24 <br/>
*
*/
public interface ValidationListener {
/**
* Error notification sent by a Validation.
*
* @param reason a constant describing the reason. This is normally the key of the
* feature that was violated in the object 'owner' for property 'propertyName'
* @param context - contains
* bean = the object that contains the error (owner)
* propertyName = the Name of the attribute that caused the error
*/
void addError(String reason, ValidationContext context);
}
Also it looks like it will be very useful to have the validation context passed to each constraint. This way one constraint can delegate the validation to other constraints.
What do you think?