is this an idiomatic way to use `ValidatedNel`? ``...
# arrow
c
is this an idiomatic way to use
ValidatedNel
?
Copy code
fun buildMyObjectObject(token: String?, name: String?): ValidatedNel<Error, MyObject> =
    Validated.fromNullable(token, { MissingField("token")  }).toValidatedNel().zip(
            Validated.fromNullable(name, { MissingField("name")  }).toValidatedNel()) { token, name ->
      MyObject(token, name)
    }
it kind of feels like i "shouldn't" need to call
toValidatedNel
for each
Validated
r
Hi @conner, here is an alternative but at some point you need to tell it how to combine the Non Empty List of errors.
there is a version of zip for ValidatedNel that does not need the Semigroup of Nel but in that one, the one you are using in your example, you need to make sure that the values passed are of type ValidatedNel.
This is is so it knows how to combine the errors found in each non empty list which may be on the left side of the Validation.
You can also do it like this:
since those values are of type
Validated<Nel<Error>>, String>
it knows how to infer the right version of
zip
in which you don’t need to be explicit about the semigroup, but you still need to place the errors in Nel
c
thank you for the help!
👍 1