I can validate the length of a String with an annotation like:
@Length(max = 255)
But what if I want to verifiy that the length is either 5 or 9? Can this be done with an annotation?
I can validate the length of a String with an annotation like:
@Length(max = 255)
But what if I want to verifiy that the length is either 5 or 9? Can this be done with an annotation?
Here's the documentation for implementing custom constraint.
It's failry simple:
So perhaps your annotation might look like:
@Constraint(validatedBy=YourChecker.class)
//other annotations
public @interface AllowedValues {
int[] value();
}
Try @Pattern(regex=".{5}|.{9}") (change the dot to another character class if you don't want to match everything.