Skip to content

SmartValidator which supports JSR-303 validation groups [SPR-15483] #20043

Open
@spring-projects-issues

Description

@spring-projects-issues

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 &lt;T&gt;, 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&lt;UserLogin&gt; {
 	private static final int MINIMUM_PASSWORD_LENGTH = 6;

 	public interface Identity {
 	}

 	public interface Secret {
 	}

	&#064;ValidationGroup(Identity.class)
	public void validateUserName(UserLogin login, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, &quot;userName&quot;, &quot;field.required&quot;);
	}

	&#064;ValidationGroup(Secret.class)
	public void validatePassword(UserLogin login, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, &quot;password&quot;, &quot;field.required&quot;);

		if (login.getPassword() != null &amp;&amp; login.getPassword().trim().length() &lt; MINIMUM_PASSWORD_LENGTH) {
			errors.rejectValue(&quot;password&quot;, &quot;field.min.length&quot;, new Object[] { Integer.valueOf(MINIMUM_PASSWORD_LENGTH) },
				&quot;The password must be at least [&quot; + MINIMUM_PASSWORD_LENGTH + &quot;] characters in length.&quot;);
		}
	}
}</code></pre>
<p>You would then &quot;run&quot; 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>
	&#064;PostMapping("/identity")
	public void postIdentity(&#064;Validated(Identity.class) &#064;ModelAttribute UserLogin login)
</code></pre>
<p>or this (in a {@link org.springframework.web.bind.annotation.RestController RestController}):
<pre><code>
	&#064;PostMapping("/identity")
	public void postIdentity(&#064;Validated(Identity.class) &#064;RequestBody UserLogin login)
</code></pre>
 */

No further details from SPR-15483

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions