{-# LANGUAGE Safe #-}
module Valida.Combinators
(
failureIf
, failureUnless
, failureIf'
, failureUnless'
, negateRule
, negateRule'
, andAlso
, falseRule
, orElse
, satisfyAll
, satisfyAny
, (</>)
, atleastContains
, lengthAbove
, lengthBelow
, lengthWithin
, maxLengthOf
, maxValueOf
, minLengthOf
, minValueOf
, mustBe
, mustContain
, notEmpty
, ofLength
, onlyContains
, valueAbove
, valueBelow
, valueWithin
, atleastContains'
, lengthAbove'
, lengthBelow'
, lengthWithin'
, maxLengthOf'
, maxValueOf'
, minLengthOf'
, minValueOf'
, mustBe'
, mustContain'
, notEmpty'
, ofLength'
, onlyContains'
, valueAbove'
, valueBelow'
, valueWithin'
, optionally
) where
import Control.Applicative (Applicative (liftA2))
import Data.Bool (bool)
import Data.Foldable (Foldable (fold))
import Data.Ix (Ix (inRange))
import Data.List.NonEmpty (NonEmpty)
import Valida.Utils (neSingleton)
import Valida.Validation (Validation (..), validationConst)
import Valida.ValidationRule (ValidationRule (..), vrule)
failureIf :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureIf :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureIf a -> Bool
predc = (a -> Bool) -> NonEmpty e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule e a
predToRule (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
predc) (NonEmpty e -> ValidationRule (NonEmpty e) a)
-> (e -> NonEmpty e) -> e -> ValidationRule (NonEmpty e) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> NonEmpty e
forall a. a -> NonEmpty a
neSingleton
failureUnless :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless a -> Bool
predc = (a -> Bool) -> NonEmpty e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule e a
predToRule a -> Bool
predc (NonEmpty e -> ValidationRule (NonEmpty e) a)
-> (e -> NonEmpty e) -> e -> ValidationRule (NonEmpty e) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> NonEmpty e
forall a. a -> NonEmpty a
neSingleton
failureIf' :: (a -> Bool) -> ValidationRule () a
failureIf' :: (a -> Bool) -> ValidationRule () a
failureIf' a -> Bool
predc = (a -> Bool) -> () -> ValidationRule () a
forall a e. (a -> Bool) -> e -> ValidationRule e a
predToRule (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
predc) ()
failureUnless' :: (a -> Bool) -> ValidationRule () a
failureUnless' :: (a -> Bool) -> ValidationRule () a
failureUnless' = ((a -> Bool) -> () -> ValidationRule () a)
-> () -> (a -> Bool) -> ValidationRule () a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> Bool) -> () -> ValidationRule () a
forall a e. (a -> Bool) -> e -> ValidationRule e a
predToRule ()
mustBe :: Eq a => a -> e -> ValidationRule (NonEmpty e) a
mustBe :: a -> e -> ValidationRule (NonEmpty e) a
mustBe a
x = (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x)
{-# INLINABLE mustBe #-}
ofLength :: Foldable t => Int -> e -> ValidationRule (NonEmpty e) (t a)
ofLength :: Int -> e -> ValidationRule (NonEmpty e) (t a)
ofLength Int
n = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE ofLength #-}
{-# SPECIALIZE ofLength :: Int -> e -> ValidationRule (NonEmpty e) [a] #-}
minLengthOf :: Foldable t => Int -> e -> ValidationRule (NonEmpty e) (t a)
minLengthOf :: Int -> e -> ValidationRule (NonEmpty e) (t a)
minLengthOf Int
n = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE minLengthOf #-}
{-# SPECIALIZE minLengthOf :: Int -> e -> ValidationRule (NonEmpty e) [a] #-}
maxLengthOf :: Foldable t => Int -> e -> ValidationRule (NonEmpty e) (t a)
maxLengthOf :: Int -> e -> ValidationRule (NonEmpty e) (t a)
maxLengthOf Int
n = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<=Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE maxLengthOf #-}
{-# SPECIALIZE maxLengthOf :: Int -> e -> ValidationRule (NonEmpty e) [a] #-}
lengthAbove :: Foldable t => Int -> e -> ValidationRule (NonEmpty e) (t a)
lengthAbove :: Int -> e -> ValidationRule (NonEmpty e) (t a)
lengthAbove Int
n = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE lengthAbove #-}
{-# SPECIALIZE lengthAbove :: Int -> e -> ValidationRule (NonEmpty e) [a] #-}
lengthBelow :: Foldable t => Int -> e -> ValidationRule (NonEmpty e) (t a)
lengthBelow :: Int -> e -> ValidationRule (NonEmpty e) (t a)
lengthBelow Int
n = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE lengthBelow #-}
{-# SPECIALIZE lengthBelow :: Int -> e -> ValidationRule (NonEmpty e) [a] #-}
notEmpty :: Foldable t => e -> ValidationRule (NonEmpty e) (t a)
notEmpty :: e -> ValidationRule (NonEmpty e) (t a)
notEmpty = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureIf t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null
{-# INLINABLE notEmpty #-}
{-# SPECIALIZE notEmpty :: e -> ValidationRule (NonEmpty e) [a] #-}
lengthWithin :: Foldable t => (Int, Int) -> e -> ValidationRule (NonEmpty e) (t a)
lengthWithin :: (Int, Int) -> e -> ValidationRule (NonEmpty e) (t a)
lengthWithin (Int, Int)
r = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int, Int)
r (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE lengthWithin #-}
{-# SPECIALIZE lengthWithin :: (Int, Int) -> e -> ValidationRule (NonEmpty e) [a] #-}
minValueOf :: Ord a => a -> e -> ValidationRule (NonEmpty e) a
minValueOf :: a -> e -> ValidationRule (NonEmpty e) a
minValueOf a
x = (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
x)
{-# INLINABLE minValueOf #-}
maxValueOf :: Ord a => a -> e -> ValidationRule (NonEmpty e) a
maxValueOf :: a -> e -> ValidationRule (NonEmpty e) a
maxValueOf a
x = (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
x)
{-# INLINABLE maxValueOf #-}
valueAbove :: Ord a => a -> e -> ValidationRule (NonEmpty e) a
valueAbove :: a -> e -> ValidationRule (NonEmpty e) a
valueAbove a
n = (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
n)
{-# INLINABLE valueAbove #-}
valueBelow :: Ord a => a -> e -> ValidationRule (NonEmpty e) a
valueBelow :: a -> e -> ValidationRule (NonEmpty e) a
valueBelow a
n = (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
n)
{-# INLINABLE valueBelow #-}
valueWithin :: Ord a => (a, a) -> e -> ValidationRule (NonEmpty e) a
valueWithin :: (a, a) -> e -> ValidationRule (NonEmpty e) a
valueWithin (a
m, a
n) = (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((a -> Bool) -> e -> ValidationRule (NonEmpty e) a)
-> (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
forall a b. (a -> b) -> a -> b
$ \a
x -> a
m a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
n
{-# INLINABLE valueWithin #-}
{-# SPECIALIZE valueWithin :: (Int, Int) -> e -> ValidationRule (NonEmpty e) Int #-}
onlyContains :: Foldable t => (a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
onlyContains :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
onlyContains a -> Bool
x = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> t a -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all a -> Bool
x
{-# INLINABLE onlyContains #-}
{-# SPECIALIZE onlyContains :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) [a] #-}
atleastContains :: Foldable t => (a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
atleastContains :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
atleastContains a -> Bool
x = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> t a -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any a -> Bool
x
{-# INLINABLE atleastContains #-}
{-# SPECIALIZE atleastContains :: (a -> Bool) -> e -> ValidationRule (NonEmpty e) [a] #-}
mustContain :: (Foldable t, Eq a) => a -> e -> ValidationRule (NonEmpty e) (t a)
mustContain :: a -> e -> ValidationRule (NonEmpty e) (t a)
mustContain a
x = (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a e. (a -> Bool) -> e -> ValidationRule (NonEmpty e) a
failureUnless ((t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a))
-> (t a -> Bool) -> e -> ValidationRule (NonEmpty e) (t a)
forall a b. (a -> b) -> a -> b
$ a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
x
{-# INLINABLE mustContain #-}
{-# SPECIALIZE mustContain :: Eq a => a -> e -> ValidationRule (NonEmpty e) [a] #-}
mustBe' :: Eq a => a -> ValidationRule () a
mustBe' :: a -> ValidationRule () a
mustBe' a
x = (a -> Bool) -> ValidationRule () a
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x)
{-# INLINABLE mustBe' #-}
ofLength' :: Foldable t => Int -> ValidationRule () (t a)
ofLength' :: Int -> ValidationRule () (t a)
ofLength' Int
n = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE ofLength' #-}
{-# SPECIALIZE ofLength' :: Int -> ValidationRule () [a] #-}
minLengthOf' :: Foldable t => Int -> ValidationRule () (t a)
minLengthOf' :: Int -> ValidationRule () (t a)
minLengthOf' Int
n = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE minLengthOf' #-}
{-# SPECIALIZE minLengthOf' :: Int -> ValidationRule () [a] #-}
maxLengthOf' :: Foldable t => Int -> ValidationRule () (t a)
maxLengthOf' :: Int -> ValidationRule () (t a)
maxLengthOf' Int
n = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<=Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE maxLengthOf' #-}
{-# SPECIALIZE maxLengthOf' :: Int -> ValidationRule () [a] #-}
lengthAbove' :: Foldable t => Int -> ValidationRule () (t a)
lengthAbove' :: Int -> ValidationRule () (t a)
lengthAbove' Int
n = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE lengthAbove' #-}
{-# SPECIALIZE lengthAbove' :: Int -> ValidationRule () [a] #-}
lengthBelow' :: Foldable t => Int -> ValidationRule () (t a)
lengthBelow' :: Int -> ValidationRule () (t a)
lengthBelow' Int
n = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Int
n) (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE lengthBelow' #-}
{-# SPECIALIZE lengthBelow' :: Int -> ValidationRule () [a] #-}
notEmpty' :: Foldable t => ValidationRule () (t a)
notEmpty' :: ValidationRule () (t a)
notEmpty' = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureIf' t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null
{-# INLINABLE notEmpty' #-}
{-# SPECIALIZE notEmpty' :: ValidationRule () [a] #-}
lengthWithin' :: Foldable t => (Int, Int) -> ValidationRule () (t a)
lengthWithin' :: (Int, Int) -> ValidationRule () (t a)
lengthWithin' (Int, Int)
r = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> Int -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (Int, Int)
r (Int -> Bool) -> (t a -> Int) -> t a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
{-# INLINABLE lengthWithin' #-}
{-# SPECIALIZE ofLength' :: Int -> ValidationRule () [a] #-}
minValueOf' :: Ord a => a -> ValidationRule () a
minValueOf' :: a -> ValidationRule () a
minValueOf' a
x = (a -> Bool) -> ValidationRule () a
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
x)
{-# INLINABLE minValueOf' #-}
maxValueOf' :: Ord a => a -> ValidationRule () a
maxValueOf' :: a -> ValidationRule () a
maxValueOf' a
x = (a -> Bool) -> ValidationRule () a
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
x)
{-# INLINABLE maxValueOf' #-}
valueAbove' :: Ord a => a -> ValidationRule () a
valueAbove' :: a -> ValidationRule () a
valueAbove' a
n = (a -> Bool) -> ValidationRule () a
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
n)
{-# INLINABLE valueAbove' #-}
valueBelow' :: Ord a => a -> ValidationRule () a
valueBelow' :: a -> ValidationRule () a
valueBelow' a
n = (a -> Bool) -> ValidationRule () a
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
n)
{-# INLINABLE valueBelow' #-}
valueWithin' :: Ord a => (a, a) -> ValidationRule () a
valueWithin' :: (a, a) -> ValidationRule () a
valueWithin' (a
m, a
n) = (a -> Bool) -> ValidationRule () a
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((a -> Bool) -> ValidationRule () a)
-> (a -> Bool) -> ValidationRule () a
forall a b. (a -> b) -> a -> b
$ \a
x -> a
m a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
n
{-# INLINABLE valueWithin' #-}
{-# SPECIALIZE valueWithin' :: (Int, Int) -> ValidationRule () Int #-}
onlyContains' :: Foldable t => (a -> Bool) -> ValidationRule () (t a)
onlyContains' :: (a -> Bool) -> ValidationRule () (t a)
onlyContains' a -> Bool
x = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> t a -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all a -> Bool
x
{-# INLINABLE onlyContains' #-}
{-# SPECIALIZE onlyContains' :: (a -> Bool) -> ValidationRule () [a] #-}
atleastContains' :: Foldable t => (a -> Bool) -> ValidationRule () (t a)
atleastContains' :: (a -> Bool) -> ValidationRule () (t a)
atleastContains' a -> Bool
x = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> t a -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any a -> Bool
x
{-# INLINABLE atleastContains' #-}
{-# SPECIALIZE atleastContains' :: (a -> Bool) -> ValidationRule () [a] #-}
mustContain' :: (Foldable t, Eq a) => a -> ValidationRule () (t a)
mustContain' :: a -> ValidationRule () (t a)
mustContain' a
x = (t a -> Bool) -> ValidationRule () (t a)
forall a. (a -> Bool) -> ValidationRule () a
failureUnless' ((t a -> Bool) -> ValidationRule () (t a))
-> (t a -> Bool) -> ValidationRule () (t a)
forall a b. (a -> b) -> a -> b
$ a -> t a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
x
{-# INLINABLE mustContain' #-}
{-# SPECIALIZE mustContain' :: Eq a => a -> ValidationRule () [a] #-}
negateRule :: e -> ValidationRule e1 a -> ValidationRule e a
negateRule :: e -> ValidationRule e1 a -> ValidationRule e a
negateRule e
err (ValidationRule a -> Validation e1 ()
rule) = (a -> Validation e ()) -> ValidationRule e a
forall a e. (a -> Validation e ()) -> ValidationRule e a
vrule ((a -> Validation e ()) -> ValidationRule e a)
-> (a -> Validation e ()) -> ValidationRule e a
forall a b. (a -> b) -> a -> b
$ Validation e ()
-> Validation e () -> Validation e1 () -> Validation e ()
forall p e a. p -> p -> Validation e a -> p
validationConst (() -> Validation e ()
forall e a. a -> Validation e a
Success ()) (e -> Validation e ()
forall e a. e -> Validation e a
Failure e
err) (Validation e1 () -> Validation e ())
-> (a -> Validation e1 ()) -> a -> Validation e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Validation e1 ()
rule
negateRule' :: ValidationRule e a -> ValidationRule () a
negateRule' :: ValidationRule e a -> ValidationRule () a
negateRule' (ValidationRule a -> Validation e ()
rule) = (a -> Validation () ()) -> ValidationRule () a
forall a e. (a -> Validation e ()) -> ValidationRule e a
vrule ((a -> Validation () ()) -> ValidationRule () a)
-> (a -> Validation () ()) -> ValidationRule () a
forall a b. (a -> b) -> a -> b
$ ((() -> Validation () ()) -> () -> Validation () ()
forall a b. (a -> b) -> a -> b
$ ()) ((() -> Validation () ()) -> Validation () ())
-> (a -> () -> Validation () ()) -> a -> Validation () ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (() -> Validation () ())
-> (() -> Validation () ())
-> Validation e ()
-> ()
-> Validation () ()
forall p e a. p -> p -> Validation e a -> p
validationConst () -> Validation () ()
forall e a. a -> Validation e a
Success () -> Validation () ()
forall e a. e -> Validation e a
Failure (Validation e () -> () -> Validation () ())
-> (a -> Validation e ()) -> a -> () -> Validation () ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Validation e ()
rule
infixr 5 </>
(</>) :: Semigroup e => ValidationRule e a -> ValidationRule e a -> ValidationRule e a
ValidationRule a -> Validation e ()
rule1 </> :: ValidationRule e a -> ValidationRule e a -> ValidationRule e a
</> ValidationRule a -> Validation e ()
rule2 = (a -> Validation e ()) -> ValidationRule e a
forall a e. (a -> Validation e ()) -> ValidationRule e a
vrule ((a -> Validation e ()) -> ValidationRule e a)
-> (a -> Validation e ()) -> ValidationRule e a
forall a b. (a -> b) -> a -> b
$ (Validation e () -> Validation e () -> Validation e ())
-> (a -> Validation e ())
-> (a -> Validation e ())
-> a
-> Validation e ()
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Validation e () -> Validation e () -> Validation e ()
forall a. Semigroup a => a -> a -> a
(<>) a -> Validation e ()
rule1 a -> Validation e ()
rule2
{-# INLINABLE (</>) #-}
{-# SPECIALIZE (</>)
:: ValidationRule (NonEmpty err) a
-> ValidationRule (NonEmpty err) a
-> ValidationRule (NonEmpty err) a #-}
{-# SPECIALIZE (</>) :: ValidationRule () a -> ValidationRule () a -> ValidationRule () a #-}
{-# SPECIALIZE (</>) :: ValidationRule [err] a -> ValidationRule [err] a -> ValidationRule [err] a #-}
orElse :: Semigroup e => ValidationRule e a -> ValidationRule e a -> ValidationRule e a
orElse :: ValidationRule e a -> ValidationRule e a -> ValidationRule e a
orElse = ValidationRule e a -> ValidationRule e a -> ValidationRule e a
forall e a.
Semigroup e =>
ValidationRule e a -> ValidationRule e a -> ValidationRule e a
(</>)
{-# INLINABLE orElse #-}
falseRule :: Monoid e => ValidationRule e a
falseRule :: ValidationRule e a
falseRule = (a -> Validation e ()) -> ValidationRule e a
forall a e. (a -> Validation e ()) -> ValidationRule e a
vrule ((a -> Validation e ()) -> ValidationRule e a)
-> (a -> Validation e ()) -> ValidationRule e a
forall a b. (a -> b) -> a -> b
$ Validation e () -> a -> Validation e ()
forall a b. a -> b -> a
const (Validation e () -> a -> Validation e ())
-> Validation e () -> a -> Validation e ()
forall a b. (a -> b) -> a -> b
$ e -> Validation e ()
forall e a. e -> Validation e a
Failure e
forall a. Monoid a => a
mempty
{-# INLINABLE falseRule #-}
andAlso :: ValidationRule e a -> ValidationRule e a -> ValidationRule e a
andAlso :: ValidationRule e a -> ValidationRule e a -> ValidationRule e a
andAlso = ValidationRule e a -> ValidationRule e a -> ValidationRule e a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINABLE andAlso #-}
satisfyAny :: (Foldable t, Semigroup e) => t (ValidationRule e a) -> ValidationRule e a
satisfyAny :: t (ValidationRule e a) -> ValidationRule e a
satisfyAny = (ValidationRule e a -> ValidationRule e a -> ValidationRule e a)
-> t (ValidationRule e a) -> ValidationRule e a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 ValidationRule e a -> ValidationRule e a -> ValidationRule e a
forall e a.
Semigroup e =>
ValidationRule e a -> ValidationRule e a -> ValidationRule e a
(</>)
{-# INLINABLE satisfyAny #-}
{-# SPECIALIZE satisfyAny :: [ValidationRule (NonEmpty err) a] -> ValidationRule (NonEmpty err) a #-}
{-# SPECIALIZE satisfyAny :: [ValidationRule () a] -> ValidationRule () a #-}
{-# SPECIALIZE satisfyAny :: [ValidationRule [err] a] -> ValidationRule [err] a #-}
satisfyAll :: Foldable t => t (ValidationRule e a) -> ValidationRule e a
satisfyAll :: t (ValidationRule e a) -> ValidationRule e a
satisfyAll = t (ValidationRule e a) -> ValidationRule e a
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold
{-# INLINABLE satisfyAll #-}
{-# SPECIALIZE satisfyAll :: [ValidationRule e a] -> ValidationRule e a #-}
optionally :: ValidationRule e a -> ValidationRule e (Maybe a)
optionally :: ValidationRule e a -> ValidationRule e (Maybe a)
optionally (ValidationRule a -> Validation e ()
rule) = (Maybe a -> Validation e ()) -> ValidationRule e (Maybe a)
forall a e. (a -> Validation e ()) -> ValidationRule e a
vrule ((Maybe a -> Validation e ()) -> ValidationRule e (Maybe a))
-> (Maybe a -> Validation e ()) -> ValidationRule e (Maybe a)
forall a b. (a -> b) -> a -> b
$ Validation e ()
-> (a -> Validation e ()) -> Maybe a -> Validation e ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Validation e ()
forall e a. a -> Validation e a
Success ()) a -> Validation e ()
rule
predToRule :: (a -> Bool) -> e -> ValidationRule e a
predToRule :: (a -> Bool) -> e -> ValidationRule e a
predToRule a -> Bool
predc e
err = (a -> Validation e ()) -> ValidationRule e a
forall a e. (a -> Validation e ()) -> ValidationRule e a
vrule ((a -> Validation e ()) -> ValidationRule e a)
-> (a -> Validation e ()) -> ValidationRule e a
forall a b. (a -> b) -> a -> b
$ Validation e () -> Validation e () -> Bool -> Validation e ()
forall a. a -> a -> Bool -> a
bool (e -> Validation e ()
forall e a. e -> Validation e a
Failure e
err) (() -> Validation e ()
forall e a. a -> Validation e a
Success ()) (Bool -> Validation e ()) -> (a -> Bool) -> a -> Validation e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
predc