Data Validation in Kotlin: Library Overview and Practical Examples
Data validation is one of the key aspects of developing reliable applications. In the Kotlin ecosystem, there are several mature libraries that allow you to declaratively describe validation rules, freeing the developer from boilerplate code. In this article, we will look at three popular tools: Konform, Valiktor, and Kotlinx Validation, and also discuss when and which tool to choose.
1. Konform — Functional Validation Based on Arrow
The Konform library is built on the Arrow functional framework. It uses the Validated concept for accumulating errors and supports both simple and nested data structures.
Installation
// build.gradle.ktsimplementation("io.konform:konform:0.4.0")Usage Example
import io.konform.validation.*
data class User( val name: String, val email: String, val age: Int)
val userValidation = Validation<User> { User::name { minLength(2) hint "Name must contain at least 2 characters" maxLength(50) hint "Name must not exceed 50 characters" } User::email { pattern(Regex("^[\\\\w.-]+@[\\\\w.-]+\\\\.\\\\w{2,}$")) hint "Invalid email" } User::age { minimum(18) hint "Age must be at least 18 years" maximum(120) hint "Enter a real age" }}
fun main() { val user = User("A", "invalid-email", 15) val result = userValidation(user) if (result.errors.isNotEmpty()) { result.errors.forEach { error -> println("Field: ${error.path} — ${error.message}") } } else { println("Validation passed successfully") }}Features
- Full integration with Arrow for composing validations
- Support for nested objects and lists
- Type-safe DSL constructs
- Accumulation of all errors (does not stop at the first one)
2. Valiktor — Imperative Validation with Fluent API
Valiktor offers a more traditional approach using a fluent interface. The library does not require functional dependencies and is easy to learn for developers familiar with Java.
Installation
implementation("org.valiktor:valiktor-core:0.12.0")Usage Example
import org.valiktor.functions.*import org.valiktor.validate
data class Order( val id: String, val amount: Double, val items: List<String>)
fun validateOrder(order: Order) { validate(order) { validate(Order::id).isNotBlank().hasSize(10..36) validate(Order::amount).isPositive().isLessThanOrEqualTo(10000.0) validate(Order::items).isNotEmpty().hasSize(1..50) }}
fun main() { val order = Order("", -100.0, emptyList()) try { validateOrder(order) println("Order is valid") } catch (e: Exception) { println("Validation error: ${e.message}") }}Features
- Simple and clear API without functional abstractions
- Throws an exception on the first error (fail-fast mode)
- Support for custom validators via extensions
- Easy integration with Java projects
3. Kotlinx Validation — Official Library from JetBrains
Although the Kotlinx Validation project is still in an experimental stage, it already offers an interesting approach with annotations and reflection, reminiscent of Javax Validation (Bean Validation).
Installation
implementation("org.jetbrains.kotlinx:kotlinx-validation:0.1.0")Usage Example
i Also in library
Sending Email in Java: A Complete Guide to JavaMail and Jakarta Mail
Working with Excel in TypeScript: Libraries, Tools, and Code Examples
gRPC for C# (.NET): Libraries, Setup, and Code Examples