Reputation: 173
I've recently used ScalaZ for validation purposes and decided to choose ValidationNel as a fail fast behavior wasn't desired. I had more than 12 validation checks to make, thus I couldn't use operator |@| so I've expressed it with <*> like that:
validatePropertyA(object.propertyA) <*>
(validatePropertyB(object.propertyB) <*>
(validatePropertyC(object.propertyC) map (_ => _ => _ => object)))
Having about 15 properties to check, this code becomes rather complicated and looks at the first glance like something which could be expressed with foldRight instead, but I don't have a clue how to achieve this.
Is foldRight feasible for the job of making this chunk of code concise or do I have to use something else?
Upvotes: 0
Views: 165
Reputation: 23532
You could easily do this using traverse
or sequence
:
List(validatePropertyA(object.propertyA), validatePropertyB(object.propertyB), ...)
.sequence.map(_ => object)
That will give you back a Validated
with all the errors on the left side and the object
on the right side.
Upvotes: 1