36 lines
1.3 KiB
Kotlin
36 lines
1.3 KiB
Kotlin
package net.thermetics.chargepoint.models
|
|
|
|
import arrow.core.Either
|
|
import arrow.core.EitherNel
|
|
import arrow.core.raise.either
|
|
import arrow.core.raise.ensure
|
|
import java.util.*
|
|
|
|
typealias ValidationResult = EitherNel<ValidationError, TransactionAuthRequest>
|
|
|
|
sealed class ValidationError(override val message: String) : Error(message) {
|
|
data object InvalidStationUuid : ValidationError("Invalid station ID")
|
|
data object IdentifierTooShort : ValidationError("Invalid driver identifier - too short")
|
|
data object IdentifierTooLong : ValidationError("Invalid driver identifier - too long")
|
|
}
|
|
|
|
fun validateStationUuid(stationUuid: String) =
|
|
Either.catch { UUID.fromString(stationUuid) }
|
|
.mapLeft { ValidationError.InvalidStationUuid }
|
|
|
|
fun validateIdentifierNotShort(driver: DriverIdentifier) = either {
|
|
ensure(driver.id.length >= 20) { ValidationError.IdentifierTooShort }
|
|
driver
|
|
}
|
|
|
|
fun validateIdentifierNotLong(driver: DriverIdentifier) = either {
|
|
ensure(driver.id.length <= 80) { ValidationError.IdentifierTooLong }
|
|
driver
|
|
}
|
|
|
|
fun validateRequest(req: TransactionAuthRequest): EitherNel<ValidationError, TransactionAuthRequest> =
|
|
Either.zipOrAccumulate(
|
|
validateStationUuid(req.stationUuid),
|
|
validateIdentifierNotShort(req.driverIdentifier),
|
|
validateIdentifierNotLong(req.driverIdentifier),
|
|
) { _, _, _ -> req } |