views:

45

answers:

2

Hi there.

I'm using the annotation @Length on a String:

@Length
private String variable;

I know that i can set a maximum value on this annotation, but does anyone knows if it is possible to set an unlimited max value?

Thanks

+2  A: 

For both javax.validation.Length and org.hibernate.validator.constraints.Length the default value is:

int max() default Integer.MAX_VALUE;

i.e. 2147483647. So, not quite unlimited, but enough.

If this doesn't suit you, you can define your own constraint (per JSR 303 - javax.validation) and use a long param instead of int.

Bozho
A: 

I know that i can set a maximum value on this annotation, but does anyone knows if it is possible to set an unlimited max value?

The max is an int and defaults to Integer.MAX_VALUE. And since the Java String API does NOT support Strings longer than Integer.MAX_VALUE characters (such a String would require more than 4GB of storage), this is coherent. And allowing a bigger size at the constraint level is definitely not.

And by the way, an unlimited max value doesn't make much sense as "constraint". If you don't want to constraint something, don't define a constraint.

As a side note, I suggest to use the standard @Size from bean validation instead of the @Length annotation from Hibernate Validator.

Pascal Thivent