validationt-0.3.0: Straightforward validation monad
Safe HaskellNone
LanguageHaskell2010

Control.Monad.Validation

Synopsis

Documentation

newtype ValidationT e m a Source #

Collects all thrown warnings in StateT and errors in ExceptT into a single value using Monoid.

Constructors

ValidationT 

Fields

Instances

Instances details
MonadBase b m => MonadBase b (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

liftBase :: b α -> ValidationT e m α #

MonadBaseControl b m => MonadBaseControl b (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Associated Types

type StM (ValidationT e m) a #

Methods

liftBaseWith :: (RunInBase (ValidationT e m) b -> b a) -> ValidationT e m a #

restoreM :: StM (ValidationT e m) a -> ValidationT e m a #

MonadTrans (ValidationT e) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

lift :: Monad m => m a -> ValidationT e m a #

Monad m => Monad (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

(>>=) :: ValidationT e m a -> (a -> ValidationT e m b) -> ValidationT e m b #

(>>) :: ValidationT e m a -> ValidationT e m b -> ValidationT e m b #

return :: a -> ValidationT e m a #

Functor m => Functor (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

fmap :: (a -> b) -> ValidationT e m a -> ValidationT e m b #

(<$) :: a -> ValidationT e m b -> ValidationT e m a #

MonadFix m => MonadFix (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

mfix :: (a -> ValidationT e m a) -> ValidationT e m a #

MonadFail m => MonadFail (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

fail :: String -> ValidationT e m a #

Monad m => Applicative (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

pure :: a -> ValidationT e m a #

(<*>) :: ValidationT e m (a -> b) -> ValidationT e m a -> ValidationT e m b #

liftA2 :: (a -> b -> c) -> ValidationT e m a -> ValidationT e m b -> ValidationT e m c #

(*>) :: ValidationT e m a -> ValidationT e m b -> ValidationT e m b #

(<*) :: ValidationT e m a -> ValidationT e m b -> ValidationT e m a #

Contravariant m => Contravariant (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

contramap :: (a -> b) -> ValidationT e m b -> ValidationT e m a #

(>$) :: b -> ValidationT e m b -> ValidationT e m a #

(Monad m, Monoid e) => Alternative (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

empty :: ValidationT e m a #

(<|>) :: ValidationT e m a -> ValidationT e m a -> ValidationT e m a #

some :: ValidationT e m a -> ValidationT e m [a] #

many :: ValidationT e m a -> ValidationT e m [a] #

(Monad m, Monoid e) => MonadPlus (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

mzero :: ValidationT e m a #

mplus :: ValidationT e m a -> ValidationT e m a -> ValidationT e m a #

MonadIO m => MonadIO (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

liftIO :: IO a -> ValidationT e m a #

MonadThrow m => MonadThrow (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

throwM :: Exception e0 => e0 -> ValidationT e m a #

MonadCatch m => MonadCatch (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

catch :: Exception e0 => ValidationT e m a -> (e0 -> ValidationT e m a) -> ValidationT e m a #

MonadMask m => MonadMask (ValidationT e m) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

mask :: ((forall a. ValidationT e m a -> ValidationT e m a) -> ValidationT e m b) -> ValidationT e m b #

uninterruptibleMask :: ((forall a. ValidationT e m a -> ValidationT e m a) -> ValidationT e m b) -> ValidationT e m b #

generalBracket :: ValidationT e m a -> (a -> ExitCase b -> ValidationT e m c) -> (a -> ValidationT e m b) -> ValidationT e m (b, c) #

type StM (ValidationT e m) a Source # 
Instance details

Defined in Control.Monad.Validation

type StM (ValidationT e m) a = StM (ExceptT e (StateT e m)) a

runValidationT :: (Monoid e, Monad m) => ValidationT e m a -> m (e, Maybe a) Source #

Returns mempty instead of error if no warnings have occurred. Returns Nothing as the second element of tuple if computation was interrupted by vError.

Returns all concatenated errors and warnings and the result if no errors have occurred (warnings could have occurred).

>>> :{
 runValidationT $ do
   vWarning ["warning1"]
   vError ["error"]
   vWarning ["warning2"]
   return 8
:}
(["error","warning1"],Nothing)
>>> :{
 runValidationT $ do
   vWarning ["warning1"]
   vWarning ["warning2"]
   return 8
:}
(["warning1","warning2"],Just 8)

runValidationTEither :: (Monoid e, Eq e, Monad m) => ValidationT e m a -> m (Either e a) Source #

Like runValidationT but doesn't return the result if any warning has occurred.

>>> :{
 runValidationTEither $ do
   vWarning ["warning1"]
   vError ["error"]
   vWarning ["warning2"]
   return 8
:}
Left ["error","warning1"]
>>> :{
 runValidationTEither $ do
   vWarning ["warning1"]
   vWarning ["warning2"]
   return 8
:}
Left ["warning1","warning2"]

handleValidationT :: (Monoid e, Monad m, Eq e) => (e -> m a) -> ValidationT e m a -> m a Source #

Like runValidationTEither, but takes an error handler instead of returning errors and warnings.

>>> :{
 handleValidationT (\_ -> return 11) $ do
   vWarning ["warning1"]
   vError ["error"]
   vWarning ["warning2"]
   return 8
:}
11
>>> :{
 handleValidationT (\_ -> return 11) $ do
   vWarning ["warning1"]
   vWarning ["warning2"]
   return 8
:}
11

vError :: Monad m => e -> ValidationT e m a Source #

Stops further execution and appends the given error.

vWarning :: (Monad m, Monoid e) => e -> ValidationT e m () Source #

Does not stop further execution and appends the given warning.

vErrorL :: (Monad m, Monoid e) => ASetter' e a -> a -> ValidationT e m x Source #

Like vError but allows you to use a setter to insert an error somewhere deeper into an empty (mempty) "e" from "ValidationT e m x", which is then combined with all gathered warnings.

vWarningL :: (Monad m, Monoid e) => ASetter' e a -> a -> ValidationT e m () Source #

Like vWarning but allows you to use a setter to insert an error somewhere deeper into an empty (mempty) "e" from "ValidationT e m x", which is then combined with all gathered warnings.

vZoom :: (Monad m, Monoid a, Monoid b) => (a -> b) -> ValidationT a m x -> ValidationT b m x Source #

Allows you apply a transformation to the "e" in "ValidationT e m x".

>>> :{
runValidationT . vZoom (Data.Map.singleton "password errors") $ do
 vWarning ["warning1"]
 vError ["error"]
 vWarning ["warning2"]
 return 8
:}
(fromList [("password errors",["error","warning1"])],Nothing)
>>> :{
 runValidationT . vZoom (Data.Map.singleton "password errors") $ do
   vWarning ["warning1"]
   vWarning ["warning2"]
   return 8
:}
(fromList [("password errors",["warning1","warning2"])],Just 8)

vZoomL :: (Monad m, Monoid a, Monoid b) => ASetter' b a -> ValidationT a m x -> ValidationT b m x Source #

Like vZoom but takes a setter instead of a function.

vPromote :: (Monad m, Monoid a, Eq a) => ValidationT a m x -> ValidationT a m x Source #

Turn any warnings the have occurred into errors.

mmSingleton :: (Eq v, Monoid v, Ord k) => k -> v -> MonoidMap k v Source #

Convenient for vZoom as first argument. Will prevent generation of map with mempty values.

setMempty :: Monoid s => ASetter' s a -> a -> s Source #

Sets given value to mempty.

neConcat :: Foldable f => (a -> a -> a) -> f a -> Maybe a Source #

textErrors :: [Text] -> Maybe Text Source #

Returns the strings, concatanated with ", " if the list is not empty.

Returns Nothing if the list is empty

>>> textErrors ["foo", "bar"]
Just "foo, bar"
>>> textErrors ["foo"]
Just "foo"
>>> textErrors []
Nothing

_MonoidMap :: forall k v k v. Iso (MonoidMap k v) (MonoidMap k v) (Map k v) (Map k v) Source #

newtype MonoidMap k v Source #

Map with Monoid instance which mappend its values

This can be used as the e in `ValidationT e m a` to provide different sets of errors and warnings for different keys.

>>> :{
  mconcat
  [ MonoidMap $ M.fromList [(1, "foo"), (2, "hello, "), (3, "oh no")]
  , MonoidMap $ M.fromList [(1, "bar"), (2, "world")]
  ]
:}
MonoidMap (fromList [(1,"foobar"),(2,"hello, world"),(3,"oh no")])

Constructors

MonoidMap (Map k v) 

Instances

Instances details
Foldable (MonoidMap k) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

fold :: Monoid m => MonoidMap k m -> m #

foldMap :: Monoid m => (a -> m) -> MonoidMap k a -> m #

foldMap' :: Monoid m => (a -> m) -> MonoidMap k a -> m #

foldr :: (a -> b -> b) -> b -> MonoidMap k a -> b #

foldr' :: (a -> b -> b) -> b -> MonoidMap k a -> b #

foldl :: (b -> a -> b) -> b -> MonoidMap k a -> b #

foldl' :: (b -> a -> b) -> b -> MonoidMap k a -> b #

foldr1 :: (a -> a -> a) -> MonoidMap k a -> a #

foldl1 :: (a -> a -> a) -> MonoidMap k a -> a #

toList :: MonoidMap k a -> [a] #

null :: MonoidMap k a -> Bool #

length :: MonoidMap k a -> Int #

elem :: Eq a => a -> MonoidMap k a -> Bool #

maximum :: Ord a => MonoidMap k a -> a #

minimum :: Ord a => MonoidMap k a -> a #

sum :: Num a => MonoidMap k a -> a #

product :: Num a => MonoidMap k a -> a #

(Eq k, Eq v) => Eq (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

(==) :: MonoidMap k v -> MonoidMap k v -> Bool #

(/=) :: MonoidMap k v -> MonoidMap k v -> Bool #

(Ord k, Ord v) => Ord (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

compare :: MonoidMap k v -> MonoidMap k v -> Ordering #

(<) :: MonoidMap k v -> MonoidMap k v -> Bool #

(<=) :: MonoidMap k v -> MonoidMap k v -> Bool #

(>) :: MonoidMap k v -> MonoidMap k v -> Bool #

(>=) :: MonoidMap k v -> MonoidMap k v -> Bool #

max :: MonoidMap k v -> MonoidMap k v -> MonoidMap k v #

min :: MonoidMap k v -> MonoidMap k v -> MonoidMap k v #

(Show k, Show v) => Show (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

showsPrec :: Int -> MonoidMap k v -> ShowS #

show :: MonoidMap k v -> String #

showList :: [MonoidMap k v] -> ShowS #

(Ord k, Semigroup v) => Semigroup (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

(<>) :: MonoidMap k v -> MonoidMap k v -> MonoidMap k v #

sconcat :: NonEmpty (MonoidMap k v) -> MonoidMap k v #

stimes :: Integral b => b -> MonoidMap k v -> MonoidMap k v #

(Ord k, Monoid v) => Monoid (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

mempty :: MonoidMap k v #

mappend :: MonoidMap k v -> MonoidMap k v -> MonoidMap k v #

mconcat :: [MonoidMap k v] -> MonoidMap k v #

(Ord k, Arbitrary k, Arbitrary v) => Arbitrary (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

arbitrary :: Gen (MonoidMap k v) #

shrink :: MonoidMap k v -> [MonoidMap k v] #

(ToJSON k, ToJSON v) => ToJSON (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

(Ord k, FromJSON k, FromJSON v) => FromJSON (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Ord k => Ixed (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

ix :: Index (MonoidMap k v) -> Traversal' (MonoidMap k v) (IxValue (MonoidMap k v)) #

Ord k => At (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

Methods

at :: Index (MonoidMap k v) -> Lens' (MonoidMap k v) (Maybe (IxValue (MonoidMap k v))) #

type Index (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

type Index (MonoidMap k v) = k
type IxValue (MonoidMap k v) Source # 
Instance details

Defined in Control.Monad.Validation

type IxValue (MonoidMap k v) = v