Skip to content

Getting Started

MerlinTHS edited this page Feb 17, 2023 · 8 revisions

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"
}

Hello World!

Kava comes with a few top-level functions which provide a scope for validation.

  • nullable<Type> Returns Type or null
  • optional<Type> Returns Type wrapped into java.util.Optional
  • validate Returns a ValidationResult ( Success or Failure )
  • kava Returns always Unit

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 ValidationException is thrown. So be careful when catching exceptions inside a ValidationScope.

Clone this wiki locally