Skip to content

Instantly share code, notes, and snippets.

@alexandru
Created May 6, 2023 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandru/d5d6042278452d4817715b614a72be27 to your computer and use it in GitHub Desktop.
Save alexandru/d5d6042278452d4817715b614a72be27 to your computer and use it in GitHub Desktop.
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