I’m using the popular Jakarta bean validation API v3.0.2 + Hibernate Validator. I’m having a lot of trouble debugging validation errors because beans I’m validating have a lot of fields, and the error messages do not include the name of the field that failed validation.
For example if I have an address field like this
public record Address(
@NotNull @Size(max = 50) String address1,
...
) {}
And I attempt to create one with a String with > 50 characters I get the message:
size must be between 0 and 50
I understand I could write a custom message for each field, like this:
public record Address(
@NotNull @Size(max = 50, message = "address1 must be between 0 and 50") String address1,
...
) {}
But, I’ve got hundreds of validated classes with many fields each. I don’t want to write and maintain thousands of custom message
‘s for every single @Size
annotation (+ all the other validation annotations like @NotNull).
How can I include information about which field failed validation in the error message by default? For such a popular library, I feel like there has to be a way to do this, but I haven’t been able to find a solution.