Add support for Bean Validation

This commit adds support for validating handler method inputs after
binding from the data fetching environment and before invoking the
handler method.

This support is based on Bean Validation and is enabled only if the
application context contains a `Validator` bean.

Closes gh-110
This commit is contained in:
Brian Clozel
2021-12-10 15:50:16 +01:00
parent 58f2afebb5
commit 3e29e71a6e
9 changed files with 377 additions and 8 deletions

View File

@@ -821,6 +821,52 @@ You can use `@Argument` on a `Map<String, Object>` argument, to obtain all argum
values. The name attribute on `@Argument` must not be set.
[[controllers-schema-mapping-validation]]
==== `@Argument` validation
If a {spring-framework-ref-docs}/core.html#validation-beanvalidation-overview[Bean Validation]
`Validator` (or typically, a `LocalValidatorFactoryBean`) bean is present in the application context,
the `AnnotatedControllerConfigurer` will auto-detect it and configure support for validation.
Controller arguments annotated with `@Valid` and `@Validated` are then validated before method invocation.
Bean Validation lets you declare constraints on types, as the following example shows:
[source,java,indent=0,subs="verbatim,quotes"]
----
public class BookInput {
@NotNull
private String title;
@NotNull
@Size(max=13)
private String isbn;
}
----
We can then mark our argument for validation with `@Valid`:
[source,java,indent=0,subs="verbatim,quotes"]
----
@Controller
public class BookController {
@MutationMapping
public Book addBook(@Argument @Valid BookInput bookInput) {
// ...
}
}
----
If an error occurs during validation, a `ConstraintViolationException` is thrown and can be
later <<execution-exceptions,resolved with a custom `DataFetcherExceptionResolver`>>.
[TIP]
====
Unlike Spring MVC, handler method signatures do not support the injection of `BindingResult`
for reacting to validation errors: those are globally dealt with as exceptions.
====
[[controllers-schema-mapping-projectedpayload-argument]]
==== `@ProjectPayload` Interface