Kaval

A POJO validation DSL in Kotlin

Лицензия

Лицензия

Категории

Категории

KeY Данные Data Formats Formal Verification
Группа

Группа

io.monkeypatch.kaval
Идентификатор

Идентификатор

kaval-arrow
Последняя версия

Последняя версия

0.5.1
Дата

Дата

Тип

Тип

pom
Описание

Описание

Kaval
A POJO validation DSL in Kotlin
Ссылка на сайт

Ссылка на сайт

http://github.com/MonkeyPatchIo/kaval
Организация-разработчик

Организация-разработчик

MonkeyPatch
Система контроля версий

Система контроля версий

https://github.com/MonkeyPatchIo/kaval

Скачать kaval-arrow

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/io.monkeypatch.kaval/kaval-arrow/ -->
<dependency>
    <groupId>io.monkeypatch.kaval</groupId>
    <artifactId>kaval-arrow</artifactId>
    <version>0.5.1</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/io.monkeypatch.kaval/kaval-arrow/
implementation 'io.monkeypatch.kaval:kaval-arrow:0.5.1'
// https://jarcasting.com/artifacts/io.monkeypatch.kaval/kaval-arrow/
implementation ("io.monkeypatch.kaval:kaval-arrow:0.5.1")
'io.monkeypatch.kaval:kaval-arrow:pom:0.5.1'
<dependency org="io.monkeypatch.kaval" name="kaval-arrow" rev="0.5.1">
  <artifact name="kaval-arrow" type="pom" />
</dependency>
@Grapes(
@Grab(group='io.monkeypatch.kaval', module='kaval-arrow', version='0.5.1')
)
libraryDependencies += "io.monkeypatch.kaval" % "kaval-arrow" % "0.5.1"
[io.monkeypatch.kaval/kaval-arrow "0.5.1"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

Kaval Kaval

GitHub Workflow Status GitHub ktlint Awesome Kotlin Badge

This is a Kotlin multi-platform library to validate your model.

Why

The goal is to find the right balance between conciseness, expressiveness, and composability to validate POJO.

Or you can see it as a validation DSL.

Example

We want to validate this model

data class User(
    val firstName: String,
    val lastName: String,
    val address: Address?
)

data class Address(
    val line1: String,
    val line2: String,
    val zipCode: Int,
    val city: String
)

We can validate the Address with these constraints:

  • line1: not blank, and length <= 255
  • line2: length <= 255
  • zipCode: > 0
  • city: not blank
val addressValidator: Validator<Address> =
    reflectValidator {
        Address::line1 { notBlank and maxLength(255) }
        Address::line2 { maxLength(255) }
        Address::zipCode { greaterThan(0) }
        Address::city { notBlank }
    }

And the User with these constraints:

  • firstName: not blank, and length <= 128
  • lastName: length <= 255
  • address: see above
val userValidator: Validator<User> =
    reflectValidator {
        User::firstName { notBlank and maxLength(128) }
        User::lastName { maxLength(255) }
        User::address { nullOr { Address.validator } }
    }

Now we can use the validators:

val user = User(
   firstName = "",
   lastName = "x".repeat(500),
   address = Address(
       line1 = "",
       line2 = "",
       zipCode = -1,
       city = ""
   )
)

val result: ValidationResult = userValidator.validate(user)
println(result)
// Invalid:
//  - [firstName] requires to be not blank
//  - [lastName.length] requires to be lower or equals to 255, got 500
//  - [address.line1] requires to be not blank
//  - [address.zipCode] requires to be greater than 0, got -1
//  - [address.city] requires to be not blank should be Valid

Modules

io.monkeypatch.kaval

Версии библиотеки

Версия
0.5.1
0.5.0
0.4.0