Are there any common examples that show how to use...
# arrow
k
Are there any common examples that show how to use
Valid
,
Invalid
and
Either
seamlessly? I'm thinking that this must be a fairly common pattern and would like to understand how others have done this in a simple way: 1. Make request to remote API that returns
Either<ErrorException, ResponseData>
where
ResponseData
is some
data class
2. Validate all of the fields in
ResponseData
such that they can be converted into
Invalid
or
Valid
3. Operate on the happy/sad paths
s
k
It's definitely a step in the right direction
In my head it feels like it should be possible to do something like: API call fails 1. Call external API using
IO
, returning
Either<A, B>
2. API errors
Left<A>
so return a
data class
with all fields set to
Invalid
API call successfully completes, validation fails 1. Call external API using
IO
, returning
Either<A, B>
2. API succeeds with
Right<B>
, so validate all fields 3. Validation fails, so return a
data class
with all fields set to
Invalid
4. This could be extended for partial validation, etc. API completes, Validation completes 1. API returns
Right<B>
2. Validation passes 3.
data class
is returned with all fields set to
Valid
And make that really clean
Using
IO
,
Either
, and
Validated
together feels a bit clunky
t
you could think that Validaded is a especialization of Either with extra utilities to accumulate error if you want to and other things. you could go from IO<Either<A,B>> to IO<Validated<A,B>> back and forth without loosing information (they are Isomorph) you don’t return data class with field set as invalid, you usually model with to branches, one correct and validated, and the other with error/or validation fails. you usually return Valid<Model> or Invalid<ReasonError>
👍 2