Skip to content

Instantly share code, notes, and snippets.

@lucapiccinelli
Last active January 3, 2021 14:40
Show Gist options
  • Save lucapiccinelli/9064ab3f88dad5f7275c143e7dcfc689 to your computer and use it in GitHub Desktop.
Save lucapiccinelli/9064ab3f88dad5f7275c143e7dcfc689 to your computer and use it in GitHub Desktop.
import io.konad.Result
inline class NotEmptyString
@Deprecated(level = DeprecationLevel.ERROR, message = "use companion method 'of'")
constructor(val value: String){
companion object{
@Suppress("DEPRECATION_ERROR")
fun of(value: String): Result<NotEmptyString> = valikate {
validate(NotEmptyString(value)){
validate(NotEmptyString::value).isNotBlank()
}
}
}
}
internal inline fun <reified T> valikate(valikateFn: () -> T): Result<T> = try{
valikateFn().ok()
}catch (ex: ConstraintViolationException){
ex.constraintViolations
.mapToMessage()
.joinToString("\n") { "\t\"${it.value}\" of ${T::class.simpleName}.${it.property}: ${it.message}" }
.error()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment