Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
A data type similar to Data.Either
that accumulates failures.
- data Validation err a
- validate :: Validate v => e -> (a -> Bool) -> a -> v e a
- validationNel :: Either e a -> Validation (NonEmpty e) a
- fromEither :: Either e a -> Validation e a
- liftError :: (b -> e) -> Either b a -> Validation e a
- validation :: (e -> c) -> (a -> c) -> Validation e a -> c
- toEither :: Validation e a -> Either e a
- orElse :: Validate v => v e a -> a -> a
- valueOr :: Validate v => (e -> a) -> v e a -> a
- ensure :: Validate v => e -> (a -> Bool) -> v e a -> v e a
- codiagonal :: Validation a a -> a
- validationed :: Validate v => (v e a -> v e' a') -> Validation e a -> Validation e' a'
- bindValidation :: Validation e a -> (a -> Validation e b) -> Validation e b
- _Failure :: Validate f => Prism (f e1 a) (f e2 a) e1 e2
- _Success :: Validate f => Prism (f e a) (f e b) a b
- class Validate f where
- revalidate :: (Validate f, Validate g) => Iso (f e1 s) (f e2 t) (g e1 s) (g e2 t)
Data type
data Validation err a Source #
An Validation
is either a value of the type err
or a
, similar to Either
. However,
the Applicative
instance for Validation
accumulates errors using a Semigroup
on err
.
In contrast, the Applicative
for Either
returns only the first error.
A consequence of this is that Validation
has no Bind
or Monad
instance. This is because
such an instance would violate the law that a Monad's ap
must equal the
Applicative
's <*>
An example of typical usage can be found here.
Bitraversable Validation Source # | |
Bifoldable Validation Source # | |
Bifunctor Validation Source # | |
Swapped Validation Source # | |
Validate Validation Source # | |
Functor (Validation err) Source # | |
Semigroup err => Applicative (Validation err) Source # | |
Foldable (Validation err) Source # | |
Traversable (Validation err) Source # | |
Semigroup err => Apply (Validation err) Source # | |
Alt (Validation err) Source # | |
(Eq a, Eq err) => Eq (Validation err a) Source # | |
(Data a, Data err) => Data (Validation err a) Source # | |
(Ord a, Ord err) => Ord (Validation err a) Source # | |
(Show a, Show err) => Show (Validation err a) Source # | |
Generic (Validation err a) Source # | |
Semigroup e => Semigroup (Validation e a) Source # | |
Monoid e => Monoid (Validation e a) Source # | |
(NFData e, NFData a) => NFData (Validation e a) Source # | |
type Rep (Validation err a) Source # | |
Constructing validations
validate :: Validate v => e -> (a -> Bool) -> a -> v e a Source #
validate
s the a
with the given predicate, returning e
if the predicate does not hold.
This can be thought of as having the less general type:
validate :: e -> (a -> Bool) -> a -> Validation e a
validationNel :: Either e a -> Validation (NonEmpty e) a Source #
validationNel
is liftError
specialised to NonEmpty
lists, since
they are a common semigroup to use.
fromEither :: Either e a -> Validation e a Source #
Converts from Either
to Validation
.
liftError :: (b -> e) -> Either b a -> Validation e a Source #
liftError
is useful for converting an Either
to an Validation
when the Left
of the Either
needs to be lifted into a Semigroup
.
Functions on validations
validation :: (e -> c) -> (a -> c) -> Validation e a -> c Source #
validation
is the catamorphism for Validation
.
toEither :: Validation e a -> Either e a Source #
Converts from Validation
to Either
.
orElse :: Validate v => v e a -> a -> a Source #
v
returns orElse
aa
when v
is Failure, and the a
in Success a
.
This can be thought of as having the less general type:
orElse :: Validation e a -> a -> a
valueOr :: Validate v => (e -> a) -> v e a -> a Source #
Return the a
or run the given function over the e
.
This can be thought of as having the less general type:
valueOr :: (e -> a) -> Validation e a -> a
ensure :: Validate v => e -> (a -> Bool) -> v e a -> v e a Source #
ensure
leaves the validation unchanged when the predicate holds, or
fails with e
otherwise.
This can be thought of as having the less general type:
ensure :: e -> (a -> Bool) -> Validation e a -> Validation e a
codiagonal :: Validation a a -> a Source #
codiagonal
gets the value out of either side.
validationed :: Validate v => (v e a -> v e' a') -> Validation e a -> Validation e' a' Source #
Run a function on anything with a Validate instance (usually Either) as if it were a function on Validation
This can be thought of as having the type
(Either e a -> Either e' a') -> Validation e a -> Validation e' a'
bindValidation :: Validation e a -> (a -> Validation e b) -> Validation e b Source #
bindValidation
binds through an Validation, which is useful for
composing Validations sequentially. Note that despite having a bind
function of the correct type, Validation is not a monad.
The reason is, this bind does not accumulate errors, so it does not
agree with the Applicative instance.
There is nothing wrong with using this function, it just does not make a
valid Monad
instance.
Prisms
These prisms are useful for writing code which is polymorphic in its choice of Either or Validation. This choice can then be made later by a user, depending on their needs.
An example of this style of usage can be found here
_Failure :: Validate f => Prism (f e1 a) (f e2 a) e1 e2 Source #
This prism generalises _Left
. It targets the failure case of either Either
or Validation
.
_Success :: Validate f => Prism (f e a) (f e b) a b Source #
This prism generalises _Right
. It targets the success case of either Either
or Validation
.
Isomorphisms
class Validate f where Source #
The Validate
class carries around witnesses that the type f
is isomorphic
to Validation, and hence isomorphic to Either.
_Validation :: Iso (f e a) (f g b) (Validation e a) (Validation g b) Source #
_Either :: Iso (f e a) (f g b) (Either e a) (Either g b) Source #
revalidate :: (Validate f, Validate g) => Iso (f e1 s) (f e2 t) (g e1 s) (g e2 t) Source #
revalidate
converts between any two instances of Validate
.