Reputation: 13
In my class I have an attribute with List of Short Codes: List[ShortCode]
.
I have a custom defined annotations for valid short code as ValidShortCode
.
Code for this implementation:
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = ShortCodeValidator.class)
@Documented
public @interface ValidShortCode {
String message() default "{ValidShortCode.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class ShortCodeValidator implements ConstraintValidator<ValidShortCode, String> {
private static final int MIN_LENGTH = 1;
private static final int MAX_LENGTH = 15;
private static final int EMPTY_LENGTH = 0;
@Override
public void initialize(ValidShortCode shortCode) {
// TODO Auto-generated method stub
}
@Override
public boolean isValid(final String value,
final ConstraintValidatorContext constraintContext) {
boolean valid = false;
if (null == value || value.trim().length() == EMPTY_LENGTH) {
valid = true;
}
else if((value.trim().length() >= MIN_LENGTH)&&(value.trim().length() <= MAX_LENGTH)){
valid = value.matches("([A-Za-z0-9])+");
}
return valid;
}
}
And for validating the above List[ShortCode]
I am writing another custom annotation for this List of short codes.
I have an idea to validate the short code again in this custom annotation but it is code duplication.
In my custom annotation for validating the List of Short Codes, I have this:
public class ListValidator implements ConstraintValidator<ValidList, List<String>> {
private static final int EMPTY_SIZE = 0;
@Override
public void initialize(ValidList validList) {
// TODO Auto-generated method stub
}
@Override
public boolean isValid(final List<String> value,
final ConstraintValidatorContext constraintContext) {
boolean valid = false;
if (null == value || value.size() == EMPTY_SIZE) {
valid = true;
}
else {
for(String shortCode: value) {
// Implementing the same code as done for the previous short code annotation
}
}
return valid;
}
}
Can some one help me in letting me know how I can reuse the custom defined annotation ValidShort
or any other efficient method to solve this situation?
Thanks.
Upvotes: 1
Views: 9946
Reputation: 335
You can do it without custom validators.
Wrap the short code inside a class and validate the String either on a getter or on the member itself:
class ShortCode {
private String value;
ShortCode(String value) {
this.value = value.trim();
}
@Pattern(regexp="PATTERN")
@Size(min=1, max=15)
String getValue() {
return value;
}
}
Add @Valid annotation before the list, this way all elements in the List will be validated according to the validation defined above:
@Valid
List<ShortCode> shortCodesList;
Upvotes: 3
Reputation: 11443
Nothing prevents you from using the same validator you've implemented already:
@Override
public boolean isValid(final List<String> value,
final ConstraintValidatorContext constraintContext) {
boolean valid = true;
if (null != value && value.size() != EMPTY_SIZE) {
ShortCodeValidator validator = new ShortCodeValidator();
for(String shortCode: value) {
result &= validator.isValid(shortCode);
}
}
return valid;
}
This example assumes that list validation should fail if at least one of list items is invalid.
Upvotes: 0