Reputation: 441
I would like to use jsr303 in place of current custom validation framework because of some features that the custom framework doesn't support yet, and I'm investigating if the jsr supports a certain usecase.
Say I have a loan offer field presented to customer on some front-end, and the field is pre-populated with a loan amount generated by the system based on the user's credit score, and the customer is able to take the whole amount or lower than the offered value (so not more than what was offered).
field:
@DecimalMin("1000.00")
@DecimalMax(onlyKnowAtRuntime)
private BigDecimal loanOffer;
offer = $20 000.00
customer inputs = $50 000.00
Because the credit score is runtime generated, we don't know what the max (@DecimalMax
which in this case is $20 000.00
) value will be at compile time, but we need to make sure we validate that the customer doesn't take over $20 000.00
What ideas do you have around such a problem?
Upvotes: 2
Views: 163
Reputation: 19129
Your suggested use case is not possible with the provided Bean Validation constraints. Really Bean Validation is more about describing static constraints.
Once could imagine, however, to write a custom constraints @MaxCreditExceeded. Writing a custom constraint is easy, the question is how to get hold of the current credit limit for the user. One approach could be to use a ThreadLocal. But that requires that the credit limit gets properly set and cleared into this variable.
Upvotes: 1