简体   繁体   English

DTO 级别基于注释的验证发生在字段级别验证之前,例如 @NotNull @Size 等

[英]Annotation based validation on DTO level happens before field level validation like @NotNull @Size etc

I have a DTO class where I made some fields mandatory.我有一个 DTO 类,我将一些字段设为必填字段。 And, based on another type field where value can be assume A & B if type A, need to check only 1 item can be passed, if B it can be more than 1 also.并且,基于另一个类型字段,其中值可以假设为 A & B 如果类型 A,则只需要检查 1 项可以通过,如果 B 也可以大于 1。

I need to check if a list has at least 1 value in DTO class level.我需要检查列表在 DTO 类级别中是否至少有 1 个值。 And, in custom annotation validation I need to check size of list based on type field of DTO.而且,在自定义注释验证中,我需要根据 DTO 的类型字段检查列表的大小。

So, in DTO所以,在 DTO

@NotNull
@Size(min = 1)
private List<@NotBlank String> items;

And, inside annotation并且,内部注释

        if (cancelType == CancellationTypeEnum.A && cancelDto.getItems().size() > 1) {

            isValid = false;
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(
                    "Only one item can be sent for 'A' cancel type.").addConstraintViolation();

        } 

But, since field level happens later, if I pass null for items field, it goes to this annotation validator & throw NPE as field is null & I am trying to get size但是,由于字段级别稍后发生,如果我为 items 字段传递 null,它会转到此注释验证器并抛出 NPE,因为字段为 null 并且我正在尝试获取大小

Temporary solution is I do null check, then check size, but in field level anyway we are giving @NotNull.临时解决方案是我做空检查,然后检查大小,但无论如何我们在字段级别给@NotNull。

Is there any way to do class level custom annotation to validate after field level validation happens.有没有办法在字段级验证发生后进行类级自定义注释来验证。 In that case, it will throw field level validation as field is null & will not go to custom class level annotation在这种情况下,它会抛出字段级验证,因为字段为空并且不会转到自定义类级注释

You could use JS-303 validation groups (see here ), in conjunction with @Validated , from the documentation:您可以将 JS-303 验证组(参见此处)与文档中的@Validated结合使用:

Variant of JSR-303's Valid, supporting the specification of validation groups. JSR-303 的 Valid 变体,支持验证组的规范。

For example:例如:

@CheckEnumSize(groups = {Secondary.class})
public class CancelDto {

    public CancelDto(List<String> items) {
        this.items = items;
    }

    @NotNull(groups = {Primary.class})
    @Size(min = 1)
    public List<@NotNull String> items;

    public CancellationTypeEnum cancelType;

    public List<String> getItems() {
        return items;
    }
}
 

where Primary is a simple interface:其中Primary是一个简单的界面:

public interface Primary {
}

and the same for Secondary :Secondary相同:

public interface Secondary {
}

finally you could use validated as follows:最后你可以使用验证如下:

@Service
@Validated({Primary.class, Secondary.class})
public class CancelService {

    public void applyCancel(@Valid CancelDto cancel) {
        //TODO
    }

}

when using the above code with a CancelDto with items null, you should get:将上述代码与CancelDto与项目为 null 一起使用时,您应该得到:

Caused by: javax.validation.ConstraintViolationException: applyCancel.cancel.items: must not be null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM