Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about ExceptionMapper<ValidationException> in validation guide #43592

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/src/main/asciidoc/validation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,48 @@

As you can see, we don't have to manually validate the provided `Book` anymore as it is automatically validated.

If a validation error is triggered, a violation report is generated and serialized as JSON as our end point produces a JSON output.

Check warning on line 199 in docs/src/main/asciidoc/validation.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'.", "location": {"path": "docs/src/main/asciidoc/validation.adoc", "range": {"start": {"line": 199, "column": 73}}}, "severity": "INFO"}
It can be extracted and manipulated to display a proper error message.

An example of such a report could be:

[source, json]
----
{
"title": "Constraint Violation",
"status": 400,
"violations": [
{
"field": "tryMeEndPointMethodValidation.book.title",
"message": "Title cannot be blank"
}
]
}
----

This response is produced by Quarkus via a builtin implementation of `jakarta.ws.rs.ext.ExceptionMapper`. If the application code needs to handle `ValidationException` in some custom way,

Check warning on line 218 in docs/src/main/asciidoc/validation.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'through', 'by', 'from', 'on', or 'by using' rather than 'via' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'through', 'by', 'from', 'on', or 'by using' rather than 'via' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/validation.adoc", "range": {"start": {"line": 218, "column": 27}}}, "severity": "WARNING"}

Check warning on line 218 in docs/src/main/asciidoc/validation.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'built-in' rather than 'builtin' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'built-in' rather than 'builtin' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/validation.adoc", "range": {"start": {"line": 218, "column": 33}}}, "severity": "WARNING"}

Check warning on line 218 in docs/src/main/asciidoc/validation.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'needs to'. Raw Output: {"message": "[Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'needs to'.", "location": {"path": "docs/src/main/asciidoc/validation.adoc", "range": {"start": {"line": 218, "column": 120}}}, "severity": "INFO"}
it can provide an implementation of `jakarta.ws.rs.ext.ExceptionMapper` like so:

[source, java]
----
import jakarta.validation.ValidationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

@Provider
public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper<ValidationException> {

@Override
public Response toResponse(ValidationException exception) {
// TODO: implement
}
}
----

== Service method validation

It might not always be handy to have the validation rules declared at the end point level as it could duplicate some business validation.

Check warning on line 240 in docs/src/main/asciidoc/validation.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'because' or 'while' rather than 'as'.", "location": {"path": "docs/src/main/asciidoc/validation.adoc", "range": {"start": {"line": 240, "column": 91}}}, "severity": "INFO"}

The best option is then to annotate a method of your business service with your constraints (or in our particular case with `@Valid`):

Expand Down