I believe that you're using Hibernate validators for this.
If that's the case, then depending on the special case you have, you would want to write a custom class-level validator for a Customer
instead, and let it be captured in the set of constraint violations.
Since I don't know what your Customer
bean looks like, I can only at best offer you a general direction on this approach.
// Annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Constraint(validatedBy = CustomerValidator.class)
public @interface ValidCustomer {
String message() default "Invalid customer (due to edge case)";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
// Validator
public class CustomerValidator implements ConstraintValidator<ValidCustomer, Customer> {
@Override
public void initialize(final ValidCustomer constraintAnnotation) {
}
@Override
public boolean isValid(final Customer value,
final ConstraintValidatorContext context) {
// logic for validation according to your edge case
}
}
You would then annotate your Customer
class with this validator.
@ValidCustomer
public class Customer {
// ...
}
After this, your specific constraint will be captured in violations
.