Home > database >  javax validation annotation for an integer
javax validation annotation for an integer

Time:01-23

I have a class User as show below.

class User {

private String name;

private Integer percent;

}

I wanted to have validation on percent field using javax annotations such that it only allows 4 values. (10, 50, 80, 100)

I know @Min, @Max, @Digit, but not sure how they serve my case.

I tried searching on https://www.baeldung.com/javax-validation but did not find any useful for my case.

I know I can create a custom validator. But is there any other way I can do this using existing annotations.

TIA !

CodePudding user response:

This is pretty much a non-answer, as you said

I know I can create a custom validator. But is there any other way I can do this using existing annotations.

There is no built-in validator that checks from a list of values. The closest would be @Pattern, but every implementation that I have seen only supports CharSequence. Custom validator route is probably the way to go. I bet someone has written one, so Google is your friend.

  • Related