How to create custom Validation Messages based on an annotation property? - java

I'm using the Hibernate #NotNull validator, and I'm trying to create custom messages to tell the user which field has generated the error when it is null. Something like this:
notNull.custom = The field {0} can't be null.
(this will be in my ValidationMessages.properties file).
Where the {0} should be the field name passed to the validator this way:
#NotNull(field="field name")
There is any way I can do that?

To customize your annotation message you need to disable the existing violation message inside isValid() method and build a new violation message and add it.
constraintContext.disableDefaultConstraintViolation();
constraintContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
In the given below example, I am creating an annotation for input date validation on the basis of "invalid date", "can't be greater than today date" and "date format is correct or not".
#CheckDateIsValid(displayPattern = "DD/MM/YYYY", programPattern = "dd/MM/yyyy", groups = Order2.class)
private String fromDate;
Annotation Interface -
public #interface CheckDateIsValid {
String message() default "default message";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String displayPattern();
String programPattern();
}
Annotation implementation Class -
public class CheckDateIsValidValidator implements ConstraintValidator<CheckDateIsValid, String> {
#Value("${app.country.timeZone}")
private String timeZone;
private String displayPattern;
private String programPattern;
#Override
public void initialize(CheckDateIsValid constraintAnnotation) {
this.displayPattern = constraintAnnotation.displayPattern();
this.programPattern = constraintAnnotation.programPattern();
}
#Override
public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
try {
// disable existing violation message
constraintContext.disableDefaultConstraintViolation();
if (object == null) {
return true;
}
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(programPattern);
LocalDateTime time = LocalDate.parse(object, formatter).atStartOfDay();
ZoneOffset zoneOffSet = ZoneOffset.of(timeZone);
OffsetDateTime todayDateTime = OffsetDateTime.now(zoneOffSet);
if (time == null) {
customMessageForValidation(constraintContext, "date is not valid");
return false;
} else if (todayDateTime.isBefore(time.atOffset(zoneOffSet))) {
customMessageForValidation(constraintContext, "can't be greater than today date");
return false;
}
return time != null;
} catch (Exception e) {
customMessageForValidation(constraintContext, "date format should be like " + displayPattern);
return false;
}
}
private void customMessageForValidation(ConstraintValidatorContext constraintContext, String message) {
// build new violation message and add it
constraintContext.buildConstraintViolationWithTemplate(message).addConstraintViolation();
}
}

