Skip to content

fromPredicate

fromPredicate<E, A>(pred, onFalse): (a) => Validation<E, A>

Defined in: Core/Validation.ts:91

Creates a Validation from a predicate applied to a value. Returns Passed if the predicate passes, Failed from onFalse otherwise.

E

A

(a) => boolean

(a) => E

(a) => Validation<E, A>

const validateName = Validation.fromPredicate(
  (s: string) => s.length > 0,
  () => "Name is required"
);

validateName("Alice"); // Passed("Alice")
validateName("");      // Failed(["Name is required"])