digestive-functors- A practical formlet library

Safe HaskellSafe-Infered





type Formlet m v a = Maybe a -> Form m v aSource

type Form v m a = FormTree m v m aSource

Base type for a form.

The three type parameters are:

  • v: the type for textual information, displayed to the user. For example, error messages are of this type. v stands for view.
  • m: the monad in which validators operate. The classical example is when validating input requires access to a database, in which case this m should be an instance of MonadIO.
  • a: the type of the value returned by the form, used for its Applicative instance.

data SomeForm v m Source


forall a . SomeForm (FormTree Identity v m a) 


Show (SomeForm v m) 

(.:) :: Monad m => Text -> Form v m a -> Form v m aSource

Operator to set a name for a subform.

Basic forms

stringRead :: (Monad m, Read a, Show a) => v -> Formlet v m aSource

choice :: Eq a => [(a, v)] -> Formlet v m aSource

Optional forms

optionalStringRead :: (Monad m, Read a, Show a) => v -> Maybe a -> Form v m (Maybe a)Source




:: Monad m 
=> v

Error message (if fail)

-> (a -> Bool)

Validating predicate

-> Form v m a

Form to validate

-> Form v m a

Resulting form

Validate the results of a form with a simple predicate


 check "Can't be empty" (not . null) (string Nothing)

checkM :: Monad m => v -> (a -> m Bool) -> Form v m a -> Form v m aSource

Version of check which allows monadic validations

validate :: Monad m => (a -> Result v b) -> Form v m a -> Form v m bSource

This is an extension of check that can be used to apply transformations that optionally fail

Example: taking the first character of an input string

 head' :: String -> Result String Char
 head' []      = Error "Is empty"
 head' (x : _) = Success x

 char :: Monad m => Form m String Char
 char = validate head' (string Nothing)

validateM :: Monad m => (a -> m (Result v b)) -> Form v m a -> Form v m bSource

Version of validate which allows monadic validations

Lifting forms

monadic :: m (Form v m a) -> Form v m aSource