|
Often a method parameter is actually the same as a property within a class, which already contains validation annotations.
So it would be a great feature to just let the method parameter validation point to that class and that property and let the validator use it.
Let's say you have a User class which contains an ID:
@Nonnull
@Range(min = 1, max = 9999)
private Integer id;
And then you have a method which finds the user:
public User findUser(Integer id) { ... }
Using the method validation, you can of course specify:
public User findUser(@Nonnull @Range(min = 1, max = 9999) Integer id) { ... }
But that's a violation of the DRY principle, since the validation already exists in the User class.
That's the use case for @ValidProperty. It assumes that the parameter's name is identical to the property of the specified class and then uses that property's validation.
For instance:
public User findUser(@ValidProperty(User.class) Integer id) { ... }
Of course, it should be possible to use a different parameter name, like:
public User findUser(@ValidProperty(value=User.class, property="id") Integer userID) { ... }
The previous validation framework that we used, OVal, had this feature and implemented it by weaving in the necessary code with AspectJ.
|