简体   繁体   English

在 Spring Boot 中编写验证组的最佳方式

[英]Best way to write validation groups in Spring Boot

I'm new to Spring boot development.我是 Spring Boot 开发的新手。

We are using validation groups to validate our request.我们正在使用验证组来验证我们的请求。

It's helpful for avoiding duplicate class creation.这有助于避免重复创建类。

Problem:问题:

But the problem is if we need same variable for multiple group code become very larger and difficult to add another group (because we need to scroll all the classes)但问题是如果我们需要多个组的相同变量代码变得非常大并且难以添加另一个组(因为我们需要滚动所有类)

If we work with the team, it creates code merging issues also.如果我们与团队合作,它也会产生代码合并问题。

My code:我的代码:

public class RequestDto {

@Min(groups = {ConformFtthCreateRequestByAsherOtp.class, ConformFtthCreateRequestInstallationAppointmentByServiceSeeker.class, ConformFtthCreateRequestInstallationCompletionByServiceProvider.class, ConformFtthCreateRequestIssuerCompletedByServiceProvider.class, ConformFtthCreateRequestIssuerCompletedByServiceSeeker.class, CancelServiceSeekerFtthCreateRequestByServiceSeeker.class, ConformCancelServiceSeekerFtthCreateRequestByServiceSeeker.class, FtthStatusRequestValidator.class, ConformFtthCancelRequestValidator.class,RescheduleServiceSeekerFtthCreateRequest.class,ConfirmRescheduleServiceSeekerFtthCreateRequest.class}, value = 1, message = EValidationErrorMessage.MISSING_FIELD)
@NotNull(groups = {ConformFtthCreateRequestByAsherOtp.class, ConformFtthCreateRequestInstallationAppointmentByServiceSeeker.class, ConformFtthCreateRequestInstallationCompletionByServiceProvider.class, ConformFtthCreateRequestIssuerCompletedByServiceProvider.class, ConformFtthCreateRequestIssuerCompletedByServiceSeeker.class, CancelServiceSeekerFtthCreateRequestByServiceSeeker.class, ConformCancelServiceSeekerFtthCreateRequestByServiceSeeker.class, FtthStatusRequestValidator.class, ConformFtthCancelRequestValidator.class,RescheduleServiceSeekerFtthCreateRequest.class,ConfirmRescheduleServiceSeekerFtthCreateRequest.class}, message = EValidationErrorMessage.MISSING_FIELD)
private Long id;

How to overcome this length issue or any other smart way to handle this?如何克服这个长度问题或任何其他聪明的方法来处理这个问题?

Edit:编辑:

I'm not asking how to set Default group?我不是在问如何设置默认组? I'm asking what's the best way if we have multiple validation groups.我在问如果我们有多个验证组,最好的方法是什么。 In the above example, I have 15 validation groups, In future it will increase.在上面的例子中,我有 15 个验证组,以后还会增加。 It's hard to add/edit a new group after it become bigger.新组变大后很难添加/编辑。

Does it more readable and editable than one line code?它是否比一行代码更具可读性和可编辑性?

public class RequestDto {
    @Min(
            groups = {
                    ConformFtthCreateRequestByAsherOtp.class,
                    ConformFtthCreateRequestInstallationAppointmentByServiceSeeker.class,
                    ConformFtthCreateRequestInstallationCompletionByServiceProvider.class,
                    ConformFtthCreateRequestIssuerCompletedByServiceProvider.class,
                    ConformFtthCreateRequestIssuerCompletedByServiceSeeker.class,
                    CancelServiceSeekerFtthCreateRequestByServiceSeeker.class,
                    ConformCancelServiceSeekerFtthCreateRequestByServiceSeeker.class,
                    FtthStatusRequestValidator.class,
                    ConformFtthCancelRequestValidator.class,
                    RescheduleServiceSeekerFtthCreateRequest.class,
                    ConfirmRescheduleServiceSeekerFtthCreateRequest.class
            },
            value = 1,
            message = EValidationErrorMessage.MISSING_FIELD
    )
    @NotNull(
            groups = {
                    ConformFtthCreateRequestByAsherOtp.class,
                    ConformFtthCreateRequestInstallationAppointmentByServiceSeeker.class,
                    ConformFtthCreateRequestInstallationCompletionByServiceProvider.class,
                    ConformFtthCreateRequestIssuerCompletedByServiceProvider.class,
                    ConformFtthCreateRequestIssuerCompletedByServiceSeeker.class,
                    CancelServiceSeekerFtthCreateRequestByServiceSeeker.class,
                    ConformCancelServiceSeekerFtthCreateRequestByServiceSeeker.class,
                    FtthStatusRequestValidator.class,
                    ConformFtthCancelRequestValidator.class,
                    RescheduleServiceSeekerFtthCreateRequest.class,
                    ConfirmRescheduleServiceSeekerFtthCreateRequest.class
            },
            message = EValidationErrorMessage.MISSING_FIELD
    )
    private Long id;
}

Utilize polymorphism.利用多态性。 Add another layer of validation interfaces to group the ones you have, and from each individual validation group interface, you extend your "base" validation interfaces.添加另一层验证接口以对您拥有的接口进行分组,并从每个单独的验证组接口扩展您的“基本”验证接口。

public interface BaseValidation {}

public interface ValidationGroupA extends BaseValidation {}

public interface ValidationGroupB extends BaseValidation {}

public class Dto {
  @NotEmpty(groups = BaseValidation.class)
  private String value;

  // When validating the Dto object with a validation interface that extends the BaseValidation interface, we will still get violations.
  public static void main(String[] args) {
    Dto dtoWithValue = new Dto();
    dtoWithValue.setValue("Has A Value");
    Dto dtoWithoutValue = new Dto();
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<Dto>> violations = validator.validate(dtoWithoutValue, ValidationInterfaceA.class);

    // Should have 1 constraint violation since ValidationInterfaceA extends BaseValidation which is violated.
    System.out.println(violations.size());

    violations = validator.validate(dtoWithValue, ValidationInterfaceA.class);

    // Should have 0 constraint violation since ValidationInterfaceA extends BaseValidation which is not violated.
    System.out.println(violations.size());
  }
}

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

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