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

volidator不同的类不能复用吗 #22

Closed 12266601032 closed 7 years ago

12266601032 commented 7 years ago

image

neoremind commented 7 years ago

是可以复用的,这里仅仅是为了做一个单例的Validator,保证全局有且仅有一个该类型的Validator。这需要开发人员保证Validator是thread-safe的。

12266601032 commented 7 years ago

CLASS_2_ANNOTATION_VALIDATOR_MAP是根据待验证类的class缓存的该类里的所有validator 因为上面那个问题导致 CLASS_2_ANNOTATION_VALIDATOR_MAP 最后存的validators是空的,然后在执行doValidate时 导致执行不到@FluentValidate指定的验证类

neoremind commented 7 years ago

可以贴出来复现问题的代码吗?

12266601032 commented 7 years ago

package com.baidu.unbiz.fluentvalidator;

import org.junit.Test;

import com.baidu.unbiz.fluentvalidator.annotation.FluentValidate;

public class ValidatorTest {

@Test
public void testReuseValidator(){

    CarA acar = new CarA();
    CarB bcar = new CarB();
    FluentValidator.checkAll().on(acar).doValidate().result(ResultCollectors.toSimple());
    FluentValidator.checkAll().on(bcar).doValidate().result(ResultCollectors.toSimple());

}

public class CarA{
    @FluentValidate({NameValidator.class})
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
public class CarB{
    @FluentValidate({NameValidator.class})
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
public static class NameValidator implements Validator<String>{

    @Override
    public boolean accept(ValidatorContext context, String t) {
        return true;
    }

    @Override
    public boolean validate(ValidatorContext context, String t) {
        System.out.println("..........");
        return true;
    }

    @Override
    public void onException(Exception e, ValidatorContext context, String t) {

    }

}

}

12266601032 commented 7 years ago

在验证bcar时 就不执行NameValidator了

12266601032 commented 7 years ago

image

neoremind commented 7 years ago

I will look into the issue later.

neoremind commented 7 years ago

Great catch! I am enable to reproduce the problem, your solution works, would you mind to create a PR? Or I can update the issue by myself. Thanks for you finding!

neoremind commented 7 years ago

Fixed. So close the issue.