Question Details

No question body available.

Tags

java spring jakarta-validation

Answers (1)

Accepted Answer Available
Accepted Answer
January 3, 2026 Score: 2 Rep: 29,591 Quality: High Completeness: 80%

You can use constraint groups to achieve the desired result.

First step is to create groups:

interface CreateProfileGroup {}
interface UpdateProfileGroup {}

Next step is to specify validation annotation twice on the ProfileRequest type, once each for the validation groups defined above:

@ValidProfileRequest(groups =  CreateProfileGroup.class)
@ValidProfileRequest(groups = UpdateProfileGroup.class)
class ProfileRequest {
  // code...
}

Specifying the annotation twice will result in a compilation error which can be resolved by using the @Repeatable annotation:

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = ProfileRequestValidator.class) @Repeatable(ValidProfileReqs.class) //