55 */
66package io .jooby .avaje .validator ;
77
8- import static io .jooby .validation .ValidationResult .ErrorType .FIELD ;
9- import static io .jooby .validation .ValidationResult .ErrorType .GLOBAL ;
10- import static java .util .stream .Collectors .groupingBy ;
11-
12- import java .util .ArrayList ;
13- import java .util .List ;
14- import java .util .Map ;
15-
168import org .slf4j .Logger ;
179import org .slf4j .LoggerFactory ;
1810
19- import io .avaje .validation .ConstraintViolation ;
2011import io .avaje .validation .ConstraintViolationException ;
2112import io .jooby .Context ;
2213import io .jooby .ErrorHandler ;
2314import io .jooby .StatusCode ;
15+ import io .jooby .internal .avaje .validator .ConstraintViolationMapper ;
16+ import io .jooby .validation .ValidationExceptionMapper ;
2417import io .jooby .validation .ValidationResult ;
2518
2619/**
5649 * @since 3.2.10
5750 */
5851public class ConstraintViolationHandler implements ErrorHandler {
59- private static final String ROOT_VIOLATIONS_PATH = "" ;
6052 private final Logger log = LoggerFactory .getLogger (getClass ());
6153 private final StatusCode statusCode ;
62- private final String title ;
54+ private final ValidationExceptionMapper mapper ;
6355 private final boolean logException ;
6456 private final boolean problemDetailsEnabled ;
6557
6658 public ConstraintViolationHandler (
6759 StatusCode statusCode , String title , boolean logException , boolean problemDetailsEnabled ) {
6860 this .statusCode = statusCode ;
69- this .title = title ;
61+ this .mapper = new ConstraintViolationMapper ( statusCode , title ) ;
7062 this .logException = logException ;
7163 this .problemDetailsEnabled = problemDetailsEnabled ;
7264 }
@@ -77,34 +69,11 @@ public void apply(Context ctx, Throwable cause, StatusCode code) {
7769 if (logException ) {
7870 log .error (ErrorHandler .errorMessage (ctx , code ), cause );
7971 }
80- var violations = ex .violations ();
81-
82- var groupedByPath = violations .stream ().collect (groupingBy (ConstraintViolation ::path ));
83- var errors = collectErrors (groupedByPath );
84-
85- var result = new ValidationResult (title , statusCode .value (), errors );
72+ var result = mapper .toResult (code , ex );
8673 renderOrPropagate (ctx , result , code );
8774 }
8875 }
8976
90- private List <ValidationResult .Error > collectErrors (
91- Map <String , List <ConstraintViolation >> groupedViolations ) {
92- List <ValidationResult .Error > errors = new ArrayList <>();
93- for (var entry : groupedViolations .entrySet ()) {
94- var path = entry .getKey ();
95- if (ROOT_VIOLATIONS_PATH .equals (path )) {
96- errors .add (new ValidationResult .Error (null , extractMessages (entry .getValue ()), GLOBAL ));
97- } else {
98- errors .add (new ValidationResult .Error (path , extractMessages (entry .getValue ()), FIELD ));
99- }
100- }
101- return errors ;
102- }
103-
104- private List <String > extractMessages (List <ConstraintViolation > violations ) {
105- return violations .stream ().map (ConstraintViolation ::message ).toList ();
106- }
107-
10877 private void renderOrPropagate (Context ctx , ValidationResult result , StatusCode code ) {
10978 if (problemDetailsEnabled ) {
11079 ctx .getRouter ().getErrorHandler ().apply (ctx , result .toHttpProblem (), code );
0 commit comments