If your requirement can be satisfied by interpolating hibernate messages, so you can create/name your *property file like that:
ValidationMessages.properties
And inside that:
javax.validation.constraints.NotNull.message = CUSTOMIZED MESSAGE WHEN NOTNULL is violated!
Hibernate by default searches a ResourceBundle named ValidationMessages. Also a locale might be involved: ValidationMessages_en, ValidationMessages_de, <..>
Hibernate will provide your customized message through interpolatedMessage parameter, so all ConstraintViolationException relevant information (included your message ) will be showed. So you message will be a part of real exception. Some unwieldy information will be provided!
If you want to make your custom exception (without default ConstraintViolationException behavior) check this out:
Using GenericDao concept, consider the following
public void saveOrUpdate(IEntity<?> entity) {
try {
if(entity.getId == null) {
em.persist(entity);
} else {
em.merge(entity)l
}
} catch(ConstraintViolationException cve) {
throw new ConstraintViolationEx(constructViolationMessage(cve.getConstraintViolations()));
}
}
private String constructMessage(Set<ConstraintViolation<?>> pConstraintViolations) {
StringBuilder customMessages = new StringBuilder();
for(ConstraintViolation<?> violation : pConstraintViolations) {
String targetAnnotation = violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName();
if(supportsCustomMessage(targetAnnotation)) {
applyMessage(violation, targetAnnotation, customMessages);
} else {
// do something with not customized constraints' messages e.g. append it to existing container
}
}
return customMessages.toString();
}
private void applyMessage(ConstraintViolation<?> pViolation, String pTargetAnnotation, StringBuilder pCustomMessages) {
String targetClass = pViolation.getRootBean().getClass().getName();
String targetField = pViolation.getPropertyPath().toString();
pCustomMessages.append(MessageFormat.format(getMessageByAnnotation(pTargetAnnotation), targetClass, targetField));
pCustomMessages.append(System.getProperty("line.separator"));
}
private String getBundleKey() {
return "ValidationMessages"; //FIXME: hardcoded - implement your true key
}
private String getMessageByAnnotation(String pTargetAnnotation) {
ResourceBundle messages = ResourceBundle.getBundle(getBundleKey());
switch(pTargetAnnotation) {
case "NotNull":
return messages.getString(pTargetAnnotation + ".message");
default:
return "";
}
}
private boolean supportsCustomMessage(String pTargetAnnotation) {
return customizedConstraintsTypes.contains(pTargetAnnotation);
}
Produced result:
test.model.exceptions.ConstraintViolationEx
test.model.Person : name cannot be null
test.model.Person : surname cannot be null
A hibernate ConstraintViolation provides relevant information about root class and restricted field. As you see, it applies for all hibernate supported constraints, so you need to check if current annotation can be customized by supportsCustomMessage(<..>)! If it can (it's up to you), you should get appropriate message by constraint annotation doing `getMessageByAnnotation(<..>)'.
All you need to do is implement not supported constraints logic. For example it can append it's cause message or interpolated with default message (and true exception goes to *log file)

Related

Is there a way to Spring custom validator reject value without field having a getter method?

I'm currently studying building API's with Spring. I'm working with Spring Validator to validate my input. Here it is my custom validator:
public class NewHoldValidator implements Validator {
private EntityManager manager;
public NewHoldValidator(EntityManager manager) {
this.manager = manager;
}
#Override
public boolean supports(Class<?> clazz) {
return NewHoldRequest.class.isAssignableFrom(clazz);
}
#Override
public void validate(Object target, Errors errors) {
if (errors.hasErrors()) {
return;
}
NewHoldRequest request = (NewHoldRequest) target;
Patron patron = manager.find(Patron.class, request.patronId);
BookInstance bookInstance = manager.find(BookInstance.class, request.bookInstanceId);
Assert.state(patron != null, "Patron does not exists.");
Assert.state(bookInstance != null, "Book instance does not exists.");
if (!bookInstance.acceptToBeHoldTo(patron)) {
errors.reject(null, "This book instance cannot be hold to this patron");
}
if (!request.hasDaysHold()) {
if (!patron.researcher()) {
errors.rejectValue("daysHold", null, "You need to pass a daysHold attribute");
}
}
}
}
And here is my NewHoldRequest class:
public class NewHoldRequest {
#NotNull
public final Long patronId;
#NotNull
public final Long bookInstanceId;
#Positive
#Max(60)
public final Integer daysHold;
public NewHoldRequest(#NotNull Long patronId, #NotNull Long bookInstanceId, #Positive #Max(60) Integer daysHold) {
this.patronId = patronId;
this.bookInstanceId = bookInstanceId;
this.daysHold = daysHold;
}
#Override
public String toString() {
return "NewHoldRequest{" + "patronId=" + patronId + ", bookId=" + bookInstanceId + ", daysHold=" + daysHold + '}';
}
public boolean hasDaysHold() {
return this.daysHold != null;
}
Even if my field "daysHold" is public I still need to create a getter to it so Spring can show the rejected error properly, otherwise, it will throw a NotReadablePropertyException. Is there a way to define that Spring can reject public fields without getters or I will need to add accessor methods to all fields I want to reject?
Here is the message that shows up when my validation is triggered.
org.springframework.beans.NotReadablePropertyException: Invalid property 'daysHold' of bean class [com.api.library.usecases.newhold.NewHoldRequest]: Bean property 'daysHold' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
I'll advise you to use the interface ConstraintValidator. It's a generic's interface, without casting and other problems.
You should create a custom constraint annotation for you it's #NewHoldValid after that implement CustomValidator interface:
#Component
public class NewHoldValidator implements ConstraintValidator<NewHoldValid, NewHoldRequest> {
#Override
public boolean isValid(NewHoldRequest value, ConstraintValidatorContext context) {
if(ANY_CONDITION){
return true;
}else {
return false;
}
}
After that use #NewHoldValid annotation above your NewHoldRequest.
If you'll want to set an error message text which different from default use that:
context.buildConstraintViolationWithTemplate("Your error message").addConstraintViolation();

#Notnull Spring Custom Annotation Validation with multiple field

How to write (type level annotation) custom annotation to allow a choice validation (exactly one of a number of properties has to be not null)?
Even if this "question" is very broad I will give an answer as I have exactly the needed piece of code here:
#Target(ElementType.TYPE)
#Retention(RUNTIME)
#Documented
#Constraint(validatedBy = ChoiceValidator.class)
public #interface Choice {
String[] value();
boolean multiple() default false;
String message() default "{com.stackoverflow.validation.Choice.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Here a validator using a bean property access:
public class ChoiceValidator implements ConstraintValidator<Choice, Object> {
private String[] properties;
private boolean allowMultiple;
#Override
public void initialize(Choice constraintAnnotation) {
if (constraintAnnotation.value().length < 2) {
throw new IllegalArgumentException("at least two properties needed to make a choice");
}
properties = constraintAnnotation.value();
allowMultiple = constraintAnnotation.multiple();
}
#Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
try {
BeanInfo info = getBeanInfo(value.getClass());
long notNull = Stream.of(properties)
.map(property -> Stream.of(info.getPropertyDescriptors())
.filter(desr -> desr.getName().equals(property))
.findAny()
.orElse(null)
)
.map(prop -> getProperty(prop, value))
.filter(Objects::nonNull)
.count();
return allowMultiple ? notNull != 0 : notNull == 1;
} catch (IntrospectionException noBean) {
return false;
}
}
private Object getProperty(PropertyDescriptor prop, Object bean) {
try {
return prop.getReadMethod() == null ? null : prop.getReadMethod().invoke(bean);
} catch (ReflectiveOperationException noAccess) {
return null;
}
}
}
A typical usage can look like this (lombok annotation to generate getters and setters):
#Data
#Choice({"one", "two"})
class OneOf {
private String one;
private String two;
private String whatever;
}
#Data
#Choice(value = {"one", "two"}, multiple = true)
class AnyOf {
private String one;
private String two;
}
But to clarify: Stackoverflow is a QA community for developers to exchange knowledge. It is not a place to ask "Can you please code this for me for free?". All valuable contributors at least try to solve problems first and come with detailed questions afterwards. People answering questions spend their spare time and are not payed for. Please show respect by asking for specific problems and showing own efforts in the future.

Spring service method and a complex validation logic/rules

At my Spring/Boot Java project I have a set of service methods, for example like a following one:
#Override
public Decision create(String name, String description, String url, String imageUrl, Decision parentDecision, Tenant tenant, User user) {
name = StringUtils.trimMultipleSpaces(name);
if (org.apache.commons.lang3.StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("Decision name can't be blank");
}
if (!org.apache.commons.lang3.StringUtils.isEmpty(url) && !urlValidator.isValid(url)) {
throw new IllegalArgumentException("Decision url is not valid");
}
if (!org.apache.commons.lang3.StringUtils.isEmpty(imageUrl) && !urlValidator.isValid(imageUrl)) {
throw new IllegalArgumentException("Decision imageUrl is not valid");
}
if (user == null) {
throw new IllegalArgumentException("User can't be empty");
}
if (tenant != null) {
List<Tenant> userTenants = tenantDao.findTenantsForUser(user.getId());
if (!userTenants.contains(tenant)) {
throw new IllegalArgumentException("User doesn't belong to this tenant");
}
}
if (parentDecision != null) {
if (tenant == null) {
if (findFreeChildDecisionByName(parentDecision.getId(), name) != null) {
throw new EntityAlreadyExistsException("Parent decision already contains a child decision with a given name");
}
} else {
if (findTenantedChildDecisionByName(parentDecision.getId(), name, tenant.getId()) != null) {
throw new EntityAlreadyExistsException("Parent decision already contains a child decision with a given name");
}
}
Tenant parentDecisionTenant = tenantDao.findTenantForDecision(parentDecision.getId());
if (parentDecisionTenant != null) {
if (tenant == null) {
throw new IllegalArgumentException("Public decision cannot be added as a child to tenanted parent decision");
}
if (!parentDecisionTenant.equals(tenant)) {
throw new IllegalArgumentException("Decision cannot belong to tenant other than parent decision tenant");
}
} else {
if (tenant != null) {
throw new IllegalArgumentException("Tenanted decision cannot be added as a child to public parent decision");
}
}
} else {
if (tenant == null) {
if (findFreeRootDecisionByName(name) != null) {
throw new EntityAlreadyExistsException("Root decision with a given name already exists");
}
} else {
if (findTenantedRootDecisionByName(name, tenant.getId()) != null) {
throw new EntityAlreadyExistsException("Root decision with a given name for this tenant already exists");
}
}
}
Decision decision = createOrUpdate(new Decision(name, description, url, imageUrl, parentDecision, user, tenant));
if (parentDecision != null) {
parentDecision.addChildDecision(decision);
}
criterionGroupDao.create(CriterionGroupDaoImpl.DEFAULT_CRITERION_GROUP_NAME, null, decision, user);
characteristicGroupDao.create(CharacteristicGroupDaoImpl.DEFAULT_CHARACTERISTIC_GROUP_NAME, null, decision, user);
return decision;
}
As you can see, most of the code lines from this method are occupied with a validation logic and I continue to adding a new validation cases there.
I want to refactor this method and move validation logic outside of this method in a more appropriate places. Please suggest how it can be done with Spring framework.
As chrylis mentioned in the comments, you can achieve this goal by using JSR-303 bean validation. The first step is to create a class that contains your input parameters:
public class DecisionInput {
private String name;
private String description;
private String url;
private String imageUrl;
private Decision parentDecision;
private Tenant tenant;
private User user;
// Constructors, getters, setters, ...
}
After that, you can start adding validation annotations, for example:
public class DecisionInput {
#NotEmpty
private String name;
#NotEmpty
private String description;
#NotEmpty
private String url;
#NotEmpty
private String imageUrl;
private Decision parentDecision;
private Tenant tenant;
#NotNull
private User user;
// Constructors, getters, setters, ...
}
Be aware that the #NotEmpty annotation is not a standard JSR-303 annotation, but a Hibernate annotation. If you prefer using standard JSR-303 you can always create your own custom validator. For your tenant and your decision, you certainly need a custom validator. First of all create an annotation (eg #ValidTenant). On your annotation class, make sure to add the #Constraint annotation, for example:
#Constraint(validatedBy = TenantValidator.class) // Your validator class
#Target({ TYPE, ANNOTATION_TYPE }) // Static import from ElementType, change this to METHOD/FIELD if you want to create a validator for a single field (rather than a cross-field validation)
#Retention(RUNTIME) // Static import from RetentionPolicy
#Documented
public #interface ValidTenant {
String message() default "{ValidTenant.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Now you have to create the TenantValidator class and make it implement ConstraintValidator<ValidTenant, DecisionInput>, for example:
#Component
public class TenantValidator implements ConstraintValidator<ValidTenant, DecisionInput> {
#Autowired
private TenantDAO tenantDao;
#Override
public void initialize(ValidTenant annotation) {
}
#Override
public boolean isValid(DecisionInput input, ConstraintValidatorContext context) {
List<Tenant> userTenants = tenantDao.findTenantsForUser(input.getUser().getId());
return userTenants.contains(input.getTenant());
}
}
The same can be done for the validation of the parent decision. Now you can just refactor your service method to this:
public Decision create(#Valid DecisionInput input) {
// No more validation logic necessary
}
If you want to use your own error messages, I suggest reading this answer. Basically you create a ValidationMessages.properties file and put your messages there.

MBeanOperationInfo and MBeanAttributeInfo meta data?

I have written a JMX interface for one of our applications. Another application then connects and allows the user to see various state related attributes / invoke operations remotely via this management tool. I stumbled across a small bug where our database connection settings are being exposed over JMX, with the password unencrypted. I would like to tag the attributes / operations that should be obfuscated with some flag, but it doesnt appear as though the MBeanAttributeInfo or MBeanOperationInfo objects support adding any user defined values exception for name and description. I suppose I could delimit the description field like
String desc = getAttrDesc() + ";" + getIsObfuscated();
But I dont like this approach very much. The question then is, is there a better way to provide arbitrary key value pairs to an attribute / operation info object, or the Dynamic MBean itself? It doesnt have to be on the info objects themselves, just as long as I can match them up on the management tool side. Any insight would be appreciated.
Just for clarification, when I construct the MBeanOperationInfo (leaving out the attributes for the sake of example) I do so like this:
LinkedList<MBeanOperationInfo> opperInfos = new LinkedList<MBeanOperationInfo>();
for (Method m : m_InstObj.getMethods()) {
InstrumentedOperation anno = m.getAnnotation(InstrumentedOperation.class);
String desc = anno.description();
opperInfos.add(new MBeanOperationInfo(desc, m));
}
m_Operations = new MBeanOperationInfo[opperInfos.size()];
int I = 0;
for (MBeanOperationInfo info : opperInfos) {
m_Operations[I] = info;
I++;
}
I would like the InstrumentedOperation annotation to have a field for obfuscated that I can use like this:
anno.obfuscated(); // <- retreives a boolean set as a compile time constant on the annotation
and be able to include this value in the Info object.
Then on the receiving side I do this:
MBeanOperationInfo[] operInfos = conn.getMBeanInfo(name).getOperations();
for (MBeanOperationInfo info : operInfos) {
String propName = getPropNameFromInfo(info.getName());
if (!uniqueSettings.contains(propName)) {
// this setting hasn't been handled, get the getters and setters and make the method map
String getter = getGetterForSetting(operInfos, info.getName());
String setter = getSetterForSetting(operInfos, info.getName());
Object value = conn.invoke(name, getter, new Object[] {}, new String[] {});
if (getter != null && setter != null) {
SettingMethodMap map = new SettingMethodMap(name.getKeyProperty("type"), propName, info.getName(), setter, getter, value);
uniqueSettings.add(propName);
m_Settings.add(map);
}
}
}
Here I would like to be able to retreive the key value pair through some mechanism, so I would know that I need to handle this field different and obfuscate it in the editor.
This can be achieved using the javax.management.DescriptorKey.
For example, using a code sample that I adapted for this, using a standard mbean:
"Obfuscated" annotation:
import java.lang.annotation.*;
import javax.management.DescriptorKey;
#Documented
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface Obfuscated {
#DescriptorKey("obfuscated")
boolean value() default true;
}
MBean interface:
public interface LoginMBean {
String getName();
#Obfuscated
String getPassword();
}
MBean implementation:
public class Login implements LoginMBean {
private final String user;
private final String password;
public Login(String user, String password) {
this.user = user;
this.password = password;
}
#Override public String getName() { return user; }
#Override public String getPassword() { return password; }
}
Some code to register the MBean and browse its information:
import java.lang.management.ManagementFactory;
import javax.management.*;
public class Main {
public static void main(String[] args) {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName mbeanName = new ObjectName("com.mydomain", "type", "login");
server.registerMBean(
new StandardMBean(new Login("John Doe", "password"), LoginMBean.class), mbeanName);
MBeanInfo mbeanInfo = server.getMBeanInfo(mbeanName);
MBeanAttributeInfo[] attrs = mbeanInfo.getAttributes();
for (MBeanAttributeInfo attr: attrs) {
Descriptor desc = attr.getDescriptor();
boolean obfuscated = false;
if (desc.getFieldValue("obfuscated") != null) {
obfuscated = (Boolean) desc.getFieldValue("obfuscated");
}
if (obfuscated) System.out.printf("field '%s' is obfuscated%n", attr.getName());
else {
Object value = server.getAttribute(mbeanName, attr.getName());
System.out.printf("value of field '%s' is '%s'%n",
attr.getName(), value == null ? "null" : value.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Finally, the output after running Main:
value of field 'Name' is 'John Doe'
field 'Password' is obfuscated

Is it possible to set multiple messages using oval AbstractAnnotationCheck?

I am using the Oval validation framework to validate fields that HTML fields cannot hold malicious javascript code. For the malicious code detection, I am using an external framework that returns me a list of errors that I would like to use as error messages on the field. The problem I am running into is that I can only setMessage in the check implementation, while I would rather do something like setMessages(List). So while I am currently just joining the errors with a comma, I would rather pass them back up as a list.
Annotation
#Target({ ElementType.METHOD, ElementType.FIELD})
#Retention( RetentionPolicy.RUNTIME)
#Constraint(checkWith = HtmlFieldValidator.class)
public #interface HtmlField {
String message() default "HTML could not be validated";
}
Check
public class HtmlFieldValidator extends AbstractAnnotationCheck<HtmlDefaultValue> {
public boolean isSatisfied( Object o, Object o1, OValContext oValContext, Validator validator ) throws OValException {
if (o1 == null) {
return true;
} else {
CleanResults cleanResults = UIowaAntiSamy.cleanHtml((String) o1);
if (cleanResults.getErrorMessages().size() > 0) {
String errors = StringUtils.join(cleanResults.getErrorMessages(), ", ");
this.setMessage(errors);
return false;
} else {
return true;
}
}
}
}
Model class
class Foo {
#HtmlField
public String bar;
}
Controller code
Validator validator = new Validator(); // use the OVal validator
Foo foo = new Foo();
foo.bar = "<script>hack()</script>";
List<ConstraintViolation> violations = validator.validate(bo);
if (violations.size() > 0) {
// inform the user that I cannot accept the string because
// it contains invalid html, using error messages from OVal
}
If setMessage(String message) is a method created by a superclass, you can override it and once it receives the data, simply split the string into a list and call a second function in which you would actually place your code. On a side note, I would also recommend changing the separating string to something more unique as the error message itself could include a comma.
Your question doesn't really make much sense though. If you are "passing them back up" to a method implemented in a superclass, then this voids the entire point of your question as the superclass will be handling the data.
I am going to assume the setError methods is a simple setter that sets a String variable to store an error message that you plan to access after checking the data. Since you want to have the data in your preferred type, just create a new array of strings in your class and ignore the superclass. You can even use both if you so desire.
public class HtmlFieldValidator extends AbstractAnnotationCheck<HtmlDefaultValue> {
public String[] errorMessages = null;
public void setErrorMessages(String[] s) {
this.errorMessages = s;
}
public boolean isSatisfied( Object o, Object o1, OValContext oValContext, Validator validator ) throws OValException {
if (o1 == null) {
return true;
} else {
CleanResults cleanResults = UIowaAntiSamy.cleanHtml((String) o1);
if (cleanResults.getErrorMessages().size() > 0) {
//String errors = StringUtils.join(cleanResults.getErrorMessages(), ", ");
//this.setMessage(errors);
this.setErrorMessages(cleanResults.getErrorMessages());
return false;
} else {
return true;
}
}
}
}
Elsewhere:
HtmlFieldValidator<DefaultValue> hfv = new HtmlFieldValidator<DefaultValue>();
boolean satisfied = hfv.isSatisfied(params);
if (!satisfied) {
String[] errorMessages = hfv.errorMessages;
//instead of using their error message
satisfy(errorMessages);//or whatever you want to do
}
EDIT:
After you updated your code I see what you mean. While I think this is sort of overdoing it and it would be much easier to just convert the string into an array later, you might be able to do it by creating a new class that extends Validator its setMessage method. In the method, you would call super.setMethod as well as splitting and storing the string as an array in its class.
class ValidatorWithArray extends Validator {
public String[] errors;
public final static String SPLIT_REGEX = ";&spLit;";// Something unique so you wont accidentally have it in the error
public void setMessage(String error) {
super.setMessage(error);
this.errors = String.split(error, SPLIT_REGEX);
}
}
In HtmlFieldValidator:
public boolean isSatisfied( Object o, Object o1, OValContext oValContext, Validator validator ) throws OValException {
if (o1 == null) {
return true;
} else {
CleanResults cleanResults = UIowaAntiSamy.cleanHtml((String) o1);
if (cleanResults.getErrorMessages().size() > 0) {
String errors = StringUtils.join(cleanResults.getErrorMessages(), ValidatorWithArray.SPLIT_REGEX);
this.setMessage(errors);
return false;
} else {
return true;
}
}
}
And now just use ValidatorWithArray instead of Validator
The situation in which I want to achieve this was different from yours, however what I found was best in my case was to create an annotation for each error (rather than having one that would return multiple errors). I guess it depends on how many errors you are likely to be producing in my case it was only two or three.
This method makes also makes your code really easy to reuse as you can just add the annotations wherenever you need them and combine them at will.

Categories

Resources