Coder Social home page Coder Social logo

nesk / akkurate Goto Github PK

View Code? Open in Web Editor NEW
229.0 6.0 9.0 776 KB

The expressive validation library for Kotlin

Home Page: https://akkurate.dev

License: Apache License 2.0

Kotlin 100.00%
constraints dsl kotlin multiplatform validation validation-library kotlin-library

akkurate's People

Contributors

nesk avatar raulraja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

akkurate's Issues

Support validating nullable iterables

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 { /* ... */ }
}

Support overriding the visibility of the generated accessors

  • we sould ignore all private/protected properties
  • we should generate accessors for internal/public properties
  • a property can be public in a private class, we should be careful with this
  • the accessor should have the same visibility than the property
  • the user should be able to override the visibility of the whole accessors, to force them being internal for example, to avoid leaks into the public API

Expose a helper to test custom constraints

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!

Add basic missing constraints

CharSequence:

  • hasLengthEqualTo, hasLengthNotEqualTo
  • isBlank, isNotBlank
  • regex with isMatching, isNotMatching

All collections:

  • isEmpty, isNotEmpty
  • hasSizeEqualTo, hasSizeNotEqualTo
  • isContaining, isNotContaining

Provide some conditional helpers

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

Generated accessors for mutable properties are improperly cast

@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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.