I'm currently updating our code from a mix of Hibernate Validator 3 and custom validation logic to the new version. All in all it looks very nice.
Some minor issues, that I've encountered so far:
WarningsSection 2.1.1.3 mentions that payload() may be used to implement various severities (i.e. warnings). While that is better than nothing, it is much too limited; ConstraintValidator.isValid() should be able to set the severity, depending on the actual data being validated!
Example:
I need to validate bids for an auction. Placing a bid, that is below the best one is an error; placing a bid that is 100% larger than the best bid is a warning.
Of course, one could split that into two constraints, but that's not feasible for a larger data model with complex business rules.
Multiple ErrorsIt may be a good idea to explicitely mention, that ConstraintValidator.isValid() is allowed to report multiple errors at once, i.e. the code below triggers two constraint violations:
Code:
context.disableDefaultError();
context.buildErrorWithMessageTemplate("foo").addError();
context.buildErrorWithMessageTemplate("bar").addError();
Message InterpolationThe default message interpolation is a bit limited. For advanced cases, it should be possible to:
- have the offending value interpolated into the message
- reference the name of the invalid property
- add arbitary parameters
Example:
When validating events, you would require that start date is before end date:
Code:
if (startDate.after(endDate)) {
context.buildErrorWithMessageTemplate("{foo}").addSubNode("startDate").addError();
}
Without reference to the either the invalid value or the end date, the error message won't be very usable and would probably just say: "Start date is invalid" instead of "Start date must be before XX/XX/XXXX".
As a workaround I could make the message specific to this case ("Start date must be before end date"), but it would be nicer to have one generic error message, that can be reused for other date checks.
Cheers,
Stephan