I am using fp-ts to handle validation in my application. I want to write a function that accepts a list of validators and runs all of them over a single input.
These are the types I want to use:
import { Either } from "fp-ts/lib/Either";
import { NonEmptyArray } from "fp-ts/lib/NonEmptyArray";
type Validation<E, A> = Either<NonEmptyArray<E>, A>;
type Validator<E, A> = (a: A) => Validation<E, A>;
declare function validations<E, A>(vs: Validator<E, A>[], a: A): Validation
Internally I want validations
to be implemented in terms of fp-ts Validation
. I'm following the example, but it requires to know the list of Validator
s statically, when I want to produce that list dynamically.
Notice that for simplicity I'm making the Validation type take a NonEmptyArray of errors, since I'm mainly interested on how to combine the validations from a dynamically constructed array.
Finally, if the list of validations is empty, we can assume the input is valid.
How can I implement validations
using the combinators provided by getValidation
from fp-ts