Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

["Request"] DSL method for validation #3485

Open
koswag opened this issue Aug 30, 2024 · 1 comment
Open

["Request"] DSL method for validation #3485

koswag opened this issue Aug 30, 2024 · 1 comment

Comments

@koswag
Copy link

koswag commented Aug 30, 2024

What version are you currently using?

1.2.4

What would you like to see?

Hey guys, first of all I love Arrow and how well it cooperates with Kotlin, hell of a good job!

My main use-case for the Either type is parallel validation. For that I use zipOrAccumulate of course:

either {
    zipOrAccumulate(
        { ensure(conditionA) { ErrorA } },
        { ensure(conditionB) { ErrorB } },
        { ensure(conditionC) { ErrorC } },
    ) { _, _, _ ->
       ResultIfValid(...)
    }
}

It was always quite weird to me that Raise DSL doesn't include a method that would just execute all validation checks ignoring their results, cause usually I just discard trailing lambda arguments, as above. I now implemented something like this:

@RaiseDSL
fun <Error> Raise<NonEmptyList<Error>>.accumulateErrors(
    vararg validationChecks: Raise<Error>.() -> Unit,
) {
    mapOrAccumulate(validationChecks.asIterable()) { check -> check() }
}

// usage:
either {
    accumulateErrors(
        { ensure(conditionA) { ErrorA } },
        { ensure(conditionB) { ErrorB } },
        { ensure(conditionC) { ErrorC } },
    )

    ResultIfValid(...)
}

I find this approach much cleaner, as adding a new validation check doesn't require adding another lambda argument which is usually discarded anyway. It also reads more declaratively I think: "The result is either accumulated errors or ResultIfValid"

The difference is cosmetic, but few people I've been working with, who had no prior experience with Arrow, got quite scared by the zipOrAccumulate syntax (in the first example we don't even zip anything). It's also annoying that the compiler immediately starts to complain when there are not enough lambda parameters, as it makes experimenting and refactoring harder.

@koswag koswag changed the title ["Request"] ["Request"] Parallel validation DSL method Aug 30, 2024
@koswag koswag changed the title ["Request"] Parallel validation DSL method ["Request"] DSL method for validation Sep 9, 2024
@serras
Copy link
Member

serras commented Sep 10, 2024

This is actually work-in-progress in #3436. You'll be able to wrap each of the ensure in accumulate to get the behavior you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants