A validator that applies a function x -> Validation err a.
The Applicative instance accumulates errors using Semigroup, like Validation.
Unlike Validation and the other validators, the Alt instance does not accumulate errors: it behaves like Either, returning the first success, or otherwise the second failure. As a consequence there are no Plus or Alternative instances, and <> (which accumulates) differs from <!>.
let Validator f = Validator (\x -> if x > 0 then Success x else Failure ["not positive"]) :: Validator Int [String] Intf 5Success 5
f (-1)Failure ["not positive"]
Constructors
Validator (x -> Validation err a)
Instances31Bifunctor, Swap, Functor, Applicative, Alt, Apply, …
Bifunctor (Validator x)Defined in validation-1.3.3 · Data.Validation.ValidatorExample3 expressions import Data.Bifunctor(Bifunctor(bimap))let Validator f = bimap (map (++ "!")) (+1) (Validator Success :: Validator Int [String] Int)f 10Success 11
Example2 expressions let Validator f = bimap (map (++ "!")) (+1) (Validator (\_ -> Failure ["err"]) :: Validator Int [String] Int)f 0Failure ["err!"]
Swap (Validator x)Defined in validation-1.3.3 · Data.Validation.ValidatorExample3 expressions import Data.Bifunctor.Swap(Swap(swap))let Validator f = swap (Validator (\_ -> Failure "err") :: Validator Int String Int)f 0Success "err"
Example2 expressions let Validator f = swap (Validator (\_ -> Success 1) :: Validator Int String Int)f 0Failure 1
Functor (Validator x err)Defined in validation-1.3.3 · Data.Validation.ValidatorExample2 expressions let Validator f = fmap (+1) (Validator Success :: Validator Int [String] Int)f 10Success 11
Example2 expressions let Validator f = fmap (+1) (Validator (\_ -> Failure ["err"]) :: Validator Int [String] Int)f 10Failure ["err"]
Semigroup err => Applicative (Validator x err)Defined in validation-1.3.3 · Data.Validation.ValidatorAccumulates errors using Semigroup.
Example2 expressions let Validator f = pure 42 :: Validator Int [String] Intf 0Success 42
Example2 expressions let Validator f = pure (+) <*> (Validator (\_ -> Failure ["e1"]) :: Validator Int [String] Int) <*> (Validator (\_ -> Failure ["e2"]) :: Validator Int [String] Int)f 0Failure ["e1","e2"]
Alt (Validator x err)Defined in validation-1.3.3 · Data.Validation.ValidatorFirst success wins, like Either; if both fail, the second failure is returned. Errors are not accumulated, so no Semigroup constraint is required.
This differs from the Alt instances for Validation, ValidatorProfunctor, ValidatorMonadT and ValidatorMonadProfunctorT, which all accumulate errors when both sides fail. Converting a Validator to one of those types (for example with validatorProfunctor) therefore changes the meaning of <!>. Use <> to accumulate errors from two Validators.
Example3 expressions import Data.Functor.Alt(Alt((<!>)))let Validator f = (Validator (\_ -> Success 1) :: Validator Int [String] Int) <!> Validator (\_ -> Success 2)f 0Success 1
Example2 expressions let Validator f = (Validator (\_ -> Success 1) :: Validator Int [String] Int) <!> Validator (\_ -> Failure ["e2"])f 0Success 1
Example2 expressions let Validator f = (Validator (\_ -> Failure ["e1"]) :: Validator Int [String] Int) <!> Validator (\_ -> Success 2)f 0Success 2
Example2 expressions let Validator f = (Validator (\_ -> Failure ["e1"]) :: Validator Int [String] Int) <!> Validator (\_ -> Failure ["e2"])f 0Failure ["e2"]
Example2 expressions let Validator f = (Validator (\_ -> Failure 1) :: Validator Int Int Int) <!> Validator (\_ -> Failure 2)f 0Failure 2
Semigroup err => Apply (Validator x err)Defined in validation-1.3.3 · Data.Validation.ValidatorAccumulates errors using Semigroup.
Example3 expressions import Data.Functor.Apply(Apply((<.>)))let Validator f = Validator (\_ -> Success (+1)) <.> (Validator Success :: Validator Int [String] Int)f 10Success 11
Example2 expressions let Validator f = (Validator (\_ -> Failure ["e1"]) :: Validator Int [String] (Int -> Int)) <.> (Validator (\_ -> Failure ["e2"]) :: Validator Int [String] Int)f 0Failure ["e1","e2"]
Extend (Validator x err)Defined in validation-1.3.3 · Data.Validation.ValidatorLike Validation, a failure is kept: for each input, the result fails where the validator fails, and otherwise succeeds with
fapplied to the validator.Example3 expressions import Data.Functor.Extend(Extend(extended))let Validator f = extended (\_ -> 42) (Validator (\_ -> Success 1) :: Validator Int [String] Int)f 0Success 42
Example2 expressions let Validator f = extended (\_ -> 42) (Validator (\x -> if x > 0 then Success x else Failure ["not positive"]) :: Validator Int [String] Int)f 5Success 42
Example1 expression f (-1)Failure ["not positive"]
Semigroup err => Selective (Validator x err)Defined in validation-1.3.3 · Data.Validation.ValidatorExample3 expressions import Control.Selective(Selective(select))let Validator f = select (Validator (\_ -> Success (Right 1)) :: Validator Int [String] (Either Int Int)) (pure (+1))f 0Success 1
Example2 expressions let Validator f = select (Validator (\_ -> Success (Left 1)) :: Validator Int [String] (Either Int Int)) (pure (+1))f 0Success 2
Generic (Validator x err a)Defined in validation-1.3.3 · Data.Validation.ValidatorSemigroup err => Semigroup (Validator x err a)Defined in validation-1.3.3 · Data.Validation.ValidatorFirst success wins; two failures accumulate. Unlike <!> for Validator, this requires Semigroup on
err.Example2 expressions let Validator f = (Validator (\_ -> Failure ["e1"]) :: Validator Int [String] Int) <> Validator (\_ -> Failure ["e2"])f 0Failure ["e1","e2"]
Example2 expressions let Validator f = (Validator (\_ -> Success 1) :: Validator Int [String] Int) <> Validator (\_ -> Failure ["e2"])f 0Success 1
Monoid err => Monoid (Validator x err a)Defined in validation-1.3.3 · Data.Validation.ValidatorExample2 expressions let Validator f = mempty :: Validator Int [String] Intf 0Failure []
Wrapped (Validator x err a)Defined in validation-1.3.3 · Data.Validation.ValidatorExample3 expressions import Control.Lens(view, _Wrapped')let v = Validator (\x -> Success (x + 1)) :: Validator Int [String] Int(view _Wrapped' v) 10Success 11
AsValidator (Validator x err a) x err aDefined in validation-1.3.3 · Data.Validation.ValidatorAsValidatorProfunctor (Validator x err a) err x aDefined in validation-1.3.3 · Data.Validation.ValidatorGetValidator (Validator x err a) x err aDefined in validation-1.3.3 · Data.Validation.ValidatorGetValidatorProfunctor (Validator x err a) err x aDefined in validation-1.3.3 · Data.Validation.ValidatorHasValidator (Validator x err a) x err aDefined in validation-1.3.3 · Data.Validation.ValidatorHasValidatorProfunctor (Validator x err a) err x aDefined in validation-1.3.3 · Data.Validation.ValidatorReviewValidator (Validator x err a) x err aDefined in validation-1.3.3 · Data.Validation.ValidatorReviewValidatorProfunctor (Validator x err a) err x aDefined in validation-1.3.3 · Data.Validation.ValidatorAsValidatorMonadProfunctorT (Validator x err a) err Identity x aDefined in validation-1.3.3 · Data.Validation.ValidatorAsValidatorMonadT (Validator x err a) x err Identity aDefined in validation-1.3.3 · Data.Validation.ValidatorGetValidatorMonadProfunctorT (Validator x err a) err Identity x aDefined in validation-1.3.3 · Data.Validation.ValidatorGetValidatorMonadT (Validator x err a) x err Identity aDefined in validation-1.3.3 · Data.Validation.ValidatorHasValidatorMonadProfunctorT (Validator x err a) err Identity x aDefined in validation-1.3.3 · Data.Validation.ValidatorHasValidatorMonadT (Validator x err a) x err Identity aDefined in validation-1.3.3 · Data.Validation.ValidatorReviewValidatorMonadProfunctorT (Validator x err a) err Identity x aDefined in validation-1.3.3 · Data.Validation.ValidatorReviewValidatorMonadT (Validator x err a) x err Identity aDefined in validation-1.3.3 · Data.Validation.ValidatorRewrapped (Validator x err a) (Validator x' err' b)Defined in validation-1.3.3 · Data.Validation.Validatortype Rep (Validator x err a) = D1 ('MetaDataDefined in validation-1.3.3 · Data.Validation.Validator"Validator"
"Data.Validation.Validator"
"validation-1.3.3-30svjXBZxFa33c5ZrZGHG7"
'True) (C1 ('MetaCons"Validator"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (x -> Validation err a))))type Unwrapped (Validator x err a) = x -> Validation err aDefined in validation-1.3.3 · Data.Validation.Validator