From 2741f47ef7b87255203bc2f7f7b2b9140c70b8f0 Mon Sep 17 00:00:00 2001
From: Joris
Date: Thu, 1 Nov 2018 13:14:25 +0100
Subject: Implementing client side validation

---
 common/src/Common/Validation/Atomic.hs | 47 ++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 common/src/Common/Validation/Atomic.hs

(limited to 'common/src/Common/Validation/Atomic.hs')

diff --git a/common/src/Common/Validation/Atomic.hs b/common/src/Common/Validation/Atomic.hs
new file mode 100644
index 0000000..3516668
--- /dev/null
+++ b/common/src/Common/Validation/Atomic.hs
@@ -0,0 +1,47 @@
+module Common.Validation.Atomic
+  ( nonEmpty
+  , minLength
+  , number
+  , nonNullNumber
+  , day
+  ) where
+
+import           Data.Text          (Text)
+import qualified Data.Text          as T
+import           Data.Time.Calendar (Day)
+import           Data.Validation    (Validation)
+import qualified Data.Validation    as V
+import qualified Text.Read          as T
+
+import qualified Common.Msg         as Msg
+import qualified Common.Util.Time   as Time
+
+minLength :: Int -> Text -> Validation Text Text
+minLength l =
+  V.validate
+    (Msg.get (Msg.Form_MinChars l))
+    (\t -> if T.length t >= l then Just t else Nothing)
+
+nonEmpty :: Text -> Validation Text Text
+nonEmpty =
+  V.validate
+    (Msg.get Msg.Form_NonEmpty)
+    (\t -> if (not . T.null $ t) then Just t else Nothing)
+
+number :: Text -> Validation Text Int
+number input =
+  case (T.readMaybe . T.unpack $ input) of
+    Just n -> V.Success n
+    _      -> V.Failure (Msg.get Msg.Form_InvalidInt)
+
+nonNullNumber :: Int -> Validation Text Int
+nonNullNumber =
+  V.validate
+    (Msg.get Msg.Form_NonNullNumber)
+    (\n -> if n /= 0 then Just n else Nothing)
+
+day :: Text ->  Validation Text Day
+day str =
+  case Time.parseDay str of
+    Just d  -> V.Success d
+    Nothing -> V.Failure $ Msg.get Msg.Form_InvalidDate
-- 
cgit v1.2.3