-
Notifications
You must be signed in to change notification settings - Fork 16
Description
avaje-http provides a validation API, where avaje-validator among others are available.
Having avaje-http automatically register, for example, the following, could add convenience:
jex.error(ValidationException.class, (context, exception) -> {
List<ValidationException.Violation> violations = exception.getErrors();
int violationCount = violations.size();
String violationList = violations.stream()
.map(violation -> "'" + violation.getField() + "' " + violation.getMessage())
.collect(joining("\n"));
// return a plain-text error message
context.write(format("Bad Request [{0} validation violations: \n{1}]", violationCount, violationList));
});-
avaje-httpprovides an agnosticio.avaje.http.api.ValidationExceptionamong each validator implementation.Therefore, this can be used by the generator, and assumed to exist in all cases.
If a user does not have a validator, the error-handling middleware is just a hidden no-op they won't notice. -
The above example displays a plain-text error message.
-
When
avaje-jsonbis available (or justavaje-json-corereally):
RFC 7807 / RFC 9457 compliant message as follows can be emitted from theValidationException:{ "type": "https://avaje.io/validator/validation-error-message", "title": "Your request is not valid.", "errors": [ { "detail": "must be a positive integer", "pointer": "#/age" }, { "detail": "must be 'green', 'red' or 'blue'", "pointer": "#/profile/color" } ] }(this is a really bad, rough example)
-
Javalin offers a default middleware for their
Validatoras well.app.get("/test123/{value}", ctx -> { int value = ctx.queryParamAsClass("value", Integer.class) .check(q -> (q > 0), "Quantity must be positive") .get(); ctx.result(value); });
Below is what their middleware looks like:
app.exception(ValidationException::class, (error, ctx) -> ctx.json(e.errors).status(400); });