Skip to content

Validation

Validation<E, A> = Valid<A> | Invalid<E>

Defined in: Core/Validation.ts:29

Validation represents a value that is either valid with a success value, or invalid with accumulated errors. Unlike Result, Validation can accumulate multiple errors instead of short-circuiting.

Use Validation when you need to collect all errors (e.g., form validation). Use Result when you want to fail fast on the first error.

E

A

const validateName = (name: string): Validation<string, string> =>
  name.length > 0 ? Validation.of(name) : Validation.fail("Name is required");

const validateAge = (age: number): Validation<string, number> =>
  age >= 0 ? Validation.of(age) : Validation.fail("Age must be positive");

// Accumulates all errors using ap
pipe(
  Validation.of((name: string) => (age: number) => ({ name, age })),
  Validation.ap(validateName("")),
  Validation.ap(validateAge(-1))
);
// Invalid(["Name is required", "Age must be positive"])