Validation
21 questions in 6 subtopics.
Bean Validation
What is Bean Validation, and what do you need in a Spring Boot 3 project to use annotations like@NotBlank?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?Where should validation happen: in the controller, the service, the entity or the database? Explain the role of each.Validation logic often ends up duplicated in frontends, APIs, services and databases. How would you approach this at organisation level over many years?@Valid and @Validated
What is the difference between@Valid and @Validated?A request with an invalid nested address passes validation, although Address has @NotBlank on street. The controller has @Valid @RequestBody CustomerDto. Why?What exceptions are thrown when validation fails for a @RequestBody, a @ModelAttribute and a @RequestParam with @Min, in Spring Framework 6.1 and later?Bean Validation has been part of Java for many years and is now Jakarta Validation. How do you judge whether to keep relying on such standards or adopt newer approaches, for example validation generated from schemas?Validation groups
Use validation groups so that id must be null on create and not null on update for the same DTO.Validation groups have grown to six groups on one DTO and nobody understands which rules apply where. What would you do instead?Validation rules are often business rules in disguise and change often. How would you decide what belongs in annotations and what belongs in a rules engine or domain code over the long term?Custom constraints
Write a custom constraint@ValidIsoCurrency for a String field.Write a class-level constraint that checks startDate is before endDate and reports the error on the endDate field.A custom validator needs a repository to check that a customer id exists. Is that a good idea and how does Spring inject it?You want to offer shared constraints such as @ValidCurrency, @ValidCountry and @ValidIban to all services. How would you design and maintain the library?Method validation
Validate the parameters of a service method voidtransfer(@NotNull String from, @Positive BigDecimal amount). What do you need?A controller annotated with @Validated returns 500 when @Min on a @RequestParam fails, while invalid bodies return 400. Why, and how do you fix it?A security review found that some services validate only in the controller, while internal message consumers and batch jobs call the same services without validation. What would you change?Define a validation standard for all services: where validation happens, error format and shared constraints.Spring Validator interface
Implement a Springorg.springframework.validation.Validator for a PasswordChangeForm that checks the new password differs from the old one and both confirmation fields match, and register it for one controller.Compare Spring's Validator interface with Jakarta Bean Validation. How does Spring bridge the two, and when would you choose a Spring Validator over a custom constraint annotation?No question matches these choices.