spring-projects / spring-projects/spring-framework
SmartValidator which supports JSR-303 validation groups [SPR-15483]
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
Description
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:
/**
* Extend this to create a Spring MVC {@link org.springframework.validation.Validator Validator} class which is capable of doing partial validations,
* using the <a href="http://beanvalidation.org/1.0/spec/#constraintdeclarationvalidationprocess-groupsequence">JSR 303 specification for groups</a>.
* <p>
* Custom validation methods must be declared as public void and can be given any name (other than <code>validate</code> or <code>supports</code>.
* They must take in two parameters: first a target instance of type <T>, followed by an {@link Errors} object. They can then optionally be assigned to a specific {@link ValidationGroup}.
* <p>
* Find below a variation of the {@link org.springframework.validation.Validator Validator} class's javadoc example where the userName and password properties can be validated in different actions of your <code>Controller</code>.
*
* <pre><code>
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.");
}
}
}</code></pre>
<p>You would then "run" a group by using Spring's {@link org.springframework.validation.annotation.Validated Validated} annotation in your controller action method, similar to this (in a standard {@link org.springframework.stereotype.Controller Controller}):
<pre><code>
@PostMapping("/identity")
public void postIdentity(@Validated(Identity.class) @ModelAttribute UserLogin login)
</code></pre>
<p>or this (in a {@link org.springframework.web.bind.annotation.RestController RestController}):
<pre><code>
@PostMapping("/identity")
public void postIdentity(@Validated(Identity.class) @RequestBody UserLogin login)
</code></pre>
*/
No further details from SPR-15483
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides a proposed GroupedValidator design and controller examples but names no repository files or tests. Start by reviewing Spring MVC validation and JSR-303 group support; done would require an accepted scope and corresponding implementation and tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100