Top Inners Fields Constructors Methods
org.hibernate.validator.internal.engine.constraintvalidation

public Class CrossParameterConstraintValidatorContextImpl

extends ConstraintValidatorContextImpl
implements HibernateCrossParameterConstraintValidatorContext
Class Inheritance
All Implemented Interfaces
org.hibernate.validator.constraintvalidation.HibernateCrossParameterConstraintValidatorContext, org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext, jakarta.validation.ConstraintValidatorContext
Imports
java.util.List, jakarta.validation.ClockProvider, .ElementKind, jakarta.validation.metadata.ConstraintDescriptor, org.hibernate.validator.constraintvalidation.HibernateConstraintViolationBuilder, .HibernateCrossParameterConstraintValidatorContext, org.hibernate.validator.internal.engine.path.PathImpl, org.hibernate.validator.internal.util.Contracts, org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel

Author
Marko Bekhta

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private final List<String>

Constructor Summary

AccessConstructor and Description
public
CrossParameterConstraintValidatorContextImpl(List<String> methodParameterNames, ClockProvider clockProvider, PathImpl propertyPath, ConstraintDescriptor<?> constraintDescriptor, Object constraintValidatorPayload, ExpressionLanguageFeatureLevel constraintExpressionLanguageFeatureLevel, ExpressionLanguageFeatureLevel customViolationExpressionLanguageFeatureLevel)

Method Summary

Modifier and TypeMethod and Description
public final HibernateConstraintViolationBuilder
buildConstraintViolationWithTemplate(String
new un-interpolated constraint message
messageTemplate
)

Overrides org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl.buildConstraintViolationWithTemplate.

Implements org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext.buildConstraintViolationWithTemplate.

Returns a constraint violation builder building a violation report allowing to optionally associate it to a sub path.
public List<String>
public <T> T
unwrap(Class<T>
the class of the object to be returned
type
)

Overrides org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl.unwrap.

Implements jakarta.validation.ConstraintValidatorContext.unwrap.

Returns an instance of the specified type allowing access to provider-specific APIs.
Inherited from org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl:
addExpressionVariableaddMessageParameterdisableDefaultConstraintViolationgetClockProvidergetConstraintDescriptorgetConstraintValidatorPayloadgetConstraintViolationCreationContextsgetCopyOfBasePathgetDefaultConstraintMessageTemplatewithDynamicPayload

Field Detail

methodParameterNamesback to summary
private final List<String> methodParameterNames

Constructor Detail

CrossParameterConstraintValidatorContextImplback to summary
public CrossParameterConstraintValidatorContextImpl(List<String> methodParameterNames, ClockProvider clockProvider, PathImpl propertyPath, ConstraintDescriptor<?> constraintDescriptor, Object constraintValidatorPayload, ExpressionLanguageFeatureLevel constraintExpressionLanguageFeatureLevel, ExpressionLanguageFeatureLevel customViolationExpressionLanguageFeatureLevel)

Method Detail

buildConstraintViolationWithTemplateback to summary
public final HibernateConstraintViolationBuilder buildConstraintViolationWithTemplate(String messageTemplate)

Overrides org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl.buildConstraintViolationWithTemplate.

Implements org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext.buildConstraintViolationWithTemplate.

Doc from jakarta.validation.ConstraintValidatorContext.buildConstraintViolationWithTemplate.

Returns a constraint violation builder building a violation report allowing to optionally associate it to a sub path. The violation message will be interpolated.

To create the ConstraintViolation, one must call either one of the addConstraintViolation() methods available in one of the interfaces of the fluent API. If another method is called after addConstraintViolation() on ConstraintViolationBuilder or any of its associated nested interfaces an IllegalStateException is raised.

If ConstraintValidator#isValid(Object, ConstraintValidatorContext) returns false, a ConstraintViolation object will be built per constraint violation report including the default one (unless disableDefaultConstraintViolation() has been called).

ConstraintViolation objects generated from such a call contain the same contextual information (root bean, path and so on) unless the path has been overridden.

To create a different ConstraintViolation, a new constraint violation builder has to be retrieved from ConstraintValidatorContext Here are a few usage examples:

//assuming the following domain model
public class User {
    public Map<String,Address> getAddresses() { ... }
}

public class Address {
    public String getStreet() { ... }
    public Country getCountry() { ... }
}

public class Country {
    public String getName() { ... }
}

//From a property-level constraint on User.addresses
//Build a constraint violation on the default path - i.e. the "addresses" property
context.buildConstraintViolationWithTemplate( "this detail is wrong" )
            .addConstraintViolation();

//From a class level constraint on Address
//Build a constraint violation on the default path + "street"
//i.e. the street property of Address
context.buildConstraintViolationWithTemplate( "this detail is wrong" )
            .addPropertyNode( "street" )
            .addConstraintViolation();

//From a property-level constraint on  User.addresses
//Build a constraint violation on the default path + the bean stored
//under the "home" key in the map
context.buildConstraintViolationWithTemplate( "Incorrect home address" )
            .addBeanNode()
                .inContainer( Map.class, 1 )
                .inIterable().atKey( "home" )
            .addConstraintViolation();

//From a class level constraint on User
//Build a constraint violation on the default path + addresses["home"].country.name
//i.e. property "country.name" on the object stored under "home" in the map
context.buildConstraintViolationWithTemplate( "this detail is wrong" )
            .addPropertyNode( "addresses" )
            .addPropertyNode( "country" )
                .inContainer( Map.class, 1 )
                .inIterable().atKey( "home" )
            .addPropertyNode( "name" )
            .addConstraintViolation();

//From a class level constraint on User
//Build a constraint violation on the default path + addresses["home"].<map key>
//i.e. a container element constraint violation for the map key
context.buildConstraintViolationWithTemplate( "the map key is invalid" )
            .addPropertyNode( "addresses" )
            .addContainerElementNode( "<map key>", Map.class, 0 )
                .inIterable().atKey( "invalid" )
            .addConstraintViolation();

Cross-parameter constraints on a method can create a node specific to a particular parameter if required. Let's explore a few examples:

//Cross-parameter constraint on method
//createUser(String password, String passwordRepeat)
//Build a constraint violation on the default path + "passwordRepeat"
context.buildConstraintViolationWithTemplate("Passwords do not match")
            .addParameterNode(1)
            .addConstraintViolation();

//Cross-parameter constraint on a method
//mergeAddresses(Map<String,Address> addresses,
//        Map<String,Address> otherAddresses)
//Build a constraint violation on the default path + "otherAddresses["home"]
//i.e. the Address bean hosted in the "home" key of the "otherAddresses" map parameter
context.buildConstraintViolationWithTemplate(
        "Map entry home present in both and does not match")
            .addParameterNode(1)
            .addBeanNode()
                .inContainer( Map.class, 1 )
                .inIterable().atKey("home")
            .addConstraintViolation();

//Cross-parameter constraint on a method
//mergeAddresses(Map<String,Address> addresses,
//        Map<String,Address> otherAddresses)
//Build a constraint violation on the default path + "otherAddresses["home"].city
//i.e. on the "city" property of the Address bean hosted in
//the "home" key of the "otherAddresses" map
context.buildConstraintViolationWithTemplate(
        "Map entry home present in both but city does not match")
            .addParameterNode(1)
            .addPropertyNode("city")
                .inContainer( Map.class, 1 )
                .inIterable().atKey("home")
            .addConstraintViolation();
Parameters
messageTemplate:String

new un-interpolated constraint message

Returns:HibernateConstraintViolationBuilder

returns a constraint violation builder

Annotations
@Override
getMethodParameterNamesback to summary
public List<String> getMethodParameterNames()

Implements org.hibernate.validator.constraintvalidation.HibernateCrossParameterConstraintValidatorContext.getMethodParameterNames.

Returns:List<String>

Doc from org.hibernate.validator.constraintvalidation.HibernateCrossParameterConstraintValidatorContext.getMethodParameterNames.

the list of the parameter names of the validated method.

Annotations
@Override
unwrapback to summary
public <T> T unwrap(Class<T> type)

Overrides org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl.unwrap.

Implements jakarta.validation.ConstraintValidatorContext.unwrap.

Doc from jakarta.validation.ConstraintValidatorContext.unwrap.

Returns an instance of the specified type allowing access to provider-specific APIs. If the Jakarta Bean Validation provider implementation does not support the specified class, ValidationException is thrown.

Parameters
<T>
the type of the object to be returned
type:Class<T>

the class of the object to be returned

Returns:T

an instance of the specified class

Annotations
@Override
org.hibernate.validator.internal.engine.constraintvalidation back to summary

private Class CrossParameterConstraintValidatorContextImpl.CrossParameterConstraintViolationBuilderImpl

extends ConstraintViolationBuilderImpl
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final List<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext
private void
Inherited from org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl.ConstraintViolationBuilderImpl:
addBeanNodeaddContainerElementNodeaddNodeaddPropertyNodeenableExpressionLanguage

Field Detail

methodParameterNamesback to summary
private final List<String> methodParameterNames

Constructor Detail

CrossParameterConstraintViolationBuilderImplback to summary
private CrossParameterConstraintViolationBuilderImpl(List<String> methodParameterNames, String template, PathImpl path)

Method Detail

addParameterNodeback to summary
public ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext addParameterNode(int index)

Overrides org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl.ConstraintViolationBuilderImpl.addParameterNode.

Implements jakarta.validation.ConstraintValidatorContext.ConstraintViolationBuilder.addParameterNode.

Doc from jakarta.validation.ConstraintValidatorContext.ConstraintViolationBuilder.addParameterNode.

Adds a method parameter node to the path the ConstraintViolation will be associated to. The parameter index must be valid (i.e. within the boundaries of the method parameter indexes). May only be called from within cross-parameter validators.

Parameters
index:int

the parameter index

Returns:ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderDefinedContext

a builder representing the index-th parameter node

Annotations
@Override
dropLeafNodeback to summary
private void dropLeafNode()