0

I am using spring-boot 1.4.0 and hibernate-validator 5.2.0. I have a model which contains custom validator inside the custom validator i want to check whether the property value is a valid URL for that i need to call URLValidator in hibernate but no luck.Could anyone please guide me to resolve this issue

CustomValidator.java

@Component
public class BookValidator extends GenericValidator<Book,    ConstraintValidatorContext> implements ConstraintValidator<ValidBooks,  List<Book>> {


public BookValidator() {
    addValidators();
}

private void addValidators() {
 getValidators().add((book, context) -> {
        boolean isValid = book.getUrl(); //here i want to check against Hibernate URL validator 
        if (!isValid) {
            context.disableDefaultConstraintViolation();
            context
                    .buildConstraintViolationWithTemplate("Book URL should be valid!")
                    .addConstraintViolation();
        }
        return isValid;
    });
}
@Override
public void initialize(ValidBooks constraintAnnotation) {
}
}

How can i check whether the URL is a valid one boolean isValid = book.getUrl(); using hibernate URLValidator?

1 Answer 1

2

This works:

AnnotationDescriptor<URL> descriptor = new AnnotationDescriptor<URL>( URL.class ); 
URL url = AnnotationFactory.create(descriptor);
URLValidator urlValidator = new URLValidator();
urlValidator.initialize(url);

boolean isValid = urlValidator.isValid(book.getUrl(), context);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.