{-# LANGUAGE Safe #-}
module Valida.Combinators
(
failureIf
, failureUnless
, failureIf'
, failureUnless'
, negateV
, negateV'
, andAlso
, orElse
, failV
, 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
, withDefault
) where
import Data.Ix (Ix (inRange))
import Data.List.NonEmpty (NonEmpty)
import Valida.Utils (neSingleton)
import Valida.Validation (Validation (..), validationConst)
import Valida.Validator (Validator (Validator))
failureIf :: (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureIf :: (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureIf a -> Bool
predc = (a -> Bool) -> NonEmpty e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator e a a
validatorFrom (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
predc) (NonEmpty e -> Validator (NonEmpty e) a a)
-> (e -> NonEmpty e) -> e -> Validator (NonEmpty e) a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> NonEmpty e
forall a. a -> NonEmpty a
neSingleton
failureUnless :: (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless :: (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless a -> Bool
predc = (a -> Bool) -> NonEmpty e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator e a a
validatorFrom a -> Bool
predc (NonEmpty e -> Validator (NonEmpty e) a a)
-> (e -> NonEmpty e) -> e -> Validator (NonEmpty e) a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> NonEmpty e
forall a. a -> NonEmpty a
neSingleton
failureIf' :: (a -> Bool) -> Validator () a a
failureIf' :: (a -> Bool) -> Validator () a a
failureIf' a -> Bool
predc = (a -> Bool) -> () -> Validator () a a
forall a e. (a -> Bool) -> e -> Validator e a a
validatorFrom (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) -> Validator () a a
failureUnless' :: (a -> Bool) -> Validator () a a
failureUnless' = ((a -> Bool) -> () -> Validator () a a)
-> () -> (a -> Bool) -> Validator () a a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> Bool) -> () -> Validator () a a
forall a e. (a -> Bool) -> e -> Validator e a a
validatorFrom ()
mustBe :: Eq a => a -> e -> Validator (NonEmpty e) a a
mustBe :: a -> e -> Validator (NonEmpty e) a a
mustBe a
x = (a -> Bool) -> e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x)
{-# INLINABLE mustBe #-}
ofLength :: Foldable t => Int -> e -> Validator (NonEmpty e) (t a) (t a)
ofLength :: Int -> e -> Validator (NonEmpty e) (t a) (t a)
ofLength Int
n = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
minLengthOf :: Foldable t => Int -> e -> Validator (NonEmpty e) (t a) (t a)
minLengthOf :: Int -> e -> Validator (NonEmpty e) (t a) (t a)
minLengthOf Int
n = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
maxLengthOf :: Foldable t => Int -> e -> Validator (NonEmpty e) (t a) (t a)
maxLengthOf :: Int -> e -> Validator (NonEmpty e) (t a) (t a)
maxLengthOf Int
n = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
lengthAbove :: Foldable t => Int -> e -> Validator (NonEmpty e) (t a) (t a)
lengthAbove :: Int -> e -> Validator (NonEmpty e) (t a) (t a)
lengthAbove Int
n = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
lengthBelow :: Foldable t => Int -> e -> Validator (NonEmpty e) (t a) (t a)
lengthBelow :: Int -> e -> Validator (NonEmpty e) (t a) (t a)
lengthBelow Int
n = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
notEmpty :: Foldable t => e -> Validator (NonEmpty e) (t a) (t a)
notEmpty :: e -> Validator (NonEmpty e) (t a) (t a)
notEmpty = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureIf t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null
{-# INLINABLE notEmpty #-}
{-# SPECIALIZE notEmpty :: e -> Validator (NonEmpty e) [a] [a] #-}
lengthWithin :: Foldable t => (Int, Int) -> e -> Validator (NonEmpty e) (t a) (t a)
lengthWithin :: (Int, Int) -> e -> Validator (NonEmpty e) (t a) (t a)
lengthWithin (Int, Int)
r = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
minValueOf :: Ord a => a -> e -> Validator (NonEmpty e) a a
minValueOf :: a -> e -> Validator (NonEmpty e) a a
minValueOf a
x = (a -> Bool) -> e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
x)
{-# INLINABLE minValueOf #-}
maxValueOf :: Ord a => a -> e -> Validator (NonEmpty e) a a
maxValueOf :: a -> e -> Validator (NonEmpty e) a a
maxValueOf a
x = (a -> Bool) -> e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
x)
{-# INLINABLE maxValueOf #-}
valueAbove :: Ord a => a -> e -> Validator (NonEmpty e) a a
valueAbove :: a -> e -> Validator (NonEmpty e) a a
valueAbove a
n = (a -> Bool) -> e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
n)
{-# INLINABLE valueAbove #-}
valueBelow :: Ord a => a -> e -> Validator (NonEmpty e) a a
valueBelow :: a -> e -> Validator (NonEmpty e) a a
valueBelow a
n = (a -> Bool) -> e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
n)
{-# INLINABLE valueBelow #-}
valueWithin :: Ord a => (a, a) -> e -> Validator (NonEmpty e) a a
valueWithin :: (a, a) -> e -> Validator (NonEmpty e) a a
valueWithin (a
m, a
n) = (a -> Bool) -> e -> Validator (NonEmpty e) a a
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((a -> Bool) -> e -> Validator (NonEmpty e) a a)
-> (a -> Bool) -> e -> Validator (NonEmpty e) a 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 -> Validator (NonEmpty e) Int Int #-}
onlyContains :: Foldable t => (a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
onlyContains :: (a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
onlyContains a -> Bool
x = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
atleastContains :: Foldable t => (a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
atleastContains :: (a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
atleastContains a -> Bool
x = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
mustContain :: (Foldable t, Eq a) => a -> e -> Validator (NonEmpty e) (t a) (t a)
mustContain :: a -> e -> Validator (NonEmpty e) (t a) (t a)
mustContain a
x = (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a)
forall a e. (a -> Bool) -> e -> Validator (NonEmpty e) a a
failureUnless ((t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (t a))
-> (t a -> Bool) -> e -> Validator (NonEmpty e) (t a) (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 -> Validator (NonEmpty e) [a] [a] #-}
mustBe' :: Eq a => a -> Validator () a a
mustBe' :: a -> Validator () a a
mustBe' a
x = (a -> Bool) -> Validator () a a
forall a. (a -> Bool) -> Validator () a a
failureUnless' (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
x)
{-# INLINABLE mustBe' #-}
ofLength' :: Foldable t => Int -> Validator () (t a) (t a)
ofLength' :: Int -> Validator () (t a) (t a)
ofLength' Int
n = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
minLengthOf' :: Foldable t => Int -> Validator () (t a) (t a)
minLengthOf' :: Int -> Validator () (t a) (t a)
minLengthOf' Int
n = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
maxLengthOf' :: Foldable t => Int -> Validator () (t a) (t a)
maxLengthOf' :: Int -> Validator () (t a) (t a)
maxLengthOf' Int
n = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
lengthAbove' :: Foldable t => Int -> Validator () (t a) (t a)
lengthAbove' :: Int -> Validator () (t a) (t a)
lengthAbove' Int
n = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
lengthBelow' :: Foldable t => Int -> Validator () (t a) (t a)
lengthBelow' :: Int -> Validator () (t a) (t a)
lengthBelow' Int
n = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
notEmpty' :: Foldable t => Validator () (t a) (t a)
notEmpty' :: Validator () (t a) (t a)
notEmpty' = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureIf' t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null
{-# INLINABLE notEmpty' #-}
{-# SPECIALIZE notEmpty' :: Validator () [a] [a] #-}
lengthWithin' :: Foldable t => (Int, Int) -> Validator () (t a) (t a)
lengthWithin' :: (Int, Int) -> Validator () (t a) (t a)
lengthWithin' (Int, Int)
r = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
minValueOf' :: Ord a => a -> Validator () a a
minValueOf' :: a -> Validator () a a
minValueOf' a
x = (a -> Bool) -> Validator () a a
forall a. (a -> Bool) -> Validator () a a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
x)
{-# INLINABLE minValueOf' #-}
maxValueOf' :: Ord a => a -> Validator () a a
maxValueOf' :: a -> Validator () a a
maxValueOf' a
x = (a -> Bool) -> Validator () a a
forall a. (a -> Bool) -> Validator () a a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<=a
x)
{-# INLINABLE maxValueOf' #-}
valueAbove' :: Ord a => a -> Validator () a a
valueAbove' :: a -> Validator () a a
valueAbove' a
n = (a -> Bool) -> Validator () a a
forall a. (a -> Bool) -> Validator () a a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
n)
{-# INLINABLE valueAbove' #-}
valueBelow' :: Ord a => a -> Validator () a a
valueBelow' :: a -> Validator () a a
valueBelow' a
n = (a -> Bool) -> Validator () a a
forall a. (a -> Bool) -> Validator () a a
failureUnless' (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
n)
{-# INLINABLE valueBelow' #-}
valueWithin' :: Ord a => (a, a) -> Validator () a a
valueWithin' :: (a, a) -> Validator () a a
valueWithin' (a
m, a
n) = (a -> Bool) -> Validator () a a
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((a -> Bool) -> Validator () a a)
-> (a -> Bool) -> Validator () a 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) -> Validator () Int Int #-}
onlyContains' :: Foldable t => (a -> Bool) -> Validator () (t a) (t a)
onlyContains' :: (a -> Bool) -> Validator () (t a) (t a)
onlyContains' a -> Bool
x = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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) -> Validator () [a] [a] #-}
atleastContains' :: Foldable t => (a -> Bool) -> Validator () (t a) (t a)
atleastContains' :: (a -> Bool) -> Validator () (t a) (t a)
atleastContains' a -> Bool
x = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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) -> Validator () [a] [a] #-}
mustContain' :: (Foldable t, Eq a) => a -> Validator () (t a) (t a)
mustContain' :: a -> Validator () (t a) (t a)
mustContain' a
x = (t a -> Bool) -> Validator () (t a) (t a)
forall a. (a -> Bool) -> Validator () a a
failureUnless' ((t a -> Bool) -> Validator () (t a) (t a))
-> (t a -> Bool) -> Validator () (t a) (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 -> Validator () [a] [a] #-}
negateV :: e -> Validator e1 a x -> Validator e a a
negateV :: e -> Validator e1 a x -> Validator e a a
negateV e
err (Validator a -> Validation e1 x
v) = (a -> Validation e a) -> Validator e a a
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((a -> Validation e a) -> Validator e a a)
-> (a -> Validation e a) -> Validator e a a
forall a b. (a -> b) -> a -> b
$ \a
x -> Validation e a
-> Validation e a -> Validation e1 x -> Validation e a
forall p e a. p -> p -> Validation e a -> p
validationConst (a -> Validation e a
forall e a. a -> Validation e a
Success a
x) (e -> Validation e a
forall e a. e -> Validation e a
Failure e
err) (Validation e1 x -> Validation e a)
-> Validation e1 x -> Validation e a
forall a b. (a -> b) -> a -> b
$ a -> Validation e1 x
v a
x
negateV' :: Validator e a x -> Validator () a a
negateV' :: Validator e a x -> Validator () a a
negateV' (Validator a -> Validation e x
v) = (a -> Validation () a) -> Validator () a a
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((a -> Validation () a) -> Validator () a a)
-> (a -> Validation () a) -> Validator () a a
forall a b. (a -> b) -> a -> b
$ \a
x -> Validation () a
-> Validation () a -> Validation e x -> Validation () a
forall p e a. p -> p -> Validation e a -> p
validationConst (a -> Validation () a
forall e a. a -> Validation e a
Success a
x) (() -> Validation () a
forall e a. e -> Validation e a
Failure ()) (Validation e x -> Validation () a)
-> Validation e x -> Validation () a
forall a b. (a -> b) -> a -> b
$ a -> Validation e x
v a
x
infixr 5 </>
(</>) :: Semigroup e => Validator e inp a -> Validator e inp a -> Validator e inp a
Validator inp -> Validation e a
v1 </> :: Validator e inp a -> Validator e inp a -> Validator e inp a
</> Validator inp -> Validation e a
v2 = (inp -> Validation e a) -> Validator e inp a
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((inp -> Validation e a) -> Validator e inp a)
-> (inp -> Validation e a) -> Validator e inp a
forall a b. (a -> b) -> a -> b
$ inp -> Validation e a
v1 (inp -> Validation e a)
-> (inp -> Validation e a) -> inp -> Validation e a
forall a. Semigroup a => a -> a -> a
<> inp -> Validation e a
v2
{-# INLINABLE (</>) #-}
{-# SPECIALIZE (</>)
:: Validator (NonEmpty err) inp a
-> Validator (NonEmpty err) inp a
-> Validator (NonEmpty err) inp a #-}
{-# SPECIALIZE (</>) :: Validator () inp a -> Validator () inp a -> Validator () inp a #-}
{-# SPECIALIZE (</>) :: Validator [err] inp a -> Validator [err] inp a -> Validator [err] inp a #-}
orElse :: Semigroup e => Validator e inp a -> Validator e inp a -> Validator e inp a
orElse :: Validator e inp a -> Validator e inp a -> Validator e inp a
orElse = Validator e inp a -> Validator e inp a -> Validator e inp a
forall e inp a.
Semigroup e =>
Validator e inp a -> Validator e inp a -> Validator e inp a
(</>)
{-# INLINABLE orElse #-}
failV :: Monoid e => Validator e inp a
failV :: Validator e inp a
failV = (inp -> Validation e a) -> Validator e inp a
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((inp -> Validation e a) -> Validator e inp a)
-> (inp -> Validation e a) -> Validator e inp a
forall a b. (a -> b) -> a -> b
$ Validation e a -> inp -> Validation e a
forall a b. a -> b -> a
const (Validation e a -> inp -> Validation e a)
-> Validation e a -> inp -> Validation e a
forall a b. (a -> b) -> a -> b
$ e -> Validation e a
forall e a. e -> Validation e a
Failure e
forall a. Monoid a => a
mempty
{-# INLINABLE failV #-}
andAlso :: Validator e inp a -> Validator e inp a -> Validator e inp a
andAlso :: Validator e inp a -> Validator e inp a -> Validator e inp a
andAlso = Validator e inp a -> Validator e inp a -> Validator e inp a
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINABLE andAlso #-}
satisfyAny :: (Foldable t, Semigroup e) => t (Validator e inp a) -> Validator e inp a
satisfyAny :: t (Validator e inp a) -> Validator e inp a
satisfyAny = (Validator e inp a -> Validator e inp a -> Validator e inp a)
-> t (Validator e inp a) -> Validator e inp a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Validator e inp a -> Validator e inp a -> Validator e inp a
forall e inp a.
Semigroup e =>
Validator e inp a -> Validator e inp a -> Validator e inp a
orElse
{-# INLINABLE satisfyAny #-}
{-# SPECIALIZE satisfyAny :: [Validator (NonEmpty err) inp a] -> Validator (NonEmpty err) inp a #-}
{-# SPECIALIZE satisfyAny :: [Validator () inp a] -> Validator () inp a #-}
{-# SPECIALIZE satisfyAny :: [Validator [err] inp a] -> Validator [err] inp a #-}
satisfyAll :: Foldable t => t (Validator e inp a) -> Validator e inp a
satisfyAll :: t (Validator e inp a) -> Validator e inp a
satisfyAll = (Validator e inp a -> Validator e inp a -> Validator e inp a)
-> t (Validator e inp a) -> Validator e inp a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Validator e inp a -> Validator e inp a -> Validator e inp a
forall e inp a.
Validator e inp a -> Validator e inp a -> Validator e inp a
andAlso
{-# INLINABLE satisfyAll #-}
{-# SPECIALIZE satisfyAll :: [Validator e inp a] -> Validator e inp a #-}
optionally :: Validator e inp a -> Validator e (Maybe inp) (Maybe a)
optionally :: Validator e inp a -> Validator e (Maybe inp) (Maybe a)
optionally (Validator inp -> Validation e a
v) = (Maybe inp -> Validation e (Maybe a))
-> Validator e (Maybe inp) (Maybe a)
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((Maybe inp -> Validation e (Maybe a))
-> Validator e (Maybe inp) (Maybe a))
-> (Maybe inp -> Validation e (Maybe a))
-> Validator e (Maybe inp) (Maybe a)
forall a b. (a -> b) -> a -> b
$ Validation e (Maybe a)
-> (inp -> Validation e (Maybe a))
-> Maybe inp
-> Validation e (Maybe a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe a -> Validation e (Maybe a)
forall e a. a -> Validation e a
Success Maybe a
forall a. Maybe a
Nothing) ((a -> Maybe a) -> Validation e a -> Validation e (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Validation e a -> Validation e (Maybe a))
-> (inp -> Validation e a) -> inp -> Validation e (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. inp -> Validation e a
v)
withDefault :: Validator e inp a -> a -> Validator e (Maybe inp) a
withDefault :: Validator e inp a -> a -> Validator e (Maybe inp) a
withDefault (Validator inp -> Validation e a
v) a
deflt = (Maybe inp -> Validation e a) -> Validator e (Maybe inp) a
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((Maybe inp -> Validation e a) -> Validator e (Maybe inp) a)
-> (Maybe inp -> Validation e a) -> Validator e (Maybe inp) a
forall a b. (a -> b) -> a -> b
$ Validation e a
-> (inp -> Validation e a) -> Maybe inp -> Validation e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (a -> Validation e a
forall e a. a -> Validation e a
Success a
deflt) inp -> Validation e a
v
validatorFrom :: (a -> Bool) -> e -> Validator e a a
validatorFrom :: (a -> Bool) -> e -> Validator e a a
validatorFrom a -> Bool
predc e
err = (a -> Validation e a) -> Validator e a a
forall e inp a. (inp -> Validation e a) -> Validator e inp a
Validator ((a -> Validation e a) -> Validator e a a)
-> (a -> Validation e a) -> Validator e a a
forall a b. (a -> b) -> a -> b
$ \a
x -> if a -> Bool
predc a
x then a -> Validation e a
forall e a. a -> Validation e a
Success a
x else e -> Validation e a
forall e a. e -> Validation e a
Failure e
err