Jon Bailey
08/05/2023, 4:48 PMeither {
zipOrAccumulate(
{ ensure(title.isNotEmpty()) { EmptyTitle } },
{ ensureNotNull(authors.toNonEmptyListOrNull()) { NoAuthors } }
) { _, _ ->
Book(title, TODO())
}
}
What would be the best way of extracting the title not empty rule so it can be reused elsewhere, like this?
private fun titleNotEmpty(title: String): RaiseAccumulate<BookValidationError>.() -> Unit = {
ensure(title.isNotEmpty()) { EmptyTitle }
}
either {
zipOrAccumulate(
titleNotEmpty(title),
....
Also would I have to have the return type as RaiseAccumulate instead of Raise? I may want to use the rule in a non accumulating setting.
(I know it's contrived but I'm using the example from the docs instead of my own code)simon.vergauwen
08/28/2023, 6:36 AMprivate fun Raise<BookValidationError>.titleNotEmpty(title: String): Unit =
ensure(title.isNotEmpty()) { EmptyTitle }
That way you can just call titleNotEmpty("...")
inside zipOrAccumulate
and still re-use it in a non accumulating setting.