-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
The easiest way getting started is by using the Gradle Plugin. Apply it to your project and it will include all the necessary dependencies for the type of project you're working with. If you want to setup all of that manually - please refer to the Manual Setup section.
Simply apply it in your build.gradle.kts.
plugins {
id("io.github.merlinths.kava") version "1.0.0"
}Kava comes with a few top-level functions which provide a scope for validation.
-
nullable<Type>ReturnsTypeornull -
optional<Type>ReturnsTypewrapped intojava.util.Optional -
validateReturns aValidationResult(SuccessorFailure) -
kavaReturns alwaysUnit
kava can be used to execute code only in case the preceding code succeeds, without caring about the overall result of the scope, whereas validate offers a convenient way to handle failure or success with the extension functions onSuccess oronFailure.
import io.mths.kava.validator.extensions.*
fun main() = kava {
val name by mayGetName()
println("Hello $name!")
}
fun mayGetName() =
nullable { "World" }The code inside kava runs in the context of a ValidationScope.
This scope allows you to use Validated Delegations.
Each of the validations either returns a validated instance of its type or fails, which results
in the end of the ValidationScope. In the example above, the scope
would have been left, if getName had returned null.
To achieve this stop of execution, a
ValidationExceptionis thrown. So be careful when catching exceptions inside aValidationScope.