-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SmartValidator which supports JSR-303 validation groups [SPR-15483] #20043
Comments
Feliks Khantsis commented what are you talking about? Validation groups have been supported since forever. public class Book {
} |
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 |
After introducing some typed factory methods for programmatic Generally speaking, |
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
The text was updated successfully, but these errors were encountered: