Kold

Validation library for Kotlin

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

com.github.kotlin-kold
ArtifactId

ArtifactId

kold-core
Last Version

Last Version

0.2
Release Date

Release Date

Type

Type

pom
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-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.kotlin-kold/kold-core/ -->
<dependency>
    <groupId>com.github.kotlin-kold</groupId>
    <artifactId>kold-core</artifactId>
    <version>0.2</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.kotlin-kold/kold-core/
implementation 'com.github.kotlin-kold:kold-core:0.2'
// https://jarcasting.com/artifacts/com.github.kotlin-kold/kold-core/
implementation ("com.github.kotlin-kold:kold-core:0.2")
'com.github.kotlin-kold:kold-core:pom:0.2'
<dependency org="com.github.kotlin-kold" name="kold-core" rev="0.2">
  <artifact name="kold-core" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.github.kotlin-kold', module='kold-core', version='0.2')
)
libraryDependencies += "com.github.kotlin-kold" % "kold-core" % "0.2"
[com.github.kotlin-kold/kold-core "0.2"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

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
0.1.1