No validator could be found for constraint for Custom Java Bean Validator

5k views Asked by At

I created a new Custom Validator using the instructions here: https://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-customconstraints.html#validator-customconstraints-validator

public class CustomizableTypeValidator implements ConstraintValidator<CustomValidate, CustomValidator> {
    @Override
    public void initialize(final CustomValidate customValidate) {
    }

    @Override
    public boolean isValid(final CustomValidator customValidator, final ConstraintValidatorContext constraintValidatorContext) {
        if ( customValidator == null ) {
            return true;
        }

        return customValidator.isValid(constraintValidatorContext);
    }
}

but getting :

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'com.amazon.mft.data.api.v1.CustomValidate' validating type 'com.amazon.mft.data.api.v1.CustomValidator'. Check configuration for 'transactionEntries[0].record.attributesObj'

I am creating a validor using:

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();

and calling it:

 validator.validate(obj)

What do I need to do the add the custom validator to the other validations it's doing. I am using Hibernate Validator 5.2.4.

1

There are 1 answers

0
Gaurav Jain On BEST ANSWER

Answering my own question: adding AutoService annotation fixes it:

import com.google.auto.service.AutoService;

@AutoService(ConstraintValidator.class)
public class CustomizableTypeValidator implements ConstraintValidator<CustomValidate, CustomValidator> {
    @Override
    public void initialize(final CustomValidate customValidate) {
    }

    @Override
    public boolean isValid(final CustomValidator customValidator, final ConstraintValidatorContext constraintValidatorContext) {
        if ( customValidator == null ) {
            return true;
        }

        return customValidator.isValid(constraintValidatorContext);
    }
}