'javax validation OneOf constraint
Is there an annotation in javax validation, so it should constraint that value is one of predefined values. Example
@OneOf(values = "12-14m, 16m, 18m")
private String size;
Solution 1:[1]
No, there is no such constraint and you have to write it's own.
Here is the example that does mostly the same but opposite @DenyValues
:
annotation usage on a class: https://github.com/php-coder/mystamps/blob/66a2254e6d78c03d0b50531752d860efe784a1fd/src/main/java/ru/mystamps/web/controller/dto/AddCountryForm.java#L87
annotation definition: https://github.com/php-coder/mystamps/blob/66a2254e6d78c03d0b50531752d860efe784a1fd/src/main/java/ru/mystamps/web/support/beanvalidation/DenyValues.java#L35-L44
validator implementation: https://github.com/php-coder/mystamps/blob/66a2254e6d78c03d0b50531752d860efe784a1fd/src/main/java/ru/mystamps/web/support/beanvalidation/DenyValuesValidator.java#L26-L51
Solution 2:[2]
If you are using hibernate-validator, you can declare new annotation using @ConstraintComposition
. This is a good example here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Slava Semushin |
Solution 2 | linarkou |