Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nesk/akkurate

The expressive validation library for Kotlin
https://github.com/nesk/akkurate

constraints dsl kotlin kotlin-library multiplatform validation validation-library

Last synced: 9 days ago
JSON representation

The expressive validation library for Kotlin

Awesome Lists containing this project

README

        

# Akkurate

[**Get started by reading the documentation**](https://akkurate.dev)

Akkurate is a validation library taking advantage of the expressive power of Kotlin. No need \
for 30+ annotations or complicated custom constraints; write your validation code in Kotlin with a beautiful declarative
API.

Designed from scratch to handle complex business logic, its role is to help you write qualitative and maintainable
validation code.

A code example of Akkurate used to showcase the library on social networks.

> [!WARNING]
> Akkurate is under development and, despite being heavily tested, its API isn't yet stabilized; _breaking changes
> might happen on minor releases._ However, we will always provide migration guides.
>
> Report any issue or bug in the GitHub repository.

## Showcase

Here's an example showcasing how you can constrain a book and its list of authors.

```kotlin
// Define your classes

@Validate
data class Book(
val title: String,
val releaseDate: LocalDateTime,
val authors: List,
)

@Validate
data class Author(val firstName: String, val lastName: String)

// Write your validation rules

val validateBook = Validator {
// First the property, then the constraint, finally the message.
title.isNotEmpty() otherwise { "Missing title" }

releaseDate.isInPast() otherwise { "Release date must be in past" }

authors.hasSizeBetween(1..10) otherwise { "Wrong author count" }

authors.each { // Apply constraints to each author
(firstName and lastName) {
// Apply the same constraint to both properties
isNotEmpty() otherwise { "Missing name" }
}
}
}

// Validate your data

when (val result = validateBook(someBook)) {
is Success -> println("Success: ${result.value}")
is Failure -> {
val list = result.violations
.joinToString("\n") { "${it.path}: ${it.message}" }
println("Failures:\n$list")
}
}
```

Notice how each constraint applied to a property can be read like a sentence. This code:

```kotlin
title.isNotEmpty() otherwise { "Missing title" }
```

can be read:

```text
Check if 'title' is not empty otherwise write "Missing title".
```

## Features

- [**Beautiful DSL and API**](https://akkurate.dev/docs/harness-the-dsl.html) \
Write crystal clear validation code and keep it DRY. Use loops and conditions when
needed; forget about annotation hell.

- [**Bundled with all the essential constraints**](https://akkurate.dev/docs/apply-constraints.html) \
Write custom constraints for your business logic and nothing more. The rest? It's on us!

- [**Easily and highly extendable**](https://akkurate.dev/docs/extend.html) \
No need to write verbose code to create custom constraints, within Akkurate they're as simple as a lambda with
parameters.

- [**Contextual and asynchronous**](https://akkurate.dev/docs/use-external-sources.html) \
Query sync/async data sources whenever you need to, like a database, or a REST API. Everything can happen inside
validation.

- [**Integrated with your favorite tools**](https://akkurate.dev/docs/integrations.html) \
Validate your data within any popular framework, we take care of the integrations for you.

- [**Code once, deploy everywhere**](https://akkurate.dev/docs/getting-started.html#installation) \
Take advantage of Kotlin Multiplatform; write your validation code once, use it both for front-end and back-end
usages.

- [**Testable out of the box**](https://akkurate.dev/docs/extend.html#test-your-code) \
Finding how to test your validation code shouldn't be one of your tasks. You will find all the tools you need to write
good tests.