This module defines an applicative functor for applicative validation.
Applicative validation differs from monadic validation using Either
in
that it allows us to collect multiple errors using a Semigroup
, whereas
Either
terminates on the first error.
data V err result
The V
functor, used for applicative validation
The Applicative
instance collects multiple failures in
an arbitrary Semigroup
.
For example:
validate :: Person -> V [Error] Person
validate person = { first: _, last: _, email: _ }
<$> validateName person.first
<*> validateName person.last
<*> validateEmail person.email
invalid :: forall err result. err -> V err result
Fail with a validation error
runV :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
Unpack the V
type constructor, providing functions to handle the error
and success cases.
isValid :: forall err result r. V err result -> Boolean
Test whether validation was successful or not
instance showV :: (Show err, Show result) => Show (V err result)
instance functorV :: Functor (V err)
instance applyV :: (Semigroup err) => Apply (V err)
instance applicativeV :: (Semigroup err) => Applicative (V err)
This module defines an Alternative
instances for
validations that supports errors with multiple alternatives
This validation works exactly as Data.Validation
but uses Semiring
instead of Semigroup
data V err res
The V
functor, used for alternative validation
The Alternative
instance collects multiple failures in
an arbitrary Semiring
.
For example:
import Data.Semiring.Free
validate r :: Person -> V (Free Error) Person
validate person = { first: _, last: _, contact: _}
<$> validateName person.first
<*> validateName person.last
<*> (validateEmail person.contact <|> validatePhone person.contact)
invalid :: forall err result. err -> V err result
Fail with a validation error
runV :: forall err result r. (err -> r) -> (result -> r) -> V err result -> r
Unpack the V
type constructor, providing functions to handle the error
and success cases.
isValid :: forall err result. V err result -> Boolean
Test whether validation was successful or not
instance showV :: (Show err, Show result) => Show (V err result)
instance functorV :: Functor (V err)
instance applyV :: (Semiring err) => Apply (V err)
instance applicativeV :: (Semiring err) => Applicative (V err)
instance altV :: (Semiring err) => Alt (V err)
instance plusV :: (Semiring err) => Plus (V err)
instance alernativeV :: (Semiring err) => Alternative (V err)