Skip to content

How to combine two Validation<Error, Unit>? #1292

Answered by louthy
davidyu2023 asked this question in Q&A
Discussion options

You must be logged in to vote

Use the 'coalescing' operator:

    Validation<Error, Unit> mx = ...;
    Validation<Error, Unit> my = ...;

    var mr = mx | my;

I kinda wish I'd used the & operator, but there it is.

You can also use Apply (which is the applicative-functor operator), it allows you to get at the values of the validation if they all succeed, and returns all the errors if any fail.

   var mr = (mx, my).Apply((x, y) => unit);

Finally, if you only care about getting the first error and not all errors:

    var mr = from x in mx
             from y in my
             select unit;

Definitely finally, there's a nice ValidateCreditCard example in the unit-tests that visits all the different approaches to using the

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@davidyu2023
Comment options

@louthy
Comment options

@BrunoJuchli
Comment options

@harrhp
Comment options

Answer selected by davidyu2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment