1
5A simple data validation library. The main idea is to provide an easy way to
6validate web form data by aggregating errors for each field.
10Suppose you want to validate some data from the user. Say, that the password adheres to some rules.
12You might do something like this:
15validatePassword :: Monad m => String -> ValidationT [String] m ()
16validatePassword password = do
21 vLength p = when (length p < 8)
22 $ vWarning ["The password should be at least 8 characters long."]
23 vAlpha p = unless (any isAlpha p)
24 $ vWarning ["The password should contain at least one alphabetic character."]
25 vNum p = unless (any isDigit p)
26 $ vWarning ["The password should contain at least one numeric character."]
29`ValidationT e m a` essentially just gathers the errors thrown by `vWarning`.
32vWarning :: (Monad m, Monoid e) => e -> ValidationT e m ()
35`vWarning` `mappend`s the given `e` to the already collected errors. This is why the warnings are contained inside a list.
37There is also `vError`. The only difference between `vWarning` and `vError` is that `vError` stops further execution (and further collection of errors and warnings).
39You would use the validation like this:
45 result <- runValidationTEither . validatePassword $ password
46 putStrLn $ case result of
47 Left errs -> unlines err
48 Right () -> "You are fine."
51You could, of course, do more complicated things like use `vWarningL` and `vErrorL` to add an error to a `mempty` structure, which gets `mappend`ed with other errors.
53The library comes with a `MonoidMap` `newtype` wrapper around `Map`, which `mappend`s the values themselves on conflict. This can be useful if you have a multiple points of failure and you want to distinguich between them -- validating a username and a password for example:
59 deriving (Eq, Show, Ord)
61validatePassword :: Monad m => String -> ValidationT (MonoidMap Piece [String]) m ()
62validatePassword password = do
67 warning = mmSingleton UserName
68 vLength p = when (length p < 8)
69 $ warning ["The password should be at least 8 characters long."]
70 vAlpha p = unless (any isAlpha p)
71 $ warning ["The password should contain at least one alphabetic character."]
72 vNum p = unless (any isDigit p)
73 $ warning ["The password should contain at least one numeric character."]
76(`mmSingleton` is a covenience initializer for `MonoidMap`.)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list