validate

Allows validating a precondition before handling a route.

Description

Checks an arbitrary condition and passes control to the inner route if it returns true. Otherwise, rejects the request with a ValidationRejection containing the given error message.

Example

final Route route = extractUri(uri ->
  validate(() -> uri.path().length() < 5,
    "Path too long: " + uri.path(),
    () -> complete("Full URI: " + uri.toString()))
);

// tests:
testRoute(route).run(HttpRequest.GET("/234"))
  .assertEntity("Full URI: http://example.com/234");
testRoute(route).run(HttpRequest.GET("/abcdefghijkl"))
  .assertEntity("Path too long: /abcdefghijkl");
The source code for this page can be found here.