Class ConstraintValidatorContextImpl

java.lang.Object
org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl
All Implemented Interfaces:
jakarta.validation.ConstraintValidatorContext, HibernateConstraintValidatorContext
Direct Known Subclasses:
CrossParameterConstraintValidatorContextImpl

public class ConstraintValidatorContextImpl extends Object implements HibernateConstraintValidatorContext
Author:
Hardy Ferentschik, Gunnar Morling, Guillaume Smet
  • Constructor Details

    • ConstraintValidatorContextImpl

      public ConstraintValidatorContextImpl(jakarta.validation.ClockProvider clockProvider, PathImpl propertyPath, jakarta.validation.metadata.ConstraintDescriptor<?> constraintDescriptor, Object constraintValidatorPayload, ExpressionLanguageFeatureLevel defaultConstraintExpressionLanguageFeatureLevel, ExpressionLanguageFeatureLevel defaultCustomViolationExpressionLanguageFeatureLevel)
  • Method Details

    • disableDefaultConstraintViolation

      public final void disableDefaultConstraintViolation()
      Specified by:
      disableDefaultConstraintViolation in interface jakarta.validation.ConstraintValidatorContext
    • getDefaultConstraintMessageTemplate

      public final String getDefaultConstraintMessageTemplate()
      Specified by:
      getDefaultConstraintMessageTemplate in interface jakarta.validation.ConstraintValidatorContext
    • buildConstraintViolationWithTemplate

      public HibernateConstraintViolationBuilder buildConstraintViolationWithTemplate(String messageTemplate)
      Specified by:
      buildConstraintViolationWithTemplate in interface jakarta.validation.ConstraintValidatorContext
      Specified by:
      buildConstraintViolationWithTemplate in interface HibernateConstraintValidatorContext
    • unwrap

      public <T> T unwrap(Class<T> type)
      Specified by:
      unwrap in interface jakarta.validation.ConstraintValidatorContext
    • addExpressionVariable

      public HibernateConstraintValidatorContext addExpressionVariable(String name, Object value)
      Description copied from interface: HibernateConstraintValidatorContext
      Allows to set an additional expression variable which will be available as an EL variable during interpolation. The variable will be available for interpolation for all constraint violations generated for this constraint. This includes the default one as well as all violations created by the ConstraintValidatorContext.ConstraintViolationBuilder. To create multiple constraint violations with different variable values, this method can be called between successive calls to ConstraintValidatorContext.ConstraintViolationBuilder.addConstraintViolation().

      For example:

       
       public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
           HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class );
      
           context.addExpressionVariable( "foo", "bar" );
           context.buildConstraintViolationWithTemplate( "${foo}" )
                  .addConstraintViolation();
      
           context.addExpressionVariable( "foo", "snafu" );
           context.buildConstraintViolationWithTemplate( "${foo}" )
                  .addConstraintViolation();
      
           return false;
        }
        
      
       
      Specified by:
      addExpressionVariable in interface HibernateConstraintValidatorContext
      Parameters:
      name - the name under which to bind the expression variable, cannot be null
      value - the value to be bound to the specified name
      Returns:
      a reference to itself to allow method chaining
    • addMessageParameter

      public HibernateConstraintValidatorContext addMessageParameter(String name, Object value)
      Description copied from interface: HibernateConstraintValidatorContext
      Allows to set an additional named parameter which can be interpolated in the constraint violation message. The variable will be available for interpolation for all constraint violations generated for this constraint. This includes the default one as well as all violations created by the ConstraintValidatorContext.ConstraintViolationBuilder. To create multiple constraint violations with different variable values, this method can be called between successive calls to ConstraintValidatorContext.ConstraintViolationBuilder.addConstraintViolation().

      For example:

       
       public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
           HibernateConstraintValidatorContext context = constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class );
      
           context.addMessageParameter( "foo", "bar" );
           context.buildConstraintViolationWithTemplate( "{foo}" )
                  .addConstraintViolation();
      
           context.addMessageParameter( "foo", "snafu" );
           context.buildConstraintViolationWithTemplate( "{foo}" )
                  .addConstraintViolation();
      
           return false;
        }
        
      
       
      Specified by:
      addMessageParameter in interface HibernateConstraintValidatorContext
      Parameters:
      name - the name under which to bind the parameter, cannot be null
      value - the value to be bound to the specified name
      Returns:
      a reference to itself to allow method chaining
    • getClockProvider

      public jakarta.validation.ClockProvider getClockProvider()
      Specified by:
      getClockProvider in interface jakarta.validation.ConstraintValidatorContext
    • withDynamicPayload

      public HibernateConstraintValidatorContext withDynamicPayload(Object violationContext)
      Description copied from interface: HibernateConstraintValidatorContext
      Allows to set an object that may further describe the violation. The user is responsible himself to ensure that this payload is serializable in case the jakarta.validation.ConstraintViolation has to be serialized.
      Specified by:
      withDynamicPayload in interface HibernateConstraintValidatorContext
      Parameters:
      violationContext - an object representing additional information about the violation
      Returns:
      a reference to itself to allow method chaining
    • getConstraintValidatorPayload

      public <C> C getConstraintValidatorPayload(Class<C> type)
      Description copied from interface: HibernateConstraintValidatorContext
      Returns an instance of the specified type or null if the current constraint validator payload isn't of the given type.
      Specified by:
      getConstraintValidatorPayload in interface HibernateConstraintValidatorContext
      Parameters:
      type - the type of payload to retrieve
      Returns:
      an instance of the specified type or null if the current constraint validator payload isn't of the given type
      See Also:
    • getConstraintDescriptor

      public final jakarta.validation.metadata.ConstraintDescriptor<?> getConstraintDescriptor()
    • getConstraintViolationCreationContexts

      public final List<ConstraintViolationCreationContext> getConstraintViolationCreationContexts()
    • getCopyOfBasePath

      protected final PathImpl getCopyOfBasePath()