title: javax.validation.UnexpectedTypeException using Spring ConstraintValidator for an enumeration
body: <p>I have implemented a ConstraintValidator in order to valide a DTO that contains an enumeration. I followed <a href="https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html#validation-beanvalidation-spring-constraints" rel="nofollow noreferrer">this Spring documentation</a> for that.</p>
  
  <p>This is the custom annotation to be applied to the enum field:</p>
  
  <pre><code>@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR,
      ElementType.ANNOTATION_TYPE, ElementType.PARAMETER})
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy = EnumValidator.class)
  public @interface ValidEnum {
  
    String message() default "{com.test.validation.constraints.ValidEnum}";
  
    Class&lt;?&gt;[] groups() default {};
  
    Class&lt;? extends Payload&gt;[] payload() default {};
  
    Class&lt;? extends Enum&lt;?&gt;&gt; target();
  
  }
  </code></pre>
  
  <p>The <code>EnumValidator</code> looks like this:</p>
  
  <pre><code>public class EnumValidator implements ConstraintValidator&lt;ValidEnum,String&gt; {
  
        private Set&lt;String&gt; enumValues;
  
        @Override
        public void initialize(ValidEnum targetEnum) {
            Class&lt;? extends Enum&gt; enumSelected = targetEnum.targetClassType();
            enumValues = (Set&lt;String&gt;) EnumSet.allOf(enumSelected).stream()
                .map(e -&gt; ((Enum&lt;? extends Enum&lt;?&gt;&gt;) e).name()).collect(Collectors
                    .toSet());
        }
  
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return enumValues.contains(value);
        }
    }
  </code></pre>
  
  <p>This is the enum:</p>
  
  <pre><code>public enum Gender {
    MALE,FEMALE;
  }
  </code></pre>
  
  <p>This is the DTO to validate:</p>
  
  <pre><code>public final class UserDTO{
  
    @ValidEnum(target = Gender.class)
    private Gender gender;
  
    @NotEmpty
    @Max(100)
    private String fullName;
  }
  </code></pre>
  
  <p>And the controller that is validating the field:</p>
  
  <pre><code>@Controller
  public class RegistrationController {
  
    private static final String REGISTER_USER = "/register";
  
    private final RegistrationService registrationService;
  
  
    @PostMapping(value = REGISTER_USER)
    @Consumes(APPLICATION_JSON)
    @Produces(APPLICATION_JSON)
    public UserRegistrationResponse register(@Valid UserDTO userRegistrationRequest) {
  
      return registrationService.register(userRegistrationRequest);
    }
  
  }
  </code></pre>
  
  <p>It seems that Spring is not detecting the validator, because it throws this exception:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; 
  nested exception is javax.validation.UnexpectedTypeException: HV000030: 
  
  No validator could be found for constraint 'com.test.ws.web.validation.ValidEnumType' validating type 'com.test.ws.domain.model.Gender'. Check configuration for 'gender'
  </code></pre>
  
  <p>I am using Spring boot 2.0.4 which include the required dependencies for validation.</p>
  
  <p>Any idea why it fails?</p>