nesk / akkurate Goto Github PK
View Code? Open in Web Editor NEWThe expressive validation library for Kotlin
Home Page: https://akkurate.dev
License: Apache License 2.0
The expressive validation library for Kotlin
Home Page: https://akkurate.dev
License: Apache License 2.0
The following functions should support nullable iterables:
public operator fun <T> Validatable<Iterable<T>>.iterator(): Iterator<Validatable<T>>
public inline fun <T> Validatable<Iterable<T>>.each(block: Validatable<T>.() -> Unit)
Currently, the following code doesn't work, each
isn't callable:
Validator<List<Any>?> {
each { /* ... */ }
}
This could be an example with a known web framework (ktor? spring?) with a REST API for user registration. The example could also contain an exemple of database validation (error if the username already exists).
There might be some use cases to manually call the plugin compiler. Let's make its API public and ensure its explicitly defined.
This is not necessary and it was left unintentionnally.
It could be useful to transform a value before validating it, for example:
Validator<String> {
this.map { it.trim('.') }.isNotBlank()
}
By "transforming" the value, the path remains the same, until overridden by withPath
.
Essentially, it would work like Optional.map()
in Java, or Either.map()
with Arrow, but instead of returning an Optional
or an Either
, it returns a Validatable
.
Could be useful for optimizations and for Arrow Either
type.
This is missing, especially for enums and sealed classes.
isInstanceOf
isNotInstanceOf
internal
for example, to avoid leaks into the public APIIt's a bad example, we should stick to query a database.
Describe the bug
I'm defining custom constraints and there seems to be no easy way to test them. In the following example, I'd like to create a Validatable<String>
, but without the helpers defined in the _test
package, it's hard to do because the ConstraintRegistry
class is internal.
fun Validatable<String>.isValidTimeZone() = constrain {
kotlin.runCatching { TimeZone.of(it) }.isSuccess
} otherwise {
"Should be a valid time zone"
}
Expected behavior
Would it be possible to expose the helper functions, maybe in a test library so that it's possible to unit test the custom constraints?
fun "test_time_zone_constraint" {
assertTrue(Validatable("America/New_York").isValidTimeZone().satisfied)
}
Thanks!
https://kotlinlang.org/docs/type-safe-builders.html#scope-control-dslmarker
becomes necessary when writing this kind of code:
labels { // this: Validatable<List<String>>
hasSizeLowerThanOrEqualTo(3) // OK
each {
hasLengthGreaterThanOrEqualTo(1) // OK
hasSizeGreaterThanOrEqualTo(1) // OK BUT SHOULDN'T BE (applied to the list, not to the string)
}
}
hasNoDuplicates
CharSequence
:
hasLengthEqualTo
, hasLengthNotEqualTo
isBlank
, isNotBlank
isMatching
, isNotMatching
All collections:
isEmpty
, isNotEmpty
hasSizeEqualTo
, hasSizeNotEqualTo
isContaining
, isNotContaining
Hello
The following library is a complete validation library but unfortunately not updated.
Please check it out and use it.
โhttps://github.com/valiktor/valiktor
According to Library creators' guidelines, we should stop using data classes in the public API.
Impacted classes:
We could provide some conditional helpers :
ifNotNull
ifInstanceOf
Instead of:
if (prop.unwrap() != null) prop { // this: Validatable<SomeClass?>
constrain { it?.someBool == true }
}
we could write:
prop.ifNotNull { // this: Validatable<SomeClass>
constrain { it.someBool }
}
We avoid calling unwrap
, the receiver is automatically defined, and it's no longer nullable.
Some helpers unrelated to type casting could be useful too:
ifNotEmpty
(CharSequence
and iterables)ifNotBlank
ifNotNullOrEmpty
ifNotNullOrBlank
support all classes and interfaces
Due to a bug in Kotlin's compiler, we had to add a unnecessary cast in the generated code. However, when using Akkurate in another project, this could generate a warning from the compiler.
We should add a @Suppress
annotation to those casts.
@Validate
data class Book(var title: String)
should produce:
/**
* [Validatable] accessor of [Book.title]
*/
public val Validatable<Book>.title: Validatable<String>
@JvmName(name = "validatableBookTitle")
get() = validatableOf(Book::title as KMutableProperty1)
/**
* [Validatable] accessor of [Book.title]
*/
public val Validatable<Book?>.title: Validatable<String?>
@JvmName(name = "validatableNullableBookTitle")
get() = validatableOf(Book::title as KMutableProperty1)
instead of:
/**
* [Validatable] accessor of [Book.title]
*/
public val Validatable<Book>.title: Validatable<String>
@JvmName(name = "validatableBookTitle")
get() = validatableOf(Book::title as KProperty1)
// ^ cast error
/**
* [Validatable] accessor of [Book.title]
*/
public val Validatable<Book?>.title: Validatable<String?>
@JvmName(name = "validatableNullableBookTitle")
get() = validatableOf(Book::title as KProperty1)
// ^ cast error
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.