How to obtain ConstraintValidatorContext?
Asked Answered
M

3

7

I am writing code that has explicit call to Bean Validation (JSR-303) something like this:

public class Example {

    @DecimalMin(value = "0")
    private static final String ANNOTATED = "";

    public void isPossitiveNumber(String str){

        ValidatorFactory factory =
             Validation.buildDefaultValidatorFactory();

        ConstraintValidator<DecimalMin, String>
             validator = 
                  factory.getConstraintValidatorFactory().getInstance(
                          DecimalMinValidatorForString.class);


        validator.initialize(
                  ReflectionUtils.findField(getClass(), "ANNOTATED")
                         .getAnnotation(
                          DecimalMin.class));

        boolean isValid = validator.isValid(str, null);

        return isValid;


    }


}

Note the line boolean isValid = validator.isValid(str, null); I transfer null for ConstraintValidatorContext because I found no way to obtain/construct it. In this particular case, this if fine, because there is no use of the ConstraintValidatorContext internally, but it is obvious a hack. How should I get ConstraintValidatorContext?

ADDED

I was asked to provide use-cases. So, for example, I am writting custom validator and I want to reuse exisiting validations. Or I am writting plane Java code as desribed above and I want to reuse exisiting validation.

Madeira answered 14/3, 2013 at 12:49 Comment(2)
Could you add some details to your use case? As a user, you normally interact with Bean Validation through the javax.validation.Validator API, not by creating and invoking constraint validators by hand. ConstraintValidatorContext is passed by the engine to validators during validation, so its implementation is typically not exposed to users.Anthropomorphosis
For example, I am writting custom validator and I want to reuse exisiting validations. Or I am writting plane Java code as desribed above and I want to reuse exisiting validation. I will add this clarification to my post.Madeira
K
2

The simple answer is you cannot. ConstraintValidatorContext is an interface and there is no Bean Validation API to get an instance like this. You could write your own implementation, but to implement it properly you would have to re-implement a lot of functionality of a Bean Validation provider. Look for example at the Hibernate Validator specific implementation - https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/constraintvalidation/ConstraintValidatorContextImpl.java

That said, I believe your attempt of reuse is misguided. This is not in the indent of Bean Validation and you are ending up with non portable and hard to maintain code. If you want to reuse existing constraints have a look at constraint composition, for example @NotEmpty reusing @NotNull and @Size

@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
    String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    /**
     * Defines several {@code @NotEmpty} annotations on the same element.
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotEmpty[] value();
    }
} 
Kanchenjunga answered 15/3, 2013 at 9:45 Comment(3)
You are reusing the constraintsKanchenjunga
Not really. I can't call to validation form the plain code as above and I can't call to validation from custom validation in some complex flow.Madeira
could you please help me #67424685Forepaw
D
6

I recently had exactly the same issue as the OP. However contrary to the accepted answer it is possible to write Unit tests that include the ConstraintValidationContext. This excellent link explains how to do it, http://farenda.com/java/bean-validation-unit-testing/

Basically you need to use the ValidatorFactory to obtain a Validator interface, then call validate(c) on that interface, where the parameter c is an instance of the class containing the bean validation annotations. A code example is clearer, code sample taken from the above link.

   public class Player {

   // name have to be 3 chars:
   @Size(min = 3, max = 3)
   private String name;

   // possible score in game:
   @Min(0) @Max(100)
   private int score;

   public Player(String name, int score) {
      this.name = name;
      this.score = score;
   }

   // just for logs
   @Override
   public String toString() {
      return "Player{name='" + name + '\'' + ", score=" + score + '}';
   }
}

public class PlayerValidationTest {
   private static ValidatorFactory validatorFactory;
   private static Validator validator;

   @BeforeClass
   public static void createValidator() {
      validatorFactory = Validation.buildDefaultValidatorFactory();
      validator = validatorFactory.getValidator();
   }

   @AfterClass
   public static void close() {
      validatorFactory.close();
   }

   @Test
   public void shouldDetectInvalidName() {
      //given too short name:
      Player player = new Player("a", 44);

      //when:
      Set<ConstraintViolation<Player>> violations
            = validator.validate(player);

      //then:
      assertEquals(violations.size(), 1);
  }

}

Dragelin answered 13/6, 2018 at 10:42 Comment(1)
Looks like this link is dead. farenda.com/java/bean-validation-unit-testingSmyth
K
2

The simple answer is you cannot. ConstraintValidatorContext is an interface and there is no Bean Validation API to get an instance like this. You could write your own implementation, but to implement it properly you would have to re-implement a lot of functionality of a Bean Validation provider. Look for example at the Hibernate Validator specific implementation - https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/constraintvalidation/ConstraintValidatorContextImpl.java

That said, I believe your attempt of reuse is misguided. This is not in the indent of Bean Validation and you are ending up with non portable and hard to maintain code. If you want to reuse existing constraints have a look at constraint composition, for example @NotEmpty reusing @NotNull and @Size

@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
    String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    /**
     * Defines several {@code @NotEmpty} annotations on the same element.
     */
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    public @interface List {
        NotEmpty[] value();
    }
} 
Kanchenjunga answered 15/3, 2013 at 9:45 Comment(3)
You are reusing the constraintsKanchenjunga
Not really. I can't call to validation form the plain code as above and I can't call to validation from custom validation in some complex flow.Madeira
could you please help me #67424685Forepaw
A
0

You should declare a group for the constraints you validate in that case. Then you can call the normal validation for that group. See sections 2.1.1.2 and section 3.4 of the spec for group definitions and their semantics. For validating the group, you then just need to call Validator.validate(T Object, Class<?>... groups). There is no need to mess around with the ConstraintValidatorContext in this case.

Agueweed answered 14/3, 2013 at 14:31 Comment(3)
I want to reuse existing ConstraintValidator, not to process bean validation.Madeira
@Madeira From the documentation of ConstraintValidator: This method can be accessed concurrently, thread-safety must be ensured by the implementation. And the spec also states Compliant implementations are allowed to cache ConstraintValidator instances retrieved from the ConstraintValidatorFactory. (section 2.4)Agueweed
could you help plz #67424685Forepaw

© 2022 - 2024 — McMap. All rights reserved.