commonMain.neat.Invalid.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neat-validation-jvm Show documentation
Show all versions of neat-validation-jvm Show documentation
A collection library that is built with interoperability in mind
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)
}