Search in sources :

Example 1 with HibernateConstraintValidatorContext

use of org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext in project fess by codelibs.

the class CustomSizeValidator method isValid.

@Override
public boolean isValid(final String value, final ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }
    final HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
    hibernateContext.disableDefaultConstraintViolation();
    hibernateContext.addExpressionVariable("min", min).addExpressionVariable("max", max).buildConstraintViolationWithTemplate(message).addConstraintViolation();
    final int length = value.length();
    return length >= min && length <= max;
}
Also used : HibernateConstraintValidatorContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext)

Example 2 with HibernateConstraintValidatorContext

use of org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext in project syndesis by syndesisio.

the class UniquePropertyValidatorTest method shouldAscertainPropertyUniqueness.

@Test
public void shouldAscertainPropertyUniqueness() {
    final HibernateConstraintValidatorContext context = mock(HibernateConstraintValidatorContext.class);
    when(context.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(context);
    when(context.addExpressionVariable(eq("nonUnique"), anyString())).thenReturn(context);
    when(context.getDefaultConstraintMessageTemplate()).thenReturn("template");
    final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate("template")).thenReturn(builder);
    when(builder.addPropertyNode(anyString())).thenReturn(mock(NodeBuilderCustomizableContext.class));
    assertThat(validator.isValid(connection, context)).isEqualTo(validity);
}
Also used : ConstraintViolationBuilder(javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder) HibernateConstraintValidatorContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext) NodeBuilderCustomizableContext(javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext) Test(org.junit.Test)

Aggregations

HibernateConstraintValidatorContext (org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext)2 ConstraintViolationBuilder (javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder)1 NodeBuilderCustomizableContext (javax.validation.ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext)1 Test (org.junit.Test)1