Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSR 303 Bean Validation - Why on getter and not setter?

I don't understand why JSR 303 (bean validation) is for the getter methods and not setter? Isn't it more logical to put it under setter method since that is the entry point into a field and validation should be checked prior to that?

like image 755
yapkm01 Avatar asked Jun 08 '11 18:06

yapkm01


People also ask

In which implementation is the JSR 303 standard used?

Apache Bean Validation (formerly agimatec)

How does Bean Validation work?

Very basically, Bean Validation works by defining constraints to the fields of a class by annotating them with certain annotations.

What is Bean Validation constraints?

The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean. Constraints can be built in or user defined. User-defined constraints are called custom constraints.

Which object is used in Spring MVC by a request processing method to check validation failure?

Spring features a Validator interface that you can use to validate objects. The Validator interface works using an Errors object so that while validating, validators can report validation failures to the Errors object.


2 Answers

Annotating getters doesn't mean that validation is performed when a getter is invoked. It is just used to identify the property to which a constraint shall apply.

The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.

Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.

like image 184
Gunnar Avatar answered Sep 25 '22 04:09

Gunnar


Its a very good question and something that I have never paid attention to. But I think I know the answer ( and also why I never got this question myself).

If you are looking at this, from the point of view that, the annotation defines where the validation will happen, then putting it on getter does not make sense. ( why not validate while storing the value itself..). But this is not how it works...

The programmer needs to tell the validation framework, which properties needs to be validated. So you can put the annotation directly on the attribute (which I prefer) or you can put it on the getter. Both of them signify read operation. The Framework needs to read all the attributes of your class, that will have to be validated. So in this case, putting on setter makes no sense at all.. The key to understand is the perspective...

I hope it makes sense.

like image 23
uncaught_exceptions Avatar answered Sep 25 '22 04:09

uncaught_exceptions



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!