bean validation - Passing application context to JSR 303 ConstraintValidator -
some of jsr-3validation want context sensitive.
for example, web application has "year" context each request. when persisting new bean, want able constrain year property within year of current request context.
@incurrentcontextyear private localdate activationdate;
the current year contained in request object want pass (in way) constraint validator. example:
public class incurrentcontextyearvalidator implements constraintvalidator<incurrentcontextyear, localdate>{ @override public void initialize(incurrentcontextyear constraintannotation) {} @override public boolean isvalid( localdate value, constraintvalidatorcontext context, request request) { return dateutil.isinrequestyear(value, request) } }
now thread-local storage, i'm wondering if there's better way.
i recognize getting outside domain of "bean validation" seems using annotations such things defensible approach. annotations allow me keep beans pojos, , service nice way document rules of properties. why should need have entirely separate mechanism same thing bean validation?
Comments
Post a Comment