neoremind / fluent-validator

A Java validation framework leveraging fluent interface style and JSR 303 specification
Apache License 2.0
1.02k stars 222 forks source link

和HibernateValidator集成使用时 每次都需要 setHibernateValidator 太过麻烦 #30

Open xiaolongyuan opened 6 years ago

xiaolongyuan commented 6 years ago
        Result ret = FluentValidator.checkAll()
                .on(company, new HibernateSupportedValidator<Company>().setHibernateValidator(validator))
                .on(company, new CompanyCustomValidator())
                .doValidate().result(toSimple());

另外

public class HibernateSupportedValidator<T> extends ValidatorHandler<T> implements Validator<T> {
    private static javax.validation.Validator HIBERANTE_VALIDATOR;
    private int hibernateDefaultErrorCode;
    private ConstraintViolationTransformer constraintViolationTransformer = new DefaultConstraintViolationTransformer();

HIBERANTE_VALIDATOR 为static 何解?

neoremind commented 6 years ago

1) Because Fluent-validator is not bounded to hibernate validator, so it is intend to be created manually. If you think setHibernateValidator is redundant, I suggest using Factory design pattern to build it, note that HibernateSupportedValidator is not thread-safe, must be prototyped.

2) Validator instance is retrieved from the ValidatorFactory. Validator instances are thread-safe and may be reused multiple times. This is how we use Hibernate in the right way.