0

I have a code like this:

  return validators
            .stream()
            .flatMap(v -> v.validate(scoreRequest).stream())
            .collect(toList());

Each validator returns a List<String> of errors, if the list is empty it means that the request is valid.

Now I want to introduce vavr.Validator. The new return type of each validator.validate now is Validation<List<String>,ScoreRequest>

How can I combine the output of each validators using streams like before?

Fabry
  • 1,498
  • 4
  • 23
  • 47
  • I think you have to clarify your requirements. How could we tell you what *you* intend to do with that changed signature?! You changed the signature of that method to return a validation object. Now *you* have to determine how you want to reasonably process that?! – GhostCat Apr 16 '19 at 07:06
  • I just want to combine all the validations performed by each validator. The output of the combine should be: `Validation,ScoreRequest>` – Fabry Apr 16 '19 at 07:10
  • You say that **each** validate() method returns a Validation object. How do you expect to merge those? How do you get from a list of ScoreRequest to a single one?! – GhostCat Apr 16 '19 at 07:31
  • I thought that the Valivation.combine would have done exactly this. Combine the `List` of errors or return the scoreRequest if the request is valid – Fabry Apr 16 '19 at 08:04

1 Answers1

1

You can use sequence to do exactly that.

So if your validators is a List<Validation<List<String>, ScoreRequest>>, applying this function to it will return a Validation<Seq<String>, Seq<ScoreRequest>>, which looks exactly like what you want. I let you figure out the plumbing of converting back the Seqs to whatever structure you want.

Sir4ur0n
  • 1,753
  • 1
  • 13
  • 24