uk.gov.service.payments.commons.api.validation.MapValueAsStringLengthValidator Maven / Gradle / Ivy
package uk.gov.service.payments.commons.api.validation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Map;
import java.util.Objects;
public class MapValueAsStringLengthValidator implements ConstraintValidator> {
private int max;
private int min;
@Override
public void initialize(MapValueLength constraintAnnotation) {
max = constraintAnnotation.max();
min = constraintAnnotation.min();
}
@Override
public boolean isValid(Map theMap, ConstraintValidatorContext context) {
if (theMap == null) {
return true;
}
return theMap.values().stream()
.filter(Objects::nonNull)
.map(Object::toString)
.noneMatch(value -> value.length() < min || value.length() > max);
}
}