I want to validate the trimmed string length, like:
@Size(min = 5, max = 20, message = "Please enter a valid username (5-20 characters)")
String userName;
Given user name foo (6 chars), it will be trimmed to foo (3 chars) in the setter method, however, validate against the raw input seems a bit of useless.
Instead of altering the validation procedure, you could write your own constraint which wraps the expected validation logic.
I have found such an implementation of a
@TrimmedSizeannotation at the auto-trader-spring-spike project:And the validator:
It uses an Apache License 2.0, but even then you could write this code from scratch once you’ve seen it.