contracheck-applicative-0.1.0.1: Validation types/typeclass based on the contravariance.
Copyright(c) Fabian Birkmann 2020
LicenseGPL-3
Maintainer99fabianb@sis.gl
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Control.Validation.Check

Description

Types and functions to check properties of your data. To make best use of these functions you should check out Data.Functor.Contravariant. For documentation see the (README)[https:/gitlab.comBirkmannvalidation-check-blobmaster/README.md].

Synopsis

Unvalidated values

A newtype around unvalidated values so one cannot use the value until it is validated. You can create an Unvalidated via unvalidated, but it is often more convient If for example you have a JSON api and want to validate incoming data, you can write (using `-XStandaloneDeriving, -XDerivingStrategies, -XDerivingVia`):

import Data.Aeson(FromJSON)
deriving via (a :: Type) instance (FromJSON a) => FromJSON (Unvalidated a)

newtype Unvalidated (a :: Type) Source #

Constructors

Unvalidated 

Fields

Instances

Instances details
Functor Unvalidated Source # 
Instance details

Defined in Control.Validation.Check

Methods

fmap :: (a -> b) -> Unvalidated a -> Unvalidated b #

(<$) :: a -> Unvalidated b -> Unvalidated a #

Eq a => Eq (Unvalidated a) Source # 
Instance details

Defined in Control.Validation.Check

Ord a => Ord (Unvalidated a) Source # 
Instance details

Defined in Control.Validation.Check

Show a => Show (Unvalidated a) Source # 
Instance details

Defined in Control.Validation.Check

Generic (Unvalidated a) Source # 
Instance details

Defined in Control.Validation.Check

Associated Types

type Rep (Unvalidated a) :: Type -> Type #

Methods

from :: Unvalidated a -> Rep (Unvalidated a) x #

to :: Rep (Unvalidated a) x -> Unvalidated a #

type Rep (Unvalidated a) Source # 
Instance details

Defined in Control.Validation.Check

type Rep (Unvalidated a) = D1 ('MetaData "Unvalidated" "Control.Validation.Check" "contracheck-applicative-0.1.0.1-inplace" 'True) (C1 ('MetaCons "Unvalidated" 'PrefixI 'True) (S1 ('MetaSel ('Just "unsafeValidate") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))

Types for checks

Check results

The result of (possibly many) checks. It is either valid or a sequence of all the errors that occurred during the check. The semigroup operation is eager to collect all possible erros.

data CheckResult (e :: Type) Source #

Constructors

Passed 
Failed (Seq e) 

Instances

Instances details
Functor CheckResult Source # 
Instance details

Defined in Control.Validation.Check

Methods

fmap :: (a -> b) -> CheckResult a -> CheckResult b #

(<$) :: a -> CheckResult b -> CheckResult a #

Eq e => Eq (CheckResult e) Source # 
Instance details

Defined in Control.Validation.Check

Show e => Show (CheckResult e) Source # 
Instance details

Defined in Control.Validation.Check

Generic (CheckResult e) Source # 
Instance details

Defined in Control.Validation.Check

Associated Types

type Rep (CheckResult e) :: Type -> Type #

Methods

from :: CheckResult e -> Rep (CheckResult e) x #

to :: Rep (CheckResult e) x -> CheckResult e #

Semigroup (CheckResult e) Source # 
Instance details

Defined in Control.Validation.Check

Monoid (CheckResult e) Source # 
Instance details

Defined in Control.Validation.Check

type Rep (CheckResult e) Source # 
Instance details

Defined in Control.Validation.Check

type Rep (CheckResult e) = D1 ('MetaData "CheckResult" "Control.Validation.Check" "contracheck-applicative-0.1.0.1-inplace" 'False) (C1 ('MetaCons "Passed" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Failed" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Seq e))))

checkResult :: a -> (Seq e -> a) -> CheckResult e -> a Source #

A fold for CheckResult

failsNoMsg :: CheckResult e Source #

Throwing an error without a message.

checkResultToEither Source #

Arguments

:: a

default value

-> CheckResult e 
-> Either (Seq e) a 

The Check type

The type of a (lifted) check. A Check takes an unvalidated data and produces a CheckResult. It may need an additional context m. If the context is trivial (`m ≡ Identity`) helper types/functions are prefixed by a `'`. A Check is not a validation function, as it does not produce any values (to validated data using a Check use validateBy). The reason for this is that it gives Check some useful instances, as it now is contravariant in a and not invariant in a like e.g. `a -> Either b a`

  • Contravariant
newtype Even = Even { getEven :: Int }
checkEven :: Check' Text Even
checkEven = (== 0) . (`mod` 2) . getEven ?> mappend "Number is not even: " . show

newtype Odd = Odd { getOdd :: Int }
checkOdd :: Check' Text Odd
checkOdd = Even . (+1) . getOdd >$< checkEven
  • Semigroup/Monoid: Allows for easy composition of checks
newtype EvenAndOdd = EvenAndOdd { getEvenAndOdd :: Int }
checkevenAndOdd :: Check' Text EvenAndOdd
checkEvenAndOdd = contramap (Even . getEvenAndOdd) checkEven
                  <> contramap (Odd . getEvenAndOdd) checkOdd
  • MFunctor: Changing the effect
import Data.List(isPrefixOf)
newtype Url = Url { getUrl :: String }

check404 :: Check () IO Url -- checks if the url returns 404

checkHttps :: Check' () Identity Url
checkHttps = ("https" `isPrefixOf`) ?>> ()

checkUrl :: Check () IO Url
checkUrl = check404 <> hoist generalize checkHttps

For more information see the README.

newtype Check (e :: Type) (m :: Type -> Type) (a :: Type) Source #

Constructors

Check 

Fields

Instances

Instances details
MFunctor (Check e :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Control.Validation.Check

Methods

hoist :: forall m n (b :: k). Monad m => (forall a. m a -> n a) -> Check e m b -> Check e n b #

Contravariant (Check e m) Source # 
Instance details

Defined in Control.Validation.Check

Methods

contramap :: (a -> b) -> Check e m b -> Check e m a #

(>$) :: b -> Check e m b -> Check e m a #

Applicative m => Divisible (Check e m) Source # 
Instance details

Defined in Control.Validation.Check

Methods

divide :: (a -> (b, c)) -> Check e m b -> Check e m c -> Check e m a #

conquer :: Check e m a #

Applicative m => Decidable (Check e m) Source # 
Instance details

Defined in Control.Validation.Check

Methods

lose :: (a -> Void) -> Check e m a #

choose :: (a -> Either b c) -> Check e m b -> Check e m c -> Check e m a #

Applicative m => Semigroup (Check e m a) Source # 
Instance details

Defined in Control.Validation.Check

Methods

(<>) :: Check e m a -> Check e m a -> Check e m a #

sconcat :: NonEmpty (Check e m a) -> Check e m a #

stimes :: Integral b => b -> Check e m a -> Check e m a #

Applicative m => Monoid (Check e m a) Source # 
Instance details

Defined in Control.Validation.Check

Methods

mempty :: Check e m a #

mappend :: Check e m a -> Check e m a -> Check e m a #

mconcat :: [Check e m a] -> Check e m a #

type Check' e = Check e Identity Source #

A Check with a trivial context

passOnRight :: Applicative m => (a -> Either b ()) -> Check e m b -> Check e m a Source #

'passOnRight ignoreWhen check lets the argument pass when ignoreWhen returns Nothing and otherwise checks with check. It is a special case of choose from Decidable. It gives an example for how Checks expand to other datatypes since they are Divisible and Decidable, see generalizing a check to lists: > > checkList :: Applicative m => Check e m a -> Check e m [a] > checkList c = passOnRight (case > [] -> Right () > x:xs -> Left (x, xs)) > ( divide id c (checkList c))

mapError :: Functor m => (e -> e') -> Check e m a -> Check e' m a Source #

Mapping over the error type.

generalizeCheck :: Applicative m => Check' e a -> Check e m a Source #

Generalize a Check without context to any Check with an applicative context

validateBy :: Functor m => Check e m a -> Unvalidated a -> m (Either (Seq e) a) Source #

Validate Unvalidated data using a check.

validateBy' :: Check' e a -> Unvalidated a -> Either (Seq e) a Source #

validateBy for trivial context.

Constructing checks

Constructing a check from a predicate. Naming conventions:

  • Functions that work on trivial contexts are prefixed by an apostrophe `'`.
  • Check constructors that discard the argument on error end with `_`.
  • All infix operators start with ? and end with > (So ?> is the "normal" version).
  • Additional >: discards its argument: ?>>, ?~>>.
  • Tilde works with non-trivial contexts: ?~>, ?~>>.

checking :: (a -> m (CheckResult e)) -> Check e m a Source #

General construction function for checks.

checking' :: (a -> CheckResult e) -> Check' e a Source #

test :: Functor m => (a -> m Bool) -> (a -> e) -> Check e m a infix 7 Source #

(?~>) :: Functor m => (a -> m Bool) -> (a -> e) -> Check e m a infix 7 Source #

test' :: Applicative m => (a -> Bool) -> (a -> e) -> Check e m a infix 7 Source #

(?>) :: Applicative m => (a -> Bool) -> (a -> e) -> Check e m a infix 7 Source #

test_ :: Monad m => (a -> m Bool) -> e -> Check e m a infix 7 Source #

(?~>>) :: Monad m => (a -> m Bool) -> e -> Check e m a infix 7 Source #

test'_ :: Applicative m => (a -> Bool) -> e -> Check e m a infix 7 Source #

(?>>) :: Applicative m => (a -> Bool) -> e -> Check e m a infix 7 Source #

Helper for deriving Checkable

foldWithCheck :: (Foldable f, Applicative m) => Check e m a -> Check e m (f a) Source #

Lift a check to a foldable

traverseWithCheck :: (Traversable t, Applicative m) => Check e m a -> Check e m (t a) Source #

Lift a check to a traversable

Reexports

hoist :: forall m n (b :: k). (MFunctor t, Monad m) => (forall a. m a -> n a) -> t m b -> t n b #

Lift a monad morphism from m to n into a monad morphism from (t m) to (t n)

The first argument to hoist must be a monad morphism, even though the type system does not enforce this

contramap :: Contravariant f => (a -> b) -> f b -> f a #