In https://gist.github.com/satyagraha/897e427bfb5ed203e9d3054ac6705704 I have posted a Scala Cats validation scenario which seems reasonable, but I haven't found a very neat solution.
Essentially, there is a two-stage validation, where individual fields are validated, then a class constructor is called which may throw due to internal checks (in general this may not be under my control to change, hence the exception handling code). We wish to not to call the constructor if any field validation fails, but also combine any constructor failure into the final result. "Fail-fast" is definitely right here for the two-phase check.
This is a kind of flatMap
problem, which the cats.data.Validated
framework appears to handle via the cats.data.Validated#andThen
operation. However I couldn't find a particularly neat solution to the problem as you can see in the code. There are quite a limited number of operations available on a cats.syntax.CartesianBuilder
and is wasn't clear to me how to link it with the andThen
operation.
Any ideas welcome! Note there is a Cats issue https://github.com/typelevel/cats/issues/1343 which possibly is related, not sure.
I would make a helper second-order function to wrap the exception-throwing ones:
Also, default companions of case classes extend the FunctionN trait, so there's no need to do
(User.apply _).tupled
, it can be shortened toUser.tupled
(on custom companions, you need to writeextends ((...) => ...))
butapply
override will be autogenerated)So we end up with that using
andThen
: