Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Validation groups have grown to six groups on one DTO and nobody understands which rules apply where. What would you do instead?

I would replace the groups with separate DTOs per operation, such as CreateOrderRequest, UpdateOrderRequest and CancelOrderRequest, each with plain constraints that apply every time. Groups are a good tool only for small differences, like an id that is null on create and required on update. Rules shared by several DTOs go into composed annotations, so they are written once without bringing the groups back.

Background to this question

Validation groups start innocently: one group for create, one for update. Then come a draft state, an admin override, an import path and a partial update, and one DTO carries six groups. Every field has several annotations with different group lists, each endpoint names a different combination, and nobody can answer the simple question of which rules apply to this request without reading the whole class. In this answer, we will look at why groups scale badly, split the DTO into one class per operation, keep shared rules in composed annotations, and plan the migration safely.

1Why Six Groups Become Unreadable

A group is a label on a constraint, and each validation run checks only the labels it names. With two groups, a reader can keep both in mind. With six, the rules for any one operation are scattered: to know what applies to an admin update, we must read every annotation on every field, filter by group, remember which groups extend Default, and check the group list on the endpoint. The class no longer describes any single request.

Java
public class OrderDto {
@Null(groups = {OnCreate.class, OnImport.class})
@NotNull(groups = {OnUpdate.class, OnAdminUpdate.class, OnCancel.class, OnDraft.class})
Long id;
@NotNull(groups = {OnCreate.class, OnUpdate.class, OnImport.class})
@Null(groups = OnCancel.class)
Long customerId;
@NotEmpty(groups = {OnCreate.class, OnUpdate.class})
@Size(max = 50, groups = {OnCreate.class, OnUpdate.class, OnAdminUpdate.class, OnImport.class})
List<@Valid LineDto> lines;
@NotBlank(groups = OnCancel.class)
String cancelReason;
// ... and the question: what is checked on POST /orders/{id}/cancel?
}

Notice the other cost: the DTO advertises fields that make no sense for some operations, such as cancelReason on create. Generated API documentation shows one schema with every field optional, because the documentation tool cannot express this group applies here, so clients cannot learn the rules from the docs either.

2One DTO per Operation

The clearer design is a request class per operation. Each one contains only the fields that operation accepts and only the constraints that always apply to it. Records make this cheap.

Java
public record CreateOrderRequest(
@NotNull Long customerId,
@NotEmpty @Size(max = 50) List<@Valid LineDto> lines) {}
public record UpdateOrderRequest(
@NotEmpty @Size(max = 50) List<@Valid LineDto> lines) {} // id comes from the path
public record CancelOrderRequest(
@NotBlank @Size(max = 500) String cancelReason) {}
@PostMapping("/orders")
OrderDto create(@Valid @RequestBody CreateOrderRequest req) { ... }
@PostMapping("/orders/{id}/cancel")
OrderDto cancel(@PathVariable long id, @Valid @RequestBody CancelOrderRequest req) { ... }
Before and after
QuestionOne DTO with six groupsOne DTO per operation
Which rules apply to cancel?read all fields, filter by groupread CancelOrderRequest
Fields a client may sendevery field, alwaysonly that operation's fields
API documentationone vague schemaa precise schema per operation
Risk of the Default group traphighnone, no groups
Number of classesoneone per operation

Now the question what is checked on cancel has a one-line answer: read CancelOrderRequest. The id problem disappears because update and cancel take the id from the path, so no DTO needs @Null and @NotNull on the same field. Plain @Valid works everywhere again, the documentation shows a precise schema per operation, and an unknown field such as cancelReason on create can be rejected by Jackson instead of silently ignored. The cost is more classes and some mapping code into the domain, which is small and easy to test.

3Sharing Rules With Composed Annotations

Splitting DTOs raises a fair worry: the lines rule, not empty and at most 50, now appears twice. If it changes, someone might update only one copy. Bean Validation has an answer for this: a composed constraint, an annotation that bundles other constraints under one name.

Java
@NotEmpty
@Size(max = 50)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.RECORD_COMPONENT})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {}) // no validator of its own: the composed ones do the work
@Documented
public @interface OrderLines {
String message() default "invalid order lines";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public record CreateOrderRequest(@NotNull Long customerId, @OrderLines List<@Valid LineDto> lines) {}
public record UpdateOrderRequest(@OrderLines List<@Valid LineDto> lines) {}
// lines = [] -> lines: must not be empty (each composed constraint reports its own message)

The rule now has a name and one definition, and it reads like domain language. If we prefer a single message instead of one per inner constraint, adding @ReportAsSingleViolation makes the composed annotation report only its own message.

4Where Groups Still Fit

Groups are not bad; they are a tool for small differences. If two operations differ in exactly one rule, such as the id being null on create and required on update, a single DTO with two groups that extend Default is simpler than two nearly identical classes. A useful rule of thumb for a team guideline is: at most two groups on a DTO, and as soon as a third would be needed, or a field makes sense for only some operations, split the DTO.

5Migrating Without Breaking Clients

The new DTOs must accept exactly what the old one accepted for each operation, or clients break. So before changing anything, we write tests per endpoint that send valid and invalid requests and record the current status codes and messages. Then we introduce the new DTO for one endpoint at a time, run the same tests, and remove the group annotations that no longer have a user. If the old DTO accepted fields it ignored, we decide deliberately whether the new one should ignore or reject them, and announce a stricter behaviour as a change. After the last endpoint moves, the group interfaces can be deleted.

Step throughMigrating from groups to DTOs
  1. Write tests per endpointrecord current valid and invalid behaviour
  2. Create the DTO for one operationonly its fields and rules
  3. Switch the endpoint to @Validmap the DTO to the domain
  4. Do the tests still pass?
    YesNext endpointNoFix the DTO, or document a deliberate change
  5. Delete unused groupsafter the last endpoint

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Does one DTO per operation not duplicate a lot of code?

Some, but the duplication is in field declarations, which are cheap and explicit, while the rules themselves can be shared through composed annotations and shared nested types such as LineDto. In exchange, every class can be understood on its own, which matters more as the API grows.

Should the response DTO also be split?

Not necessarily. Responses do not need validation, and one OrderDto for output is usually fine. The splitting is about input, where each operation genuinely accepts different data.

Could a rules engine or the service replace these annotations?

For business rules that depend on state, such as whether an order can still be cancelled, yes, that belongs in the service. But field shape rules like not empty and at most 50 lines are exactly what annotations on a request DTO do best, so they stay there.

7Conclusion

In this answer, we saw why six validation groups on one DTO hide which rules apply, replaced them with one request DTO per operation such as CreateOrderRequest and UpdateOrderRequest with plain constraints, kept shared rules in composed annotations, limited groups to small differences such as the id rule, and migrated one endpoint at a time behind tests that pin the current behaviour.

What to say in the interviewAbout 80 seconds aloud

Open with

Six groups on one DTO is a sign the class is trying to describe six different requests at once. To know which rules apply to one operation, you have to read every annotation and filter by group, and the API docs show one vague schema where everything looks optional.

Then, if they want more

So I would split it into one request DTO per operation, like CreateOrderRequest, UpdateOrderRequest and CancelOrderRequest. Each one has only the fields that operation accepts and plain constraints that always apply, so we are back to simple @Valid, there is no Default group trap, and the docs become precise.

Many group rules disappear on their own, for example the id moves to the path for update and cancel. For rules shared between DTOs, like the order lines being non-empty and at most 50, I would write a composed annotation, @OrderLines, that bundles @NotEmpty and @Size, so the rule has one definition and a domain name.

Groups still have a place for small differences, like an id that is null on create and required on update, and I would set a guideline of at most two. To migrate safely, I would first write tests that pin each endpoint's current behaviour, then move one endpoint at a time.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Validation groups have grown to six groups on one DTO and nobody understands which rules...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/6bf83c4b74/