Skip to content

Commit

Permalink
Add note about ExceptionMapper<ValidationException> in validation guide
Browse files Browse the repository at this point in the history
Relates to: #12006
  • Loading branch information
geoand committed Sep 30, 2024
1 parent 96d8967 commit fa187a7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/src/main/asciidoc/validation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,42 @@ As you can see, we don't have to manually validate the provided `Book` anymore a
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"}
Expand Down

0 comments on commit fa187a7

Please sign in to comment.