-
Notifications
You must be signed in to change notification settings - Fork 66
[#271] Add an initialization context to the init method of the constraint validator #312
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |||||||
| import java.lang.annotation.Annotation; | ||||||||
|
|
||||||||
| import jakarta.validation.constraintvalidation.SupportedValidationTarget; | ||||||||
| import jakarta.validation.metadata.ConstraintDescriptor; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Defines the logic to validate a given constraint {@code A} | ||||||||
|
|
@@ -34,6 +35,26 @@ | |||||||
| */ | ||||||||
| public interface ConstraintValidator<A extends Annotation, T> { | ||||||||
|
|
||||||||
| /** | ||||||||
| * Initializes the validator in preparation for | ||||||||
| * {@link #isValid(Object, ConstraintValidatorContext)} calls. | ||||||||
| * The constraint annotation descriptor for a given constraint declaration is passed. | ||||||||
| * <p> | ||||||||
| * This method is guaranteed to be called before any use of this instance for validation. | ||||||||
| * <p> | ||||||||
| * The default implementation is a no-op. | ||||||||
| * <p> | ||||||||
| * Validation providers are expected to call this method only. {@code ConstraintValidator}s should implement only one of the initialize methods. | ||||||||
| * Either this one, which will be directly called by the validation provider, or the {@link #initialize(Annotation)}, | ||||||||
| * which will be called through the default implementation of the current method. | ||||||||
|
Comment on lines
+47
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be added to the javadoc of the other |
||||||||
| * | ||||||||
| * @param constraintDescriptor annotation instance for a given constraint declaration | ||||||||
| * @param context context in which the constraint is initialized | ||||||||
| */ | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| default void initialize(ConstraintDescriptor<A> constraintDescriptor, ConstraintValidatorInitializationContext context) { | ||||||||
| initialize( constraintDescriptor.getAnnotation() ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Initializes the validator in preparation for | ||||||||
| * {@link #isValid(Object, ConstraintValidatorContext)} calls. | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||
| /* | ||||||||
| * Jakarta Validation API | ||||||||
| * | ||||||||
| * License: Apache License, Version 2.0 | ||||||||
| * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||||||||
| */ | ||||||||
| package jakarta.validation; | ||||||||
|
|
||||||||
| import java.time.Clock; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Provides contextual data and operations when initializing a constraint validator. | ||||||||
| * | ||||||||
| * @author Marko Bekhta | ||||||||
| * @since 4.0 | ||||||||
| */ | ||||||||
| public interface ConstraintValidatorInitializationContext { | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns the provider for obtaining the current time in the form of a {@link Clock}, | ||||||||
| * e.g. when validating the {@code Future} and {@code Past} constraints. | ||||||||
| * | ||||||||
| * @return the provider for obtaining the current time, never {@code null}. If no | ||||||||
| * specific provider has been configured during bootstrap, a default implementation using | ||||||||
|
Comment on lines
+23
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably need to alter the |
||||||||
| * the current system time and the current default time zone as returned by | ||||||||
| * {@link Clock#systemDefaultZone()} will be returned. | ||||||||
| */ | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| ClockProvider getClockProvider(); | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns an instance of the specified type allowing access to | ||||||||
| * provider-specific APIs. If the Jakarta Validation provider | ||||||||
| * implementation does not support the specified class, | ||||||||
| * {@link ValidationException} is thrown. | ||||||||
| * | ||||||||
| * @param type the class of the object to be returned | ||||||||
| * @param <T> the type of the object to be returned | ||||||||
| * @return an instance of the specified class | ||||||||
| * @throws ValidationException if the provider does not support the call | ||||||||
| * @since 4.0 | ||||||||
| */ | ||||||||
| <T> T unwrap(Class<T> type); | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old
initializemethod also has this line... but it's wrong, because the oldinitializemethod will no longer be called if the new one is overridden.