validate

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.

Signature

def validate(check:  Boolean, errorMsg: String): Directive0 

Example

val route =
  requestUri { uri =>
    validate(uri.path.toString.size < 5, s"Path too long: '${uri.path.toString}'") {
      complete(s"Full URI: $uri")
    }
  }

Get("/234") ~> route ~> check {
  responseAs[String] === "Full URI: http://example.com/234"
}
Get("/abcdefghijkl") ~> route ~> check {
  rejection === ValidationRejection("Path too long: '/abcdefghijkl'", None)
}