Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Which annotation would you use for each field: a name that must have at least one non-space character, an optional email, a list with 1 to 10 items, a positive amount?

The name gets @NotBlank, which rejects null, an empty string and a string of only spaces. The optional email gets @Email alone, because Bean Validation constraints treat null as valid; we add @NotNull only if the email becomes required. The list gets @Size(min = 1, max = 10), plus @NotNull if the list itself must be present, and the amount gets @Positive, plus @NotNull when it is a wrapper type such as BigDecimal or Integer.

Why this question is asked

Choosing the right constraint looks trivial, but small mistakes here let bad data through: a name of three spaces, a missing list that passes because @Size ignores null, a negative amount on a refund endpoint. Interviewers ask this question to see whether we know the exact behaviour of each annotation, especially how each one treats null. In this answer, we will recap the null rule that ties all the constraints together, then choose and test the annotation for each of the four fields.

1A Quick Recap of the Null Rule

Almost every Bean Validation constraint considers null to be valid. @Size, @Email, @Positive, @Pattern and @Min all return true for a null value. The specification does this on purpose: each constraint checks one thing, and whether a value is required at all is a separate decision expressed with @NotNull. So a field annotated with @Email alone means if there is an email, it must look like one.

The three not-annotations are the exceptions, and they differ in how much they reject. @NotNull rejects only null. @NotEmpty rejects null and an empty string, collection, map or array. @NotBlank, for character sequences only, rejects null, an empty string and a string that contains only whitespace.

What each constraint accepts
Value@NotNull@NotEmpty@NotBlank
nullinvalidinvalidinvalid
""validinvalidinvalid
" "validvalidinvalid
"Ana"validvalidvalid

2The Complete DTO

Here is the request object with all four fields annotated.

Java
import jakarta.validation.constraints.*;
import java.math.BigDecimal;
import java.util.List;
public record CreateInvoice(
@NotBlank String name, // required, at least one non-space character
@Email String email, // optional; null is valid
@NotNull @Size(min = 1, max = 10) List<String> items, // required, 1 to 10 elements
@NotNull @Positive BigDecimal amount) {} // required, greater than zero

Now let us go through the reasoning for each field, because the combination is what makes each rule correct.

3The Name and the Email

For the name, the requirement is at least one non-space character. @NotNull would accept an empty string, @NotEmpty would accept three spaces, so @NotBlank is the only one that matches. It checks that the trimmed length is greater than zero. Usually we also add @Size(max = 100) to protect the database column.

For the email, the field is optional, so @Email alone is right: null passes, and a present value must be a well-formed address. Hibernate Validator's @Email is quite permissive; it accepts any address with a local part, an @ and a domain, so a@b passes. If we need stricter rules, we can add @Pattern(regexp = ...) or check deliverability elsewhere. Be aware too that an empty string is not null; Hibernate Validator's implementation accepts an empty string for @Email, so if clients send "" for no email, either convert it to null during binding or add @Size(min = 3), and check your version's behaviour.

Java
Validator v = Validation.buildDefaultValidatorFactory().getValidator();
v.validate(new CreateInvoice(" ", null, List.of("a"), new BigDecimal("5")));
// 1 violation: name must not be blank
v.validate(new CreateInvoice("Ana", "not-an-email", List.of("a"), new BigDecimal("5")));
// 1 violation: email must be a well-formed email address
v.validate(new CreateInvoice("Ana", null, List.of("a"), new BigDecimal("5")));
// 0 violations: a missing email is fine

4The List and the Amount

For the list, @Size(min = 1, max = 10) counts the elements. Because @Size treats null as valid, a request that leaves out the list entirely would pass, so we add @NotNull when the list is required. @NotEmpty plus @Size(max = 10) is an equivalent alternative. If the elements need rules too, we put them on the type argument, as in List<@NotBlank String> items.

For the amount, @Positive means strictly greater than zero, so 0 is rejected; @PositiveOrZero would allow it. A primitive such as long can never be null, so @Positive alone is enough there, but a primitive also silently turns a missing JSON field into 0, which @Positive then rejects with a confusing message. With a wrapper such as BigDecimal, a missing value is null, which @Positive accepts, so @NotNull is needed. For money we use BigDecimal rather than double, and often add @Digits(integer = 10, fraction = 2) to limit the scale.

Java
v.validate(new CreateInvoice("Ana", null, null, new BigDecimal("5")));
// 1 violation: items must not be null
v.validate(new CreateInvoice("Ana", null, List.of(), new BigDecimal("5")));
// 1 violation: items size must be between 1 and 10
v.validate(new CreateInvoice("Ana", null, List.of("a"), BigDecimal.ZERO));
// 1 violation: amount must be greater than 0
v.validate(new CreateInvoice("Ana", null, List.of("a"), null));
// 1 violation: amount must not be null

5Follow-up questions

Why does @Size not reject null by itself?

Because the specification keeps each constraint focused on one property. If @Size rejected null, there would be no way to express an optional list that, when present, has 1 to 10 elements. Combining @NotNull with @Size makes both decisions visible on the field.

Should we use @Min(1) instead of @Positive for the amount?

@Min takes a long as its bound, so the smallest positive bound it can express is 1, and @Min(1) would reject a perfectly valid amount of 0.50. @Positive works for any number type and says exactly greater than zero. For a decimal minimum such as 0.01, @DecimalMin("0.01") is the right choice.

How do we validate each element of the list?

We annotate the type argument, for example List<@NotBlank @Size(max = 40) String> items. Hibernate Validator then checks every element and reports violations with an index in the path, such as items[2]. For a list of objects, we write List<@Valid Item> so each item's own constraints are checked.

6Conclusion

In this answer, we used the null rule to pick each constraint: @NotBlank for a name that needs a non-space character, @Email alone for an optional email because null is valid, @Size(min = 1, max = 10) with @NotNull for a required list, and @Positive with @NotNull for a BigDecimal amount. We also saw why each partner annotation is needed and tested each case with a validator.

What to say in the interviewAbout 80 seconds aloud

Open with

For the name I would use @NotBlank, because it is the only one that rejects null, an empty string and a string of only spaces; @NotEmpty would still accept three spaces.

Then, if they want more

I would usually add @Size with a max to match the column. For the optional email I would use @Email alone. The important rule is that almost all Bean Validation constraints treat null as valid, so @Email means if an email is present it must be well formed, and I only add @NotNull if the email becomes required.

One caveat is that an empty string is not null, so I would normalise empty strings to null if clients send them. For the list I would use @Size(min = 1, max = 10), and since @Size also accepts null, I add @NotNull if the list itself is required, or I could use @NotEmpty with a max size.

If the elements need rules, they go on the type argument. For the amount I would use @Positive, which means strictly greater than zero, and because I would model money as BigDecimal, a wrapper, I add @NotNull, since @Positive accepts null. Then I would write a test with one invalid value per field.

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 "Which annotation would you use for each field: a name that must have at least one...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/72441816e2/