-
-
Save alexandru/d5d6042278452d4817715b614a72be27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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