on d676bd9 help sign in
5 files changed+61−174
since splitsnapshots
unifiedsplit
.travis.yml+27−14 ChangeLog.md−6 src/Control/Monad/Validation.hs+18−147 test/Doc.hs+1−1 validationt.cabal+15−6
.travis.yml+27−14
@@ -4,35 +4,48 @@ language: haskell
44git:
55 depth: 5
66
7+cabal: "2.4"
8+
79cache:
810 directories:
911 - "$HOME/.cabal/store"
1012 - "$HOME/.stack"
1113 - "$TRAVIS_BUILD_DIR/.stack-work"
1214
13−jobs:
15+matrix:
1416 include:
15− - ghc: 8.6.5
16− env: SNAPSHOT="lts-14.22"
17− - ghc: 8.6.4
18− env: SNAPSHOT="lts-13.19"
19− - ghc: 8.6.3
20− env: SNAPSHOT="lts-13.11"
17+
18+ # Cabal
2119 - ghc: 8.4.4
22− env: SNAPSHOT="lts-12.26"
20+ - ghc: 8.6.5
21+
22+ # Stack
23+ - ghc: 8.6.5
24+ env: STACK_YAML="$TRAVIS_BUILD_DIR/stack.yaml"
2325
2426install:
2527 - |
26− # install stack
27− curl -sSL https://get.haskellstack.org/ | sh
28+ if [ -z "$STACK_YAML" ]; then
29+ ghc --version
30+ cabal --version
31+ cabal new-update
32+ cabal new-build --enable-tests --enable-benchmarks
33+ else
34+ # install stack
35+ curl -sSL https://get.haskellstack.org/ | sh
2836
29− # build project with stack
30− stack --version
31− stack build --system-ghc --test --bench --no-run-tests --no-run-benchmarks --resolver=$SNAPSHOT
37+ # build project with stack
38+ stack --version
39+ stack build --system-ghc --test --bench --no-run-tests --no-run-benchmarks
40+ fi
3241
3342script:
3443 - |
35− stack test --system-ghc
44+ if [ -z "$STACK_YAML" ]; then
45+ cabal new-test --enable-tests
46+ else
47+ stack test --system-ghc
48+ fi
3649
3750notifications:
3851 email: false
ChangeLog.md−6
@@ -1,11 +1,5 @@
11# Revision history for validationt
22
3−## 0.3.0
4−
5−* Added documentation.
6−* Fixed `textErrors` behaviour.
7−* Dropped `transformers-lift` dependency.
8−
93## 0.2.1.0
104
115* GHC 8.4 support
src/Control/Monad/Validation.hs+18−147
@@ -3,38 +3,16 @@
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE TemplateHaskell #-}
55{-# LANGUAGE TypeFamilies #-}
6−{-# LANGUAGE UndecidableInstances #-}
76
8−module Control.Monad.Validation
9− ( ValidationT(..)
10− , runValidationT
11− , runValidationTEither
12− , handleValidationT
13− , vError
14− , vWarning
15− , vErrorL
16− , vWarningL
17− , vZoom
18− , vZoomL
19− , mmSingleton
20− , setMempty
21− , neConcat
22− , textErrors
23− , _MonoidMap
24− , MonoidMap(..)
25− ) where
7+module Control.Monad.Validation where
268
27−import Control.Applicative
289import Control.Lens hiding ((.=))
2910import Control.Monad.Base
3011import Control.Monad.Catch
3112import Control.Monad.Except
32−import Control.Monad.Fail
3313import Control.Monad.State.Strict
34−import Control.Monad.Trans.Control
3514import Data.Aeson
3615import Data.Foldable as F
37−import Data.Functor
3816import Data.List as L
3917import Data.Map.Strict as M
4018import Data.Monoid
@@ -42,32 +20,20 @@ import Data.Text as T
4220import Data.Vector as V
4321import Test.QuickCheck
4422
45−
46−-- | Collects all thrown warnings in 'StateT' and errors
47−-- in 'ExceptT' into a single value using 'Monoid'.
23+-- | Collects all throwed "warnings" throwed through StateT and "errors" throwed
24+-- through ExceptT to single value using Monoid
25+-- FIXME: give more instances like HReaderT and MonadBaseControl/MonadMask
4826newtype ValidationT e m a = ValidationT
4927 { unValidationT :: ExceptT e (StateT e m) a
5028 } deriving ( Functor, Applicative, Monad, MonadThrow, MonadCatch
51− , MonadBase b, Alternative, MonadFix, MonadFail, Contravariant
52− , MonadIO, MonadPlus, MonadBaseControl b, MonadMask )
29+ , MonadBase b )
5330
5431instance MonadTrans (ValidationT e) where
5532 lift = ValidationT . lift . lift
5633
5734-- | Map with 'Monoid' instance which 'mappend' its values
58−--
59−-- This can be used as the `e` in `ValidationT e m a` to provide different
60−-- sets of errors and warnings for different keys.
61−--
62−-- >>> :{
63−-- mconcat
64−-- [ MonoidMap $ M.fromList [(1, "foo"), (2, "hello, "), (3, "oh no")]
65−-- , MonoidMap $ M.fromList [(1, "bar"), (2, "world")]
66−-- ]
67−-- :}
68−-- MonoidMap (fromList [(1,"foobar"),(2,"hello, world"),(3,"oh no")])
6935newtype MonoidMap k v = MonoidMap (Map k v)
70− deriving (Eq, Ord, Show, Arbitrary, Foldable)
36+ deriving (Eq, Ord, Show, Arbitrary)
7137
7238makePrisms ''MonoidMap
7339
@@ -95,7 +61,7 @@ instance (ToJSON k, ToJSON v) => ToJSON (MonoidMap k v) where
9561 , "value" .= v ]
9662
9763instance (Ord k, FromJSON k, FromJSON v) => FromJSON (MonoidMap k v) where
98− parseJSON = withArray "MonoidMap" go
64+ parseJSON v = withArray "MonoidMap" go v
9965 where
10066 go arr = do
10167 keyvals <- traverse fromObj arr
@@ -112,14 +78,14 @@ mmAppend :: (Ord k, Monoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
11278#endif
11379mmAppend (MonoidMap a) (MonoidMap b) = MonoidMap $ M.unionWith (<>) a b
11480
115−-- | Convenient for 'vZoom' as first argument. Will prevent generation
116−-- of map with 'mempty' values.
81+-- | Convenient for 'vZoom' as first artument. Will prevent generation
82+-- of map with 'mempty' values
11783mmSingleton :: (Eq v, Monoid v, Ord k) => k -> v -> MonoidMap k v
11884mmSingleton k v
11985 | v == mempty = mempty
12086 | otherwise = MonoidMap . M.singleton k $ v
12187
122−-- | Sets given value to 'mempty'.
88+-- | Set given value to 'mempty'
12389setMempty :: (Monoid s) => ASetter' s a -> a -> s
12490setMempty setter a = set setter a mempty
12591
@@ -128,44 +94,12 @@ neConcat f a
12894 | F.null a = Nothing
12995 | otherwise = Just $ F.foldr1 f a
13096
131−-- | Returns the strings, concatanated with @", "@ if the list is not empty.
132−--
133−-- Returns Nothing if the list is empty
134−--
135−-- >>> textErrors ["foo", "bar"]
136−-- Just "foo, bar"
137−--
138−-- >>> textErrors ["foo"]
139−-- Just "foo"
140−--
141−-- >>> textErrors []
142−-- Nothing
14397textErrors :: [Text] -> Maybe Text
14498textErrors = neConcat (\a b -> a <> ", " <> b)
14599
146−-- | Returns 'mempty' instead of error if no warnings have occured.
147−-- Returns 'Nothing' as the second element of tuple if computation was
148−-- interrupted by 'vError'.
149−--
150−-- Returns all concatenated errors and warnings and the result if no
151−-- errors have occured (warnings could have occured).
152−--
153−-- >>> :{
154−-- runValidationT $ do
155−-- vWarning ["warning1"]
156−-- vError ["error"]
157−-- vWarning ["warning2"]
158−-- return 8
159−-- :}
160−-- (["error","warning1"],Nothing)
161−--
162−-- >>> :{
163−-- runValidationT $ do
164−-- vWarning ["warning1"]
165−-- vWarning ["warning2"]
166−-- return 8
167−-- :}
168−-- (["warning1","warning2"],Just 8)
100+-- | Returns `mempty` instead of error if no warnings was occured. So, your
101+-- error should have `Eq` instance to detect that any error was occured. Returns
102+-- Nothing for second element of tuple if compuration was interruped by 'vError'
169103runValidationT :: (Monoid e, Monad m) => ValidationT e m a -> m (e, Maybe a)
170104runValidationT (ValidationT m) = do
171105 (res, warnings) <- runStateT (runExceptT m) mempty
@@ -173,25 +107,6 @@ runValidationT (ValidationT m) = do
173107 Left err -> (err <> warnings, Nothing)
174108 Right a -> (warnings, Just a)
175109
176−-- | Like 'runValidationT' but doesn't return the result
177−-- if any warning has occured.
178−--
179−-- >>> :{
180−-- runValidationTEither $ do
181−-- vWarning ["warning1"]
182−-- vError ["error"]
183−-- vWarning ["warning2"]
184−-- return 8
185−-- :}
186−-- Left ["error","warning1"]
187−--
188−-- >>> :{
189−-- runValidationTEither $ do
190−-- vWarning ["warning1"]
191−-- vWarning ["warning2"]
192−-- return 8
193−-- :}
194−-- Left ["warning1","warning2"]
195110runValidationTEither
196111 :: (Monoid e, Eq e, Monad m)
197112 => ValidationT e m a
@@ -202,71 +117,28 @@ runValidationTEither action = do
202117 Just a | err == mempty -> Right a
203118 _ -> Left err
204119
205−-- | Like 'runValidationTEither', but takes an error handler instead of
206−-- returning errors and warnings.
207−--
208−-- >>> :{
209−-- handleValidationT (\_ -> return 11) $ do
210−-- vWarning ["warning1"]
211−-- vError ["error"]
212−-- vWarning ["warning2"]
213−-- return 8
214−-- :}
215−-- 11
216−--
217−-- >>> :{
218−-- handleValidationT (\_ -> return 11) $ do
219−-- vWarning ["warning1"]
220−-- vWarning ["warning2"]
221−-- return 8
222−-- :}
223−-- 11
224120handleValidationT
225121 :: (Monoid e, Monad m, Eq e)
226122 => (e -> m a)
227123 -> ValidationT e m a
228124 -> m a
229−handleValidationT handler action =
125+handleValidationT handler action = do
230126 runValidationTEither action >>= either handler return
231127
232−-- | Stops further execution and appends the given error.
128+-- | Stops further execution of validation
233129vError :: (Monad m) => e -> ValidationT e m a
234130vError e = ValidationT $ throwError e
235131
236−-- | Does not stop further execution and appends the given warning.
132+-- | Does not stop further execution, append warning to
237133vWarning :: (Monad m, Monoid e) => e -> ValidationT e m ()
238134vWarning e = ValidationT $ modify' (<> e)
239135
240−-- | Like 'vError' but allows you to use a setter to insert an error somewhere
241−-- deeper into an empty ('mempty') "e" from "ValidationT e m x", which is then
242−-- combined with all gathered warnings.
243136vErrorL :: (Monad m, Monoid e) => ASetter' e a -> a -> ValidationT e m x
244137vErrorL l a = vError $ setMempty l a
245138
246−-- | Like 'vWarning' but allows you to use a setter to insert an error somewhere
247−-- deeper into an empty ('mempty') "e" from "ValidationT e m x", which is then
248−-- combined with all gathered warnings.
249139vWarningL :: (Monad m, Monoid e) => ASetter' e a -> a -> ValidationT e m ()
250140vWarningL l a = vWarning $ setMempty l a
251141
252−-- | Allows you apply a transformation to the "e" in "ValidationT e m x".
253−--
254−-- >>> :{
255−--runValidationT . vZoom (Data.Map.singleton "password errors") $ do
256−-- vWarning ["warning1"]
257−-- vError ["error"]
258−-- vWarning ["warning2"]
259−-- return 8
260−-- :}
261−-- (fromList [("password errors",["error","warning1"])],Nothing)
262−--
263−-- >>> :{
264−-- runValidationT . vZoom (Data.Map.singleton "password errors") $ do
265−-- vWarning ["warning1"]
266−-- vWarning ["warning2"]
267−-- return 8
268−-- :}
269−-- (fromList [("password errors",["warning1","warning2"])],Just 8)
270142vZoom
271143 :: (Monad m, Monoid a, Monoid b)
272144 => (a -> b)
@@ -276,12 +148,11 @@ vZoom up action = do
276148 (err, res) <- lift $ runValidationT action
277149 case res of
278150 Nothing -> vError $ up err
279− Just a -> vWarning (up err) $> a
151+ Just a -> vWarning (up err) *> return a
280152
281−-- | Like 'vZoom' but takes a setter instead of a function.
282153vZoomL
283154 :: (Monad m, Monoid a, Monoid b)
284155 => ASetter' b a
285156 -> ValidationT a m x
286157 -> ValidationT b m x
287−vZoomL l = vZoom (setMempty l)
158+vZoomL l action = vZoom (setMempty l) action
test/Doc.hs+1−1
@@ -3,4 +3,4 @@ module Main (main) where
33import Test.DocTest
44
55main :: IO ()
6−main = doctest [ "-XOverloadedStrings", "-isrc", "src" ]
6+main = doctest [ "-XOverloadedStrings", "src" ]
validationt.cabal+15−6
@@ -2,7 +2,7 @@
22-- documentation, see http://haskell.org/cabal/users-guide/
33
44name: validationt
5−version: 0.3.0
5+version: 0.2.1.0
66synopsis: Straightforward validation monad.
77description: Convenient solution for validating web forms and APIs.
88homepage: https://github.com/typeable/validationt
@@ -50,12 +50,21 @@ library
5050 -Wmissing-export-lists
5151
5252test-suite doctest
53− default-language: Haskell2010
5453 type: exitcode-stdio-1.0
5554 hs-source-dirs: test
5655 ghc-options: -Wall
5756 main-is: Doc.hs
58− build-depends:
59− base >= 4.10.0.0
60− , doctest >= 0.11.4
61− , validationt
57+ build-depends: base >=4.8 && < 5
58+ , aeson >= 1.0
59+ , doctest
60+ , containers
61+ , exceptions
62+ , lens
63+ , monad-control
64+ , mtl
65+ , QuickCheck
66+ , text
67+ , transformers
68+ , transformers-base
69+ , vector
70+ , doctest