All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.neat.Invalid.kt Maven / Gradle / Ivy

The newest version!
package neat

import neat.exceptions.ValidityException

data class Invalid(
    override val value: T,
    val reasons: List
) : Validity {

    constructor(value: T, reason: String) : this(value, listOf(reason))

    fun exception(): ValidityException {
        val size = reasons.size
        val terminator = "error" + if (size > 1) "s" else ""
        return ValidityException(
            message = "You have $size validation $terminator",
            reasons = reasons,
            details = buildString {
                appendLine("Validation Error(s)")
                reasons.forEachIndexed { idx, it ->
                    appendLine("\t${idx + 1}. $it")
                }
            }
        )
    }

    override fun getOrThrow(): T = throw exception()

    override fun  map(transformer: (T) -> R) = Invalid(transformer(value), reasons)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy