...

/

Before We Continue: Either or Validation?

Before We Continue: Either or Validation?

Learn why Validation is a useful alternative to Either.

We'll cover the following...

Either vs. Validation

Suppose we receive data from a frontend where a user has just filled in their information. A situation where the user fixes their errors one at a time instead of just getting back a list of all problems might not be ideal. This might lead to user frustration and additional calls to our backend because the data is resubmitted multiple times. This is why Validation can be a useful alternative to Either.

To demonstrate its potential, we’ll rewrite our three checks… The first thing we’ll need is a semigroup for an array of strings. We’ll pass it to the getValidation helper from Either.

Press + to interact
import * as E from 'fp-ts/lib/Either';
import {getSemigroup} from 'fp-ts/lib/NonEmptyArray'
const applicativeValidation = E.getValidation(getSemigroup<string>());

A semigroup is a way of combining two values of the same type. The function is called concat. The product ...