Skip to content

Instantly share code, notes, and snippets.

@alexandru
Created May 6, 2023 08:30
Embed
What would you like to do?
import arrow.core.Either
import arrow.core.raise.either
import arrow.core.raise.zipOrAccumulate
import arrow.core.right
data class UserPatchRequest(
val email: String? = null,
val password: String? = null,
val timezone: String? = null
)
suspend fun UserPatchRequest.toUserPatch(): Either<BadRequestException, UserPatch> =
either {
return zipOrAccumulate(
{ email?.let { EmailAddress.validated(it).mapLeft { e -> "email" to e }.bind() } },
{ password?.let { Password.validated(it).mapLeft { e -> "password" to e }.bind() } },
{ timezone?.let { UserTimeZone.validated(it).mapLeft { e -> "timezone" to e }.bind() } }
) { email, password, timezone ->
UserPatch(
email = email,
passwordHash = password?.let { BCryptHash.generate(it.value) },
timezone = timezone
).right()
}
}.mapLeft { errors ->
BadRequestException(errors.toMap())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment