Skip to main content
At the edge of your system, a request is either valid or rejected. validateOrThrow() returns nothing when every rule holds, and throws a ValidationException that carries the violations when any is broken:

ValidationException

ValidationException is a RuntimeException, so it needs no throws declaration and passes through any layer. It has:
  • violations, the same list validate() returns, in the same order;
  • a message that lists each violation as path: message, joined by commas.

One handler for every request

Because every invalid object throws the same exception with the same data, one error handler turns all of them into the same response. With Ktor’s StatusPages, for example:
A Spring @ExceptionHandler(ValidationException::class) does the same.

Every way to throw

validateOrThrow() is exactly validate().orThrow(): the same rules, the same violations.
Validation<T>, the rules beside a class, returns a result: call rules.validate(value).orThrow() to throw from it.