Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/jakarta/validation/ConstraintValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old initialize method also has this line... but it's wrong, because the old initialize method will no longer be called if the new one is overridden.

* <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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added to the javadoc of the other initialize method as well.

*
* @param constraintDescriptor annotation instance for a given constraint declaration
* @param context context in which the constraint is initialized
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
* @since 4.0
*/

default void initialize(ConstraintDescriptor<A> constraintDescriptor, ConstraintValidatorInitializationContext context) {
initialize( constraintDescriptor.getAnnotation() );
}

/**
* Initializes the validator in preparation for
* {@link #isValid(Object, ConstraintValidatorContext)} calls.
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to alter the Configuration classes to allow setting the provider? Unless that's already there for other purposes?

* the current system time and the current default time zone as returned by
* {@link Clock#systemDefaultZone()} will be returned.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
* @since 4.0
*/

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);
}