Functionally-Oriented Primary Data Types Validation Library for Kotlin and Java
FPrimary
Functionally-Oriented Primmary Data Types Validation Library for Kotlin.
Demonstration
import arrow.core.ValidatedNel
import arrow.core.invalidNel
import arrow.core.left
import arrow.core.right
import validation.Validation
import validation.error.ErrorAccumulator
import validation.error.InvalidStringError
import validation.error.ValidationError
fun main() {
//===String Validation===
//--By specifying a list of rules
val result: ValidatedNel<ValidationError, String> = Validation.String(" ",
Validation.String::notEmpty, //Rule is met
Validation.String::notBlank, //Rule is not met
Validation.String::startsWithLowerCase, //Rule is not met
).also(::println) //Invalid Result with BlankStringError and FirstNotLowerCapError
//===Int Validation===
//--By specifying a list of operations and accumulating errors in a List
val errors: List<ValidationError> =
ErrorAccumulator<ValidationError>()
.run { Validation.Int.inRange(1, 2, 10) } //Rule is not met --> NotInRangeError
.run { Validation.Int.positive(0) } //Rule is met --> Nothing Happens
.run { Validation.Int.strictlyPositive(0) } //Rule is not met --> NonStriclyPositiveError
.collect() //Colecting all errors --> { NotInRangeError, NonStrictlyPositiveError }
.also(::println)
//===Float Validation===
//--By specifying custom validation rules
Validation.Float(9.0f) {
val isOdd = (it % 2 == 0.0f)
if(isOdd)
it.right()
else
ValidationError("The Number $it is not odd").left()
}.also(::println)
}
Getting Started
To use the library in your kotlin or java project, you need to add these repositories and dependencies in your build.gradle.kts file:
repositories {
mavenCentral()
maven { setUrl("https://jitpack.io") }
}
dependencies {
//Other dependencies
//...
implementation("io.arrow-kt:arrow-core:1.0.1")
implementation("com.github.AmaVic:fprimary:0.6")
}