module Data.Validator
(
ValidationM, ValidationT(..)
, runValidator, runValidatorT
, (+>>)
, minLength, maxLength, lengthBetween, notEmpty
, largerThan, smallerThan, valueBetween
, matchesRegex
, conformsPred, conformsPredM
, HasLength(..), Stringable(..)
, Int64
, re, mkRegexQQ, Regex
)
where
import Control.Applicative
import Control.Monad
import Control.Monad.Identity
import Control.Monad.Trans
import Control.Monad.Trans.Either
import Data.Int
import Data.Stringable hiding (length)
import Text.Regex.PCRE.Heavy
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
type ValidationM e = ValidationT e Identity
newtype ValidationT e m a
= ValidationT { unValidationT :: EitherT e m a }
deriving (Monad, Functor, Applicative, Alternative, MonadPlus, MonadTrans)
runValidator :: (a -> ValidationM e a) -> a -> Either e a
runValidator a b = runIdentity $ runValidatorT a b
runValidatorT :: Monad m => (a -> ValidationT e m a) -> a -> m (Either e a)
runValidatorT validationSteps input =
runEitherT $ unValidationT (validationSteps input)
class HasLength a where
getLength :: a -> Int64
instance HasLength [a] where
getLength = fromIntegral . length
instance HasLength T.Text where
getLength = fromIntegral . T.length
instance HasLength TL.Text where
getLength = TL.length
instance HasLength BS.ByteString where
getLength = fromIntegral . BS.length
instance HasLength BSL.ByteString where
getLength = BSL.length
checkFailed :: Monad m => e -> ValidationT e m a
checkFailed = ValidationT . left
(+>>) :: Monad m => (a -> ValidationT e m a) -> (a -> ValidationT e m a) -> a -> ValidationT e m a
(+>>) m1 m2 a =
m1 a >>= m2
minLength :: (Monad m, HasLength a) => Int64 -> e -> a -> ValidationT e m a
minLength lowerBound e obj =
largerThan lowerBound e (getLength obj) >> return obj
maxLength :: (Monad m, HasLength a) => Int64 -> e -> a -> ValidationT e m a
maxLength upperBound e obj =
smallerThan upperBound e (getLength obj) >> return obj
lengthBetween :: (Monad m, HasLength a) => Int64 -> Int64 -> e -> a -> ValidationT e m a
lengthBetween lowerBound upperBound e obj =
valueBetween lowerBound upperBound e (getLength obj) >> return obj
notEmpty :: (Monad m, HasLength a) => e -> a -> ValidationT e m a
notEmpty = minLength 1
largerThan :: (Monad m, Ord a) => a -> e -> a -> ValidationT e m a
largerThan lowerBound = conformsPred (>= lowerBound)
smallerThan :: (Monad m, Ord a) => a -> e -> a -> ValidationT e m a
smallerThan upperBound = conformsPred (<= upperBound)
valueBetween :: (Monad m, Ord a) => a -> a -> e -> a -> ValidationT e m a
valueBetween lowerBound upperBound e obj =
(largerThan lowerBound e +>> smallerThan upperBound e) obj
conformsPred :: Monad m => (a -> Bool) -> e -> a -> ValidationT e m a
conformsPred predicate e obj =
do unless (predicate obj) $ checkFailed e
return obj
conformsPredM :: Monad m => (a -> m Bool) -> e -> a -> ValidationT e m a
conformsPredM predicate e obj =
do res <- lift $ predicate obj
unless res $ checkFailed e
return obj
matchesRegex :: (Stringable a, Monad m) => Regex -> e -> a -> ValidationT e m a
matchesRegex r = conformsPred (\obj -> obj =~ r)