0

I'm new to Spring boot development.

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 = {ConformCreateRequest.class, ConformCreateRequestInstallationAppointment.class, ConformCreateRequestInstallationCompletion.class, ConformCreateRequestIssuerCompletedBy.class, ConformCreateRequestIssuerCompleted.class, CancelCreateRequest.class, ConformCancelCreateRequest.class, StatusRequestValidator.class, ConformCancelRequestValidator.class,RescheduleCreateRequest.class,ConfirmRescheduleCreateRequest.class}, value = 1, message = EValidationErrorMessage.MISSING_FIELD)
@NotNull(groups = {ConformCreateRequestByAsherOtp.class, ConformCreateRequestInstallationAppointment.class, ConformCreateRequestInstallationCompletion.class, ConformCreateRequestIssuerCompleted.class, ConformCreateRequestIssuerCompleted.class, CancelCreateRequest.class, ConformCancelCreateRequest.class, StatusRequestValidator.class, ConformCancelRequestValidator.class,RescheduleCreateRequest.class,ConfirmRescheduleCreateRequest.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. It's hard to add/edit a new group after it become bigger.

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
  • for such a big contributor having this kind of reputation I think it was just a quick mistake that the code was posted as a screenshot. Could you please update it to match the SO guidelines? – Panagiotis Bougioukos May 16 '22 at 11:57
  • @PanagiotisBougioukos I just added the reference image for quick understanding. I have removed now to avoid confusion. – Ranjithkumar May 16 '22 at 12:03
  • Does this answer your question? [JSR-303 validation groups define a default group](https://stackoverflow.com/questions/35358447/jsr-303-validation-groups-define-a-default-group) – tbjorch May 16 '22 at 12:14
  • @tbjorch I'm asking about validation groups length issue, but you mentioned it duplicate question. Is it solving this question? – Ranjithkumar May 16 '22 at 12:26
  • @Ranjithkumar by looking into my suggested SO post you'll see that there are possibilities of extending the interfaces from e.g. Default group... alt. if you elaborate one step further on it, you can define multiple levels of validation groups which you can extend instead of listing 10-20 interfaces like you do – tbjorch May 16 '22 at 13:00
  • https://stackoverflow.com/a/42595583/4571544 – dekkard May 16 '22 at 13:01
  • @dekkard it seems it was the best way. I will check and let you know – Ranjithkumar May 16 '22 at 13:07
  • 1
    @tbjorch ohh ok. Got your point now. Thanks for the idea! – Ranjithkumar May 16 '22 at 13:24

2 Answers2

0

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;
}
0

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());
  }
}

tbjorch
  • 1,544
  • 1
  • 8
  • 21