Open spring-projects-issues opened 7 years ago
Feliks Khantsis commented
what are you talking about? Validation groups have been supported since forever.
public class Book {
@NotNull
(groups={Edit.class})
@Null
(groups={Registration.class})
private int id;
@NotNull
private String title;
}
Eric Deandrea commented
Yes Validation groups have been supported for a long time. My enhancement allows taking the concept of groups and applying it to a class that implements the Validator interface rather than using the JSR-303 annotations. The problem with the annotations is that it doesn't allow you to do cross-attribute validation (i.e. a class has 4 attributes and one of them is required, but it doesn't matter which so long as 1 of them isn't null - or the validation rules of some attributes on a class depend on the value(s) of other attributes). In these cases you can't use the JSR-303 annotations because the annotations only apply to a single attribute.
My enhancement would allow the Validator interface itself to look like it was not build prior to JDK 1.5 (by supporting generics and removing the need for the implementer to have to implement the supports method and having to cast Object to their model object type.
It would also allow for defining Validator methods and tagging them with groups - similar to how you would define groups using the JSR-303 annotations.
If we look at the Javadocs for the Validator interface and look at that example - with my enhancement that example could look like this:
public class UserLoginValidator extends GroupedValidator<UserLogin> {
private static final int MINIMUM_PASSWORD_LENGTH = 6;
public interface Identity {
}
public interface Secret {
}
@ValidationGroup(Identity.class)
public void validateUserName(UserLogin login, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "field.required");
}
@ValidationGroup(Secret.class)
public void validatePassword(UserLogin login, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
if (login.getPassword() != null && login.getPassword().trim().length() < MINIMUM_PASSWORD_LENGTH) {
errors.rejectValue("password", "field.min.length", new Object[] { Integer.valueOf(MINIMUM_PASSWORD_LENGTH) },
"The password must be at least [" + MINIMUM_PASSWORD_LENGTH + "] characters in length.");
}
}
}
In your controller you would invoke the validator by doing something like
@PostMapping("/identity")
public void doPost(@Validated(Identity.class) @ModelAttribute UserLogin userLogin, BindingResult userLoginBindingResult) {
}
or
@PostMapping(value = "/identity", accepts = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public void doPost(@Validated(Identity.class) @RequestBody UserLogin userLogin) {
}
Circling back to this - does the Spring team feel this would be a useful contribution? The big thing this enhancement provides is the ability for writing Validator
s that can do cross-attribute validation using the JSR-303 group concept.
After introducing some typed factory methods for programmatic Validator
implementations in #30341, I don't see us extending this to validation groups through annotation-based dispatching. However, we could consider a convenient mechanism for Class-based differentiation in a programmatic validator implementation.
Generally speaking, Validator
serves in an SPI role (which is where the supports
method comes from) as well as a programmatic role these days. While we do not mean to turn this into a full-fledged user programming model, there is certainly room for further improvement in that direction.
Eric Deandrea opened SPR-15483 and commented
I've had this feature in my own codebase for quite some time & I'm looking to potentially contribute it back to Spring. I wanted to start this discussion first before I go through all the "hoops" of submitting a pull request to see if it would be wanted. This is the javadoc from the code:
No further details from SPR-15483