JSR303 - 应用按顺序定义的所有验证组 [英] JSR303 - Apply all validation-groups defined in sequence

查看:185
本文介绍了JSR303 - 应用按顺序定义的所有验证组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要进行条件验证的bean。为此,我定义了一个 DefaultGroupSequenceProvider< MyObject> ,它返回要验证的组列表。

I've got a bean I'd like to do conditional validation on. For this purpose, I've defined a DefaultGroupSequenceProvider<MyObject> which returns the list of groups to validate against.

现在,在验证违反序列中多个组中的约束的对象时,只有第一个有失败的组才会返回结果。我想在所有违规行为中收到错误,而不仅仅是第一组失败的错误。

Now, when validating an object that violates the constraints in more than one group in the sequence, only the first group with failures return it's result. I'd like to get an error on all violations, not just the ones in the first group with failures.

我认为这不需要代码 - 例如,但如果我错了,我会乐意提供一个。

I'm thinking that this doesn't need a code-sample, but if I'm wrong, I'll be happy to provide one.

我跟着这个 http://kh-yiu.blogspot.com/2014/04/conditional-bean-validation-using.html 创建序列时。如果重要,我们使用Spring。

I followed this http://kh-yiu.blogspot.com/2014/04/conditional-bean-validation-using.html when creating the sequence. We use Spring if that matters.

只需注意,这是有效的,因为无效的bean不可能被报告为有效。但是如果用户有一些输入打破3个约束,并且我返回2个失败,则用户将在第一个字段被修正后立即在最后一个字段上再次失败。不完全是用户友好的。

Just a note, this works, in that it is impossible that an invalid bean will be reported as valid. But if the user has some input that breaks 3 constraints, and I return back 2 failures, the user will get another failure on the last field as soon as the first ones are corrected. Not exactly user-friendly.

示例:

Bean

@GroupSequenceProvider(BeanSequenceProvider.class)
public class MyBean {
    @NotEmpty
    private String name;

    @NotNull
    private MyType type;

    @NotEmpty(groups = Special.class)
    private String lastName;

    // Getters and setters        
}

枚举类型

public enum MyType {
    FIRST, SECOND
}

提供商

public class BeanSequenceProvider implements DefaultGroupSequenceProvider<MyBean> {
    @Override
    public List<Class<?>> getValidationGroups(final MyBean object) {
        final List<Class<?>> classes = new ArrayList<>();

        classes.add(MyBean.class);

        if (object != null && object.getType() == MyType.SECOND) {
            classes.add(Special.class);
        }

        return classes;
    }

组注释

public interface Special {
}

测试类

public class MyBeanTest {

    private static Validator validator;

    private MyBean objectUnderTest;

    @BeforeClass
    public static void setUpOnce() throws Exception {
        final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

        validator = validatorFactory.getValidator();
    }

    @Before
    public void setUp() throws Exception {
        objectUnderTest = new MyBean();

        objectUnderTest.setName("Simen");
        objectUnderTest.setType(MyType.FIRST);
        objectUnderTest.setLastName("Woop");
    }

    @Test
    public void testValid() throws Exception {
        assertThat(validator.validate(objectUnderTest), is(empty()));
    }

    @Test
    public void testMissingName() throws Exception {
        objectUnderTest.setName(null);

        assertThat(validator.validate(objectUnderTest), hasSize(1));
    }

    @Test
    public void testMissingLastName() throws Exception {
        objectUnderTest.setLastName(null);

        assertThat(validator.validate(objectUnderTest), is(empty()));

        objectUnderTest.setType(MyType.SECOND);

        assertThat(validator.validate(objectUnderTest), hasSize(1));

        objectUnderTest.setName(null);

        assertThat(validator.validate(objectUnderTest), hasSize(2));
    }

最后一次断言失败,因为有一次违规,而不是2.作为默认组中违反了约束,未违反特殊组。

That very last assert fail, as there's one violation, not 2. As the constraint is violated in the default group, the Special group is not violated.

推荐答案

好的,现在我理解你的问题。答案是,如果给定组中存在一个或多个违规,则验证将停止。引用规范:

Ok, now I understand your question. The answer is that validation stops if there are one or more violations within a given group. To quote the spec:


处理组在第4.6节验证例程中定义;
如果序列中处理的其中一个组生成一个或多个
约束违规,则序列中的后续组不得处理
。这确保了如果另一组约束有效,则仅对
计算一组约束。

Processing a group is defined in Section 4.6, "Validation routine" ; if one of the groups processed in the sequence generates one or more constraint violations, the groups following in the sequence must not be processed. This ensures that a set of constraint is evaluated only if another set of constraint is valid.

参见 http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence-groupsequence

在您的情况下,默认组中存在违规,这意味着特殊组永远不会被验证。

In your case there is a violation in the Default group which means the Special group is never validated.

这篇关于JSR303 - 应用按顺序定义的所有验证组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