I am currently learning to use ArrowKT and I have the following code for validating an input. I tried to collect all the errors at once and execute the validations in parallel since most of them are done against the database.
return Validated.applicativeNel<ValidationError>()
.tupledN(
validateA(input).toValidatedNel(),
validateB(input).toValidatedNel(),
validateC(input).toValidatedNel(),
validateSlotIsFree(input).toValidatedNel(),
)
.fix()
.map { (a, b, c, _) ->
...
}
private suspend fun validateSlotIsFree(input: CreateDto): Validated<ValidationError.SlotUnavailable, Boolean> {
val exists = appointmentRepository.existsBy...()
return if (exists) true.valid() else ValidationError.SlotUnavailable.invalid()
}
Is there any better way to handle the validation inside validateSlotIsFree
? It looks like I am forced to return something on the right side in case of valid but I would not want to. I was looking for something like an Option
where the value would be the Error and empty would mean that the validation has passed. The problem with this is that Validated.fromOption(...)
would take the value and apply it to the Right and I need the opposite.