aboutsummaryrefslogtreecommitdiff
path: root/client/src/Util
diff options
context:
space:
mode:
authorJoris2018-11-01 13:14:25 +0100
committerJoris2019-08-04 21:14:32 +0200
commit2741f47ef7b87255203bc2f7f7b2b9140c70b8f0 (patch)
treeea5f685cdf8f3de2efa1113325d45faaa90c977e /client/src/Util
parent86957359ecf54c205aee1c09e151172c327e987a (diff)
Implementing client side validation
Diffstat (limited to 'client/src/Util')
-rw-r--r--client/src/Util/Validation.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/client/src/Util/Validation.hs b/client/src/Util/Validation.hs
new file mode 100644
index 0000000..e2a3dcb
--- /dev/null
+++ b/client/src/Util/Validation.hs
@@ -0,0 +1,37 @@
+module Util.Validation
+ ( fireValidation
+ , fireMaybe
+ , nelError
+ ) where
+
+import Control.Monad (join)
+import Data.List.NonEmpty (NonEmpty)
+import qualified Data.List.NonEmpty as NEL
+import Data.Text (Text)
+import Data.Validation (Validation (Failure, Success))
+import qualified Data.Validation as Validation
+import Reflex.Dom (Dynamic, Event, Reflex)
+import qualified Reflex.Dom as R
+
+nelError :: Validation a b -> Validation (NonEmpty a) b
+nelError = Validation.validation (Failure . NEL.fromList . (:[])) Success
+
+fireValidation
+ :: forall t a b c. Reflex t
+ => Dynamic t (Maybe (Validation a b))
+ -> Event t c
+ -> Event t b
+fireValidation value validate =
+ R.fmapMaybe
+ (join . fmap (Validation.validation (const Nothing) Just))
+ (R.tag (R.current value) validate)
+
+fireMaybe
+ :: forall t a b. Reflex t
+ => Dynamic t (Maybe a)
+ -> Event t b
+ -> Event t a
+fireMaybe value validate =
+ R.fmapMaybe
+ id
+ (R.tag (R.current value) validate)