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 listvalidate()returns, in the same order;- a
messagethat lists each violation aspath: 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:@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.