Here is the solution for ConstraintValidatorContext.
I've designed a fluent API to create error reports and add their message and potentially their sub nodes and the appropriate contextual information.
I've used a nested interface model to keep things clean. Please review and comment.
/**
* Provide contextual data and operation when applying a given constraint validator
*
* @author Emmanuel Bernard
*/
public interface ConstraintValidatorContext {
/**
* Disable the default error message and default ConstraintViolation object generation.
* Useful to set a different error message or generate a ConstraintViolation based on
* a different property
*/
void disableDefaultError();
/**
* @return the current uninterpolated default message
*/
String getDefaultErrorMessage();
/**
* Return an error builder building an error allowing to optionally associate
* the error to a sub path.
* The error message will be interpolated.
* <p/>
* To create the error, one must call either one of
* the #addError method available in one of the
* interfaces of the fluent API.
* If another method is called after #addError() on
* ErrorBuilder or any of its associated nested interfaces
* an IllegalStateException is raised.
* <p/>
* If <code>isValid<code> returns <code>false</code>, a <code>ConstraintViolation</code> object will be built
* per error including the default one unless {@link #disableDefaultError()} has been called.
* <p/>
* <code>ConstraintViolation</code> objects generated from such a call
* contain the same contextual information (root bean, path and so on) unless
* the path has been overriden.
* <p/>
* To create a different error, a new error builder has to be retrieved from
* ConstraintValidatorContext
*
* Here are a few usage examples:
* <pre>//create new error with the default path the constraint
* //is located on
* context.buildErrorWithMessage( "way too long" )
* .addError();
*
* //create new error in the "street" subnode of the default
* //path the constraint is located on
* context.buildErrorWithMessage( "way too long" )
* .inSubNode( "street" )
* .addError();
*
* //create new error in the "addresses["home"].city.name
* //subnode of the default path the constraint is located on
* context.buildErrorWithMessage( "this detail is wrong" )
* .inSubNode( "addresses" )
* .inSubNode( "country" )
* .inIterable().atKey( "home" )
* .inSubNode( "name" )
* .addError();
* </pre>
*
* @param message new uninterpolated error message.
*/
ErrorBuilder buildErrorWithMessage(String message);
/**
* Error builder allowing to optionally associate
* the error to a sub path.
*
* To create the error, one must call either one of
* the #addError method available in one of the
* interfaces of the fluent API.
* If another method is called after #addError() on
* ErrorBuilder or any of its associated objects
* an IllegalStateException is raised.
*
*/
interface ErrorBuilder {
/**
* Add a subNode to the path the error will be associated to
*
* @param name property
* @return a builder representing the first level node
*/
NodeBuilder inSubNode(String name);
/**
* Add the new error report to be generated if the
* constraint validator mark the value as invalid.
* Methods of this ErrorBuilder instance and its nested
* objects returns IllegalStateException from now on.
*
* @return ConstraintValidatorContext instance the ErrorBuilder comes from
*/
ConstraintValidatorContext addError();
/**
* Represent asubnode whose context is known
* (ie index, key and isInIterable)
*/
interface NodeBuilder {
/**
* Add a subNode to the path the error will be associated to
*
* @param name property
* @return a builder representing this node
*/
InIterableNodeBuilder inSubNode(String name);
/**
* Add the new error report to be generated if the
* constraint validator mark the value as invalid.
* Methods of the ErrorBuilder instance this object comes
* from and the error builder nested
* objects returns IllegalStateException from now on.
*
* @return ConstraintValidatorContext instance the ErrorBuilder comes from
*/
ConstraintValidatorContext addError();
}
/**
* Represent a subnode whose context is
* configurable (ie index, key and isInIterable)
*/
interface InIterableNodeBuilder {
/**
* Mark the node as being in an Iterable or a Map
* @return a builder representing iterable details
*/
InIterablePropertiesBuilder inIterable();
/**
* Add a subNode to the path the error will be associated to
*
* @param name property
* @return a builder representing this node
*/
InIterableNodeBuilder inSubNode(String name);
/**
* Add the new error report to be generated if the
* constraint validator mark the value as invalid.
* Methods of the ErrorBuilder instance this object comes
* from and the error builder nested
* objects returns IllegalStateException from now on.
*
* @return ConstraintValidatorContext instance the ErrorBuilder comes from
*/
ConstraintValidatorContext addError();
}
/**
* Represent choices for a node which is
* in an Iterator or Map.
* If the iterator is an indexed collection or a map,
* the index or the key should be set.
*/
interface InIterablePropertiesBuilder {
/**
* Define the key the object is into the Map
*
* @param key map key
* @return a builder representing the current node
*/
NodeBuilder atKey(Object key);
/**
* Define the index the object is into the List or array
*
* @param index index
* @return a builder representing the current node
*/
NodeBuilder atIndex(Integer index);
/**
* Add a subNode to the path the error will be associated to
*
* @param name property
* @return a builder representing this node
*/
InIterableNodeBuilder inSubNode(String name);
/**
* Add the new error report to be generated if the
* constraint validator mark the value as invalid.
* Methods of the ErrorBuilder instance this object comes
* from and the error builder nested
* objects returns IllegalStateException from now on.
*
* @return ConstraintValidatorContext instance the ErrorBuilder comes from
*/
ConstraintValidatorContext addError();
}
}
}