Boxed.purs 3.52 KB
Newer Older
arturo's avatar
arturo committed
1 2 3 4 5
module Gargantext.Hooks.FormValidation.Boxed
  ( class Equals, equals
  , class NonEmpty, nonEmpty
  , class Minimum, minimum
  , class Maximum, maximum
6
  , lowercase, uppercase, email, date, number, int
arturo's avatar
arturo committed
7 8 9 10
  ) where

import Gargantext.Prelude

11 12 13
import Data.Int as Int
import Data.Maybe (isNothing)
import Data.Number as Number
arturo's avatar
arturo committed
14 15 16 17 18 19
import Data.String (toLower, toUpper)
import Data.String.CodeUnits (length)
import Data.String.Regex (test)
import Data.Tuple.Nested ((/\))
import Data.Validation.Semigroup (invalid)
import Effect (Effect)
arturo's avatar
arturo committed
20
import Gargantext.Hooks.FormValidation.Types (Field, VForm, emailPattern, datePattern)
arturo's avatar
arturo committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
import Toestand as T

class Eq a <= Equals a where
  equals :: Field -> T.Box a -> T.Box a -> Effect VForm

class NonEmpty a where
  nonEmpty :: Field -> T.Box a -> Effect VForm

class Ord a <= Minimum a where
  minimum :: Field -> T.Box a -> Int -> Effect VForm

class Ord a <= Maximum a where
  maximum :: Field -> T.Box a -> Int -> Effect VForm

-- Regarding String field value

instance equalsString :: Equals String where
  equals field box box' = do
    input  <- T.read box
    input' <- T.read box'
    case unit of
      _
        | (not eq input input') -> pure $ invalid [ field /\ "equals" ]
        | otherwise             -> pure $ pure unit

instance nonEmptyString :: NonEmpty String where
  nonEmpty field = T.read >=> case _ of
    input
      | input == "" -> pure $ invalid [ field /\ "nonEmpty" ]
      | otherwise   -> pure $ pure unit

instance minimumString :: Minimum String where
  minimum field box min = T.read box >>= case _ of
    input
      | (length input) < min -> pure $ invalid [ field /\ "minimum" ]
      | otherwise            -> pure $ pure unit

instance maximumString :: Maximum String where
  maximum field box max = T.read box >>= case _ of
    input
      | (length input) > max -> pure $ invalid [ field /\ "maximum" ]
      | otherwise            -> pure $ pure unit

64 65 66 67 68 69 70 71 72 73 74
-- Regarding Boolean field value

instance equalsBoolean :: Equals Boolean where
  equals field box box' = do
    input  <- T.read box
    input' <- T.read box'
    case unit of
      _
        | (not eq input input') -> pure $ invalid [ field /\ "equals" ]
        | otherwise             -> pure $ pure unit

arturo's avatar
arturo committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
uppercase :: Field -> T.Box String -> Effect VForm
uppercase field = T.read >=> case _ of
  input
    | (toLower input) == input -> pure $ invalid [ field /\ "uppercase" ]
    | otherwise                -> pure $ pure unit

lowercase :: Field -> T.Box String -> Effect VForm
lowercase field = T.read >=> case _ of
  input
    | (toUpper input) == input -> pure $ invalid [ field /\ "lowercase" ]
    | otherwise                -> pure $ pure unit

email :: Field -> T.Box String -> Effect VForm
email field = T.read >=> case _ of
  input
    | (not $ test emailPattern input) -> pure $ invalid [ field /\ "email" ]
    | otherwise                       -> pure $ pure unit
arturo's avatar
arturo committed
92 93 94 95 96 97

date :: Field -> T.Box String -> Effect VForm
date field = T.read >=> case _ of
  input
    | (not $ test datePattern input) -> pure $ invalid [ field /\ "date" ]
    | otherwise                      -> pure $ pure unit
98 99 100 101 102 103 104 105 106 107 108 109

number :: Field -> T.Box String -> Effect VForm
number field = T.read >=> case _ of
  input
    | (isNothing $ Number.fromString input) -> pure $ invalid [ field /\ "number "]
    | otherwise                             -> pure $ pure unit

int :: Field -> T.Box String -> Effect VForm
int field = T.read >=> case _ of
  input
    | (isNothing $ Int.fromString input) -> pure $ invalid [ field /\ "int" ]
    | otherwise                          -> pure $ pure unit