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 = {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. It's hard to add/edit a new group after it become bigger.
CodePudding user response:
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;
}
CodePudding user response:
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());
}
}