-
Notifications
You must be signed in to change notification settings - Fork 3
/
Validation.purs
48 lines (42 loc) 路 1.43 KB
/
Validation.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
module HTTPurple.Validation
( fromValidated
, fromValidatedE
) where
import Prelude
import Control.Monad.Cont (ContT(..))
import Data.Either (Either, either)
import Effect.Aff.Class (class MonadAff)
import HTTPurple.Response (Response, badRequest)
fromValidatedContinuation ::
forall input err validated m.
MonadAff m =>
(input -> Either err validated) ->
(err -> m Response) ->
input ->
(validated -> m Response) ->
m Response
fromValidatedContinuation validate errorHandler input handler =
either errorHandler handler $ validate $ input
defaultErrorHandler :: forall (err :: Type) (m :: Type -> Type). MonadAff m => err -> m Response
defaultErrorHandler = const $ badRequest ""
-- | Validate an input using a validate function.
-- | If validation fails, the error handler is called.
-- | Returns a continuation
fromValidatedE ::
forall input err validated m.
MonadAff m =>
(input -> Either err validated) ->
(err -> m Response) ->
input ->
ContT Response m validated
fromValidatedE validate errorHandler input = ContT $ fromValidatedContinuation validate errorHandler input
-- | Validate an input using a validate function.
-- | If validation fails, a bad request is resturned.
-- | Returns a continuation
fromValidated ::
forall input err validated m.
MonadAff m =>
(input -> Either err validated) ->
input ->
ContT Response m validated
fromValidated validator = fromValidatedE validator defaultErrorHandler