Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
This module defines the Validator
data type and helper functions
for creating various validators.
Synopsis
- data Validator err subject
- validate :: Validator err subject -> subject -> Validation err subject
- assert :: (subject -> Bool) -> err -> Validator err subject
- refute :: (subject -> Bool) -> err -> Validator err subject
- ifNothing :: err -> Validator err (Maybe a)
- ifLeft :: err -> Validator err (Either a b)
- class HasSize a where
- ifEmpty :: HasSize subject => err -> Validator err subject
- minSize :: HasSize subject => Int -> err -> Validator err subject
- maxSize :: HasSize subject => Int -> err -> Validator err subject
- class IsOnlyWhiteSpace a where
- isOnlyWhiteSpace :: a -> Bool
- ifBlank :: IsOnlyWhiteSpace subject => err -> Validator err subject
Documentation
data Validator err subject Source #
Datatype for checking if a validation holds for a subject.
- subject can be any data type for which assertions need to be checked.
- err can be any type representing an error, but it will only be possible to combine validators if the error type has a Semigroup instance.
Execute a validator by passing it to the validate
function.
A Validator is both a Semigroup
and a Monoid
, making it possible to combine
smaller validators into larger validators. A combined validator will accumulate
errors from all of it's sub-validators.
validate :: Validator err subject -> subject -> Validation err subject Source #
Runs a validator on a subject.
The result is a Validation
containing all accumulated errors,
or the subject wrapped in a Success
value.
assert :: (subject -> Bool) -> err -> Validator err subject Source #
Creates a validator that will return an error if the given predicate doesn't hold.
Since any predicate can be provided for checking if the subject satisfies certain conditions, this can be used to build your own custom validators.
Usage:
>>>
let validator = assert (> 10) ["too small"]
>>>
validate validator 11
Success 11
>>>
validate validator 1
Failure ["too small"]
refute :: (subject -> Bool) -> err -> Validator err subject Source #
Creates a validator that will return an error if the given predicate holds.
Since any predicate can be provided for checking if the subject satisfies certain conditions, this can be used to build your own custom validators.
Usage:
>>>
let validator = refute (> 10) ["too big"]
>>>
validate validator 11
Failure ["too big"]
>>>
validate validator 1
Success 1
class HasSize a where Source #
Helper typeclass for checking if a value is empty.
ifEmpty :: HasSize subject => err -> Validator err subject Source #
Returns an error if the function returns an "empty" value.
Usage:
>>>
let validator = ifEmpty ["Empty."]
>>>
validate validator []
Failure ["Empty."]
>>>
validate validator [1, 2, 3]
Success [1,2,3]>>>
validate validator (Map.fromList [('a', 1), ('b', 2)])
Success (fromList [('a',1),('b',2)])
minSize :: HasSize subject => Int -> err -> Validator err subject Source #
Returns an error if the value has a size smaller than required.
Usage:
>>>
let validator = minSize 3 ["Too small."]
>>>
validate validator []
Failure ["Too small."]>>>
validate validator [1, 2]
Failure ["Too small."]
>>>
validate validator [1, 2, 3]
Success [1,2,3]
maxSize :: HasSize subject => Int -> err -> Validator err subject Source #
Returns an error if the value has a size smaller than required.
Usage:
>>>
let validator = maxSize 3 ["Too big."]
>>>
validate validator [1, 2, 3, 4]
Failure ["Too big."]
>>>
validate validator [1, 2, 3]
Success [1,2,3]
class IsOnlyWhiteSpace a where Source #
Helper typeclass for checking if a value contains only whitespace characters.
isOnlyWhiteSpace :: a -> Bool Source #
Instances
IsOnlyWhiteSpace String Source # | |
Defined in Data.Validator isOnlyWhiteSpace :: String -> Bool Source # | |
IsOnlyWhiteSpace Text Source # | |
Defined in Data.Validator isOnlyWhiteSpace :: Text -> Bool Source # | |
IsOnlyWhiteSpace Text Source # | |
Defined in Data.Validator isOnlyWhiteSpace :: Text -> Bool Source # |
ifBlank :: IsOnlyWhiteSpace subject => err -> Validator err subject Source #
Returns an error if the function returns a value containing only whitespace.
Usage:
>>>
let validator = ifBlank ["Only whitespace."]
>>>
validate validator " \t \n \r "
Failure ["Only whitespace."]
>>>
validate validator "not empty"
Success "not empty"