Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Use validation groups so that id must be null on create and not null on update for the same DTO.

We define two marker interfaces, OnCreate and OnUpdate, annotate the id with @Null(groups = OnCreate.class) and @NotNull(groups = OnUpdate.class), and put @Validated(OnCreate.class) on the create parameter and @Validated(OnUpdate.class) on the update parameter. Because constraints without a groups attribute belong to the Default group, which is not checked when we name a group, we either list Default as well or make the marker interfaces extend Default.

Background to this question

Create and update requests often share one DTO, and the id is the classic difference: a client must not choose the id when creating, but must send it when updating. Validation groups let one class carry both rules. They also have a trap that catches almost everyone the first time: the other constraints on the DTO stop being checked. In this answer, we will recap what a group is, write the DTO and the controller, reproduce the Default group trap, and fix it in two ways.

1A Quick Recap of Validation Groups

A validation group is a label that a constraint can carry, so that a validation run can check only the constraints with certain labels. In Jakarta Validation, a group is simply a Java interface, usually empty, used as a marker. Every constraint has a groups attribute; when we leave it out, the constraint belongs to the built-in group jakarta.validation.groups.Default.

When we validate without naming a group, only Default constraints are checked. When we name groups, for example OnCreate, only constraints in those groups are checked. That second sentence is the source of the trap we will meet below.

2Writing the DTO and the Controller

First the two marker interfaces, then the DTO with the id rule split by group, then the controller that chooses the group per operation.

Java
public interface OnCreate {}
public interface OnUpdate {}
public record ProductDto(
@Null(groups = OnCreate.class)
@NotNull(groups = OnUpdate.class)
Long id,
@NotBlank String name, // no groups: belongs to Default
@Positive BigDecimal price) {} // no groups: belongs to Default
@RestController
@RequestMapping("/products")
class ProductController {
@PostMapping
ProductDto create(@Validated(OnCreate.class) @RequestBody ProductDto dto) { ... }
@PutMapping("/{id}")
ProductDto update(@PathVariable long id,
@Validated(OnUpdate.class) @RequestBody ProductDto dto) { ... }
}
// POST {"id": 5, "name": "Pen", "price": 2} -> 400: id must be null
// PUT {"name": "Pen", "price": 2} -> 400: id must not be null

We use @Validated instead of @Valid because @Valid has no way to name groups. Spring MVC passes the groups to the validator when it checks the request body, so the create endpoint checks only constraints in OnCreate and the update endpoint only those in OnUpdate.

3The Trap: Default Constraints Are Skipped

Now send a create request with a blank name and a negative price.

Java
// POST {"name": "", "price": -3}
// -> 201 Created (bug: name and price were not validated)
Which constraints run
Validation callid rulename and price rules
@Valid (Default only)not checkedchecked
@Validated(OnCreate.class)@Null checkednot checked
@Validated({OnCreate.class, Default.class})@Null checkedchecked
@Validated(OnCreate.class), OnCreate extends Default@Null checkedchecked

The id rule works, but @NotBlank and @Positive are ignored. The reason is the rule from the recap: when we name OnCreate, the validator checks only OnCreate constraints, and name and price have no groups, so they are in Default. Default is just another group, and it is not included unless we ask for it.

4Fix 1: List Default Explicitly

The direct fix is to name both groups on each endpoint.

Java
import jakarta.validation.groups.Default;
@PostMapping
ProductDto create(@Validated({OnCreate.class, Default.class}) @RequestBody ProductDto dto) { ... }
@PutMapping("/{id}")
ProductDto update(@PathVariable long id,
@Validated({OnUpdate.class, Default.class}) @RequestBody ProductDto dto) { ... }
// POST {"name": "", "price": -3} -> 400: name must not be blank, price must be greater than 0

This is explicit and easy to read, but every endpoint has to remember Default, and forgetting it brings the bug back silently.

5Fix 2: Let the Groups Extend Default

Groups are interfaces, so they can inherit. If OnCreate extends Default, validating OnCreate also validates everything in Default.

Java
public interface OnCreate extends Default {}
public interface OnUpdate extends Default {}
@PostMapping
ProductDto create(@Validated(OnCreate.class) @RequestBody ProductDto dto) { ... }
// POST {"name": "", "price": -3} -> 400: name must not be blank, price must be greater than 0
// POST {"id": 5, "name": "Pen", "price": 2} -> 400: id must be null

Now the controller stays short and nobody can forget the shared rules. The cost is a little hidden magic: a reader must know that the interfaces extend Default to understand why name is checked. A one-line comment on the interfaces helps. Either fix works; what matters is a test for each endpoint with an invalid shared field, so the trap cannot come back unnoticed.

6Follow-up questions

Do groups cascade into nested objects?

Yes. When a field is marked @Valid, the nested object is validated with the same groups as its parent, unless a @ConvertGroup annotation on the field maps them to other groups. So nested DTOs need the same awareness of Default.

Can we use groups with method validation on a service?

Yes. With @Validated on the service class, a method can carry @Validated(OnCreate.class) to choose groups, and parameters marked @Valid are then validated with those groups. The proxy rules still apply, so calls from inside the same class are not validated.

Is there a simpler alternative to groups for create and update?

Often yes: separate DTOs, such as CreateProductRequest without an id and UpdateProductRequest with one. Each class then has plain constraints and no groups. Groups are worth it only when the difference is as small as this id rule.

7Conclusion

In this answer, we used marker interfaces OnCreate and OnUpdate, put @Null(groups = OnCreate.class) and @NotNull(groups = OnUpdate.class) on the id, and chose the group per endpoint with @Validated. We also saw that naming a group skips the constraints without groups, which belong to Default, and fixed it either by listing Default with the group or by making the group interfaces extend Default.

What to say in the interviewAbout 80 seconds aloud

Open with

I would create two empty marker interfaces, OnCreate and OnUpdate, because in Jakarta Validation a group is just an interface. On the id field I put @Null(groups = OnCreate.class) and @NotNull(groups = OnUpdate.class), so the client must not send an id on create and must send one on update.

Then, if they want more

In the controller I use Spring's @Validated instead of @Valid, because @Valid cannot name groups: @Validated(OnCreate.class) @RequestBody on the POST method and @Validated(OnUpdate.class) on the PUT method. The part people miss is the Default group.

Every constraint without a groups attribute, like @NotBlank on name or @Positive on price, belongs to Default, and when I name only OnCreate, Default constraints are not checked at all, so a blank name would pass. There are two fixes.

I can list both, @Validated({OnCreate.class, Default.class}), or I can declare the interfaces as extending Default, so validating OnCreate includes Default automatically. I prefer the second with a short comment, because nobody can forget it on a new endpoint. And I would write a test per endpoint with an invalid shared field to prove the Default constraints still run.

If the differences grew beyond the id, I would switch to separate request DTOs.

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 "Use validation groups so that id must be null on create and not null on update for the...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/59ffbc1988/