Search code examples
hibernatehibernate-validator

hibernate validator LengthValidator of truncated string


Is there a similar annotation to @Length in hibernate validator but it restricted on a trimmed string.

For example:

I have

@Length(max= 2);
String str;

when: str = " ab";
--> it will pass ok.

Thanks,


Solution

  • As fare i know, there isn't one.

    But you can easely implement your own.

    The other (not so good) way is to add a getter which trim the string.

    @Length(max= 2);
    String getTrimmedStr() {
       return this.str.trim();
    }
    

    But i think implementing your own Validator is the much cleaner approach.