aboutsummaryrefslogtreecommitdiff
path: root/src/server/Validation.hs
blob: 9035be72d0aed369194b33c84c9eb2cec6f6d22f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module Validation
  ( nonEmpty
  , number
  ) where

import Data.Text (Text)
import qualified Data.Text as T

nonEmpty :: a -> Text -> Either a Text
nonEmpty x str =
  if T.null str
   then Left x
   else Right str

number :: x -> (Int -> Bool) -> Text -> Either x Int
number x numberForm str =
  case reads (T.unpack str) :: [(Int, String)] of
    (number, _) : _ ->
      if numberForm number
        then Right number
        else Left x
    _ ->
      Left x