Search code examples
javavalidationannotationsbean-validationhibernate-validator

How to dynamically update the argument of OOTB hibernate validator annotations?


I use the @Min annotation for field validation.

@Min(100)
private Long cost;

I want to extract the argument of the annotation in a separate configuration file.

Is there way to achieve it?

P.S.

I understrand that I can write my own annotation and my own validator but I want to reuse a library's code.


Solution

  • No way to do it with annotations like @Min, @Max and so on. You can use @AssertTrue to annotate a method of your entity in which your own validation logic will be implemented.

    public class MyEntity {
         private Long cost;
    
         //getters and setters...
         @AssertTrue
         public boolean isValid() {
              long minCost = MyExternalConfig.getMinCost(); //get data from where you want
              return cost > minCost; 
         }
    
    }