This is a follow up to my previous question.
Suppose I need to write a function validate
in order to make sure that a given list of strings consists of "a", "b", and one or more "c".
def validate(ss: List[String]): Either[NonEmptyList[MyError], Unit] = ???
Suppose I have three functions to check if a given string is "a", "b", or "c":
def validateA(str: String): Either[MyError, Unit] = ???
def validateB(str: String): Either[MyError, Unit] = ???
def validateC(str: String): Either[MyError, Unit] = ???
How to compose those functions to implement validate
?
One solution is a "parser combinators" approach. Define a monad instance for type Validator = Either[NonEmptyList[MyError], List[String]]
, combinators like oneOrMore
similar to parser combinators etc.
I am wondering whether there is an easier solution.
Looks like you could make use of Scalactic, which allows one to accumulate errors without short-circuiting the whole validation process.
This example looks quite similar to what you are trying to do. Check it out!
I suggest you to leverage
cats
Validated
.Let's define some helper methods, if you really don't want to change your
validateT
methods signatures:Then you can implement a
validate_
helper function:And finally implement your
validate
function as:Assumptions: the input list is sorted and if it is shorter than
3
elements a specific error (e.g. list too short) is acceptable.