Skip to content

About

A data-type like Either but with an accumulating Applicative

Resources

Stars

105 stars

Watchers

5 watching

Forks

Repository files navigation

Validation

System F Logo

A data type like Either but with an accumulating Applicative instance.

Download from hackage.

Validation

The Validation data type is isomorphic to Either, but has an instance of Applicative that accumulates on the error side. That is to say, if two (or more) errors are encountered, they are appended using a Semigroup operation.

As a consequence of this Applicative instance, there is no corresponding Bind or Monad instance. Validation is an example of, "An applicative functor that is not a monad."

The library provides:

  • Classy optics (GetValidation, HasValidation, ReviewValidation, AsValidation) following the conventions of makeClassy and makeClassyPrisms from lens.
  • Polymorphic prisms (__Failure, __Success) for type-changing operations.
  • Isomorphisms to Either and (Bool, a).

ValidationMonadT

ValidationMonadT err m a is a monad transformer wrapping m (Validation err a). Unlike Validation, it has short-circuiting Applicative, Bind, Monad, and MonadError instances.

ValidationMonad err a is a type alias for ValidationMonadT err Identity a.

Validators

The library provides four validator newtypes, each wrapping a validation function with a different type parameter order to enable different class instances:

Type Wraps Key instances
Validator x err a x -> Validation err a Bifunctor, accumulating Applicative, Either-like Alt
ValidatorProfunctor err x a x -> Validation err a Profunctor, accumulating Applicative
ValidatorMonadT x err f a x -> ValidationMonadT err f a Monad, MonadTrans, BindTrans
ValidatorMonadProfunctorT err f x a x -> ValidationMonadT err f a Profunctor, Monad, Category, Arrow

All four are isomorphic and have cross-type optics instances for converting between them.

Validator is the odd one out in its Alt instance. For Validation and the other three validators, <!> accumulates errors when both sides fail. For Validator, <!> behaves like Either: the first success wins, otherwise the second failure is returned, and no Semigroup constraint is needed. For this reason Validator has no Plus or Alternative instance. Its <> still accumulates errors, so use <> when you want accumulation.

About

A data-type like Either but with an accumulating Applicative

Resources

Stars

105 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages