Kold

Validation library for Kotlin

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

com.github.kotlin-kold
ArtifactId

ArtifactId

kold-spring-jvm
Last Version

Last Version

0.2
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

Kold
Validation library for Kotlin
Project URL

Project URL

https://github.com/kotlin-kold/kold
Source Code Management

Source Code Management

https://github.com/kotlin-kold/kold

Download kold-spring-jvm

Dependencies

compile (2)

Group / Artifact Type Version
com.github.kotlin-kold : kold-core-jvm jar 0.2
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.20

runtime (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar 2.4.0
com.fasterxml.jackson.core : jackson-core jar 2.12.0
com.fasterxml.jackson.core : jackson-databind jar 2.12.0

Project Modules

There are no modules declared in this project.

Kold is a type-safe validation library for Kotlin.

What's it for

This library is for validating raw data before constructing an object.

When to use it

There are a couple of use-cases

  • Validating value-objects like EmailAddress
  • Validating fields of an object

Validating value-objects

Imagine you have some values that should follow certain rules to be valid like email address.

We create a wrapper-types for such values, making constructor private and allowing to construct such wrappers only via factory methods which return instance of Validated.

class EmailAddress private constructor(val value: String) {
    companion object {
        fun fromString(email: String): Validated<EmailAddress> {
            val violations = buildList {
                if (email.length > 2048) {
                    add(ValueViolation("value.too.long", "Email address can't be longer than 2048 characters"))
                }

                if (!email.contains("@")) {
                    add(ValueViolation("value.invalid", "Email address is invalid format"))
                }
            }

            return if (violations.isEmpty()) {
                Validated.Valid(EmailAddress(email))
            } else {
                Validated.Invalid(violations)
            }
        }
    }
}

Validating fields of an object

We want to validate a complex object that contains a few fields, we get data from an API in JSON-like format. We convert raw data to KoldData, there are helper functions for that. Then we validate that data and convert to a desired class.

data class User(
        val name: String,
        val password: String?,
        val age: Int,
        val score: Double?
)

fun validateUser(input: KoldData): Validated<User> =
        input.validationContext {
            combineFields(
                require("name").validateValue { it.string() },
                opt("password").validateOption { it.string() },
                require("age").validateValue { it.long() },
                opt("score").validateOption { it.double() }
            ) { name, password, age, score ->
                User(name, password, age.toInt(), score)
            }
        }

More examples can be found in kold-examples module.

KoldData

KoldData is a type safe representation of json-like data. It allows you to store strings, numbers, booleans and also arrays and objects containing fields of such types.

Versions

Version
0.2
0.1.3
0.1.2