I have a bunch of these:
Validation<String, Foo> a;
Validation<String, Foo> b;
Validation<String, Foo> c;
Here are some of their methods:
boolean isValid();
boolean isInvalid(); // === !isValid()
String getError();
Now, I was trying to do this:
Stream.of(a, b, c).reduce(
Validation.valid(foo),
(a, b) -> a.isValid() && b.isValid()
? Validation.valid(foo)
: String.join("; ", a.getError(), b.getError())
);
There's the obvious issue that if only one of a
or b
is in error, then there's a needless ;
. But there's a more serious issue: getError()
throws an exception if the validation is valid.
Is there a way I can write this lambda (or use something else in the io.vavr.control.Validation library) without making all 4 cases (a && b
, a && !b
, !a && b
, !a && !b
) explicit?
EDIT
To be clearer, I wanted a result of Validation<String, Foo>
in the end. I think it behaves like a "monad," in that way, but I'm not sure.