2

I'm trying to implement some simple data validation with fp-ts, and came across this codesandboxexample:

import * as E from "fp-ts/lib/Either";
import { getSemigroup, NonEmptyArray } from "fp-ts/lib/NonEmptyArray";
import { sequence } from "fp-ts/lib/Array";
import { pipe } from "fp-ts/lib/pipeable";
import { Predicate } from "fp-ts/lib/function";

type ValidationError = NonEmptyArray<string>;

type ValidationResult = E.Either<ValidationError, unknown>;

type ValidationsResult<T> = E.Either<ValidationError, T>;

interface Validator {
  (x: unknown): ValidationResult;
}

interface Name extends String {}

const applicativeV = E.getValidation(getSemigroup<string>());

const validateName: (
  validations: Array<Validator>,
  value: unknown
) => ValidationsResult<Name> = (validations, value) =>
  validations.length
    ? pipe(
        sequence(applicativeV)(validations.map((afb) => afb(value))),
        E.map(() => value as Name)
      )
    : E.right(value as Name);

const stringLengthPredicate: Predicate<unknown> = (v) =>
  typeof v === "string" && v.length > 4;

const lengthAtLeastFour: Validator = E.fromPredicate(
  stringLengthPredicate,
  () => ["value must be a string of at least 5 characters"]
);

const requiredLetterPredicate: Predicate<unknown> = (v) =>
  typeof v === "string" && v.includes("t");

const hasLetterT: Validator = E.fromPredicate(requiredLetterPredicate, () => [
  'value must be a string that includes the letter "t"'
]);

const validations = [hasLetterT, lengthAtLeastFour];

console.log(validateName(validations, "sim"));
// {left: ['value must be a string that includes the letter "t"', 'value must be a string of at least 4 characters']}

console.log(validateName(validations, "timmy"));
// {right: 'timmy'}

What is Predicate? To what effect is it used in this example? I don't see any explanation of what it does in the docs, just that it's part of the API and that it seems to modify a provided interface.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ANimator120
  • 2,556
  • 1
  • 20
  • 52

1 Answers1

5

Predicate<A> = (a:A) => boolean

A predicate is a function that takes an argument and returns a boolean. It's the type taken by filter and also by fromPredicate belonging to many types like Option and Either.

For example,

import { filter } from 'fp-ts/Array'
import { pipe } from 'fp-ts/function'
import { fromPredicate } from 'fp-ts/Either'

const errorIfNotThree = fromPredicate<number>(x => x===3, () => 'not three!')
const f = pipe([1,2,3], filter(x => x===3))

errorIfNotThree(3) // right(3)
errorIfNotThree(5) // left('not three!')
f // [3]

In the above, x => x===3 is of type Predicate<number> because it's a function that takes a number and returns a boolean

user1713450
  • 1,307
  • 7
  • 18