* @param field the field to be validated
* @param object the value to be validated
* @return a collection of {@link ValidationError}
*/
protected Set<ValidationError> validateStringConstraint(final Field field, final Object object) {
final StringConstraint stringConstraint = field.getAnnotation(StringConstraint.class);
Set<ValidationError> errors = new HashSet<ValidationError>();
if (stringConstraint != null) {
if (stringConstraint.required() & (object == null)) errors.add(new ValidationError(field.getName(), errorCodes[0], stringConstraint, object));
if ((object != null) & (stringConstraint.regexp().length() > 0)) {
try {
Pattern pattern = Pattern.compile(stringConstraint.regexp());
Matcher matcher = pattern.matcher(object.toString());
if (!matcher.matches()) errors.add(new ValidationError(field.getName(), errorCodes[1], stringConstraint, object));
} catch (Exception ex) {
throw new ValidationException(ex);
}
}
if ((object != null) & (stringConstraint.minLength() > 0)) {
if (object.toString().length() < stringConstraint.minLength()) errors.add(new ValidationError(field.getName(), errorCodes[2], stringConstraint, object));
}
if ((object != null) & (stringConstraint.maxLength() > 0)) {
if (object.toString().length() > stringConstraint.maxLength()) errors.add(new ValidationError(field.getName(), errorCodes[3], stringConstraint, object));
}
}
return errors;
}