Possible duplicate:
Using the Hibernate Validator without invoking the annotation.
I have an annotation for composite constraint (for illustration only):
@Target... @Retention... @Constraint(validatedBy = {}) @Pattern(regexp = PasswordComplexity.AT_LEAST_TWO_NONE_ALPAH_CHARS) @Length(min = 6, max = 20) public @interface PasswordComplexity { ... }
And I use it in Spring controllers and object classes.
But now I need to check one String in the service method, where I need to apply the same restriction to one String. Because the constraint is the same, I want to use the same constraint definition (@PasswordComplexity) (the only source of truth). Something like:
public void createUser(UserDto userDto, String password) { if(hasViolation(validator.validate(password,PasswordComplexity.class))) { throw new PasswordComplexityViolationException(); } else { โฆ } }
But I do not know how to run the JSR 303 Validator on a non-annotated simple object (String). Is this possible, and how?
(I use Hibernate Validator as a JSR 303 provider)
java spring hibernate bean-validation
Ralph
source share