How can this date be confirmed in the future against a different date?
I have the following bean:
class CampaignBeanDto {
@Future
Date startDate;
Date endDate;
...
}
Obviously what endDate
should be after startDate. I want to test it.
I know that I can manually implement an annotation for @FutureAfterDate
, a validator for this, and manually initialize the threshold date manually, but I want to use the @Validated
spring mvc annotation .
How can I achieve this?
source to share
You shouldn't use annotations for validation between fields, write a validation function instead. Explained in this answer to the question, Cross-Field Validation with Hibernate Validator (JSR 303) .
For example, write a validator function like this:
public class IncomingData {
@FutureOrPresent
private Instant startTime;
@Future
private Instant endTime;
public Boolean validate() {
return startTime.isBefore(endTime);
}
}
Then just call the validation function when you first get the data:
if (Boolean.FALSE.equals(incomingData.validate())) {
response = ResponseEntity.status(422).body(UNPROCESSABLE);
}
source to share