[bv-dev] When is a method validated
misterm at gmail.com
Mon Feb 27 18:28:01 EST 2012
Ok, let's complicate matters a little bit:
If a bean is exposed through an interface and therefore constraints
are applied to the implementation, obviously parameter validation
should happen right before invoking the method and return validation
should happen right after return. In this case, validation constraints
are completely opaque for the original caller and certainly they are
not part of the API.
If a bean is exposed *directly* to the caller, constraints are part of
the API exposed to the caller. They are part of the contract pretty
much like possible thrown checked exceptions and all, so it would make
more sense to validate parameters right after the caller invokes it
and validate the return value right before delivering it to the
caller. Then, the API does what it says.
Does this different behaviour sound complicated? Yes, but this is
technically the correct thing to do if you think about validation
being part of the API or not. As cumbersome as it seems, it is
possible to implement it using CDI since it allows one to get the
exact InjectionTarget and check for the API rule above. Should it be
done that way? That is open to discussion, but I am pretty sure other
people will reason it the way I just did.
More information about the beanvalidation-dev