I have custom validations in place that I want to run after all the default validations. I know that we just need to specify the group sequence in @Validated annotation when we want the object to be validated at the controller itself but I want to run the validations explicitly.
Set<ConstraintViolation<Listing>> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(listing);
if (CollectionUtils.isNotEmpty(violations))
violations.stream().forEach(violation -> errorList.add(violation.getMessage()));
I am not able to figure out how to specify that we want to use this group sequence (ListingConstraintGroupSequence.class).
@ListingAdditionalPropertiesConstraint(groups =
ListingCustomValidationGroup.class,
additionalProperties = "additionalProperties"
)
@ListingInventoryLocationsConstraint(groups = ListingCustomValidationGroup.class,
listingInventoryLocations = "listingInventoryLocations"
)
@ListingConditionalFieldsConstraint.List({
@ListingConditionalFieldsConstraint(groups = ListingCustomValidationGroup.class,
message = "maxLimitPerUser cannot be 0 when limited is true",
booleanFlag = "limited",
field = "maxLimitPerUser"
),
@ListingConditionalFieldsConstraint(groups = ListingCustomValidationGroup.class,
message = "maxDaystoReturn cannot be 0 when willAcceptReturns is true",
booleanFlag = "willAcceptReturns",
field = "maxDaystoReturn"
)
})
@EqualsAndHashCode
@Getter
@Setter
@ToString
public class Listing implements Serializable {
//Ensures that Custom Validators are run only if the default validations pass
@GroupSequence({Default.class,ListingCustomValidationGroup.class})
public interface ListingConstraintGroupSequence {}
.
.
. }
Referred question: Bean validation group sequence not working