{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Keuringsdienst.Helpers
( filterFailedValidations,
isEqualTo,
isLesserThan,
isNegative,
isNegativeOrZero,
isNonEmptyText,
isPositive,
isPositiveOrZero,
isTextOfLength,
isTextSmallerThan,
isTextSmallerThanOrEqual,
isNotEqualTo,
)
where
import Data.Map as Map
import Data.Text as T
import Keuringsdienst as K
isEqualTo :: (Show a, Eq a) => a -> ValidationRule a
isEqualTo :: forall a. (Show a, Eq a) => a -> ValidationRule a
isEqualTo a
value = (a -> ValidationResult) -> ValidationRule a
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((a -> ValidationResult) -> ValidationRule a)
-> (a -> ValidationResult) -> ValidationRule a
forall a b. (a -> b) -> a -> b
$ \a
actual ->
if a
actual a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
value
then ValidationResult
forall err. Validation err
Success
else
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[String -> ErrMsg
pack (String -> ErrMsg) -> String -> ErrMsg
forall a b. (a -> b) -> a -> b
$ String
"Expected " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
actual String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" to equal " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
value]
isNotEqualTo :: (Show a, Eq a) => a -> ValidationRule a
isNotEqualTo :: forall a. (Show a, Eq a) => a -> ValidationRule a
isNotEqualTo a
value = (a -> ValidationResult) -> ValidationRule a
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((a -> ValidationResult) -> ValidationRule a)
-> (a -> ValidationResult) -> ValidationRule a
forall a b. (a -> b) -> a -> b
$ \a
actual ->
if a
actual a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
value
then ValidationResult
forall err. Validation err
Success
else
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[String -> ErrMsg
pack (String -> ErrMsg) -> String -> ErrMsg
forall a b. (a -> b) -> a -> b
$ String
"Expected " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
actual String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" to not equal " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
value]
isGreaterThan :: (Show a, Ord a) => a -> ValidationRule a
isGreaterThan :: forall a. (Show a, Ord a) => a -> ValidationRule a
isGreaterThan a
ruleValue = (a -> ValidationResult) -> ValidationRule a
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((a -> ValidationResult) -> ValidationRule a)
-> (a -> ValidationResult) -> ValidationRule a
forall a b. (a -> b) -> a -> b
$ \a
actual ->
if a
actual a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
ruleValue
then ValidationResult
forall err. Validation err
Success
else
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[ String -> ErrMsg
pack (a -> String
forall a. Show a => a -> String
show a
actual String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" was expected to be greater than " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
ruleValue)
]
isLesserThan :: (Show a, Ord a) => a -> ValidationRule a
isLesserThan :: forall a. (Show a, Ord a) => a -> ValidationRule a
isLesserThan a
ruleValue = (a -> ValidationResult) -> ValidationRule a
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((a -> ValidationResult) -> ValidationRule a)
-> (a -> ValidationResult) -> ValidationRule a
forall a b. (a -> b) -> a -> b
$ \a
actual ->
if a
actual a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
ruleValue
then ValidationResult
forall err. Validation err
Success
else
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[ String -> ErrMsg
pack (a -> String
forall a. Show a => a -> String
show a
actual String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" was expected to be lesser than " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
ruleValue)
]
isNonEmptyText :: ValidationRule Text
isNonEmptyText :: ValidationRule ErrMsg
isNonEmptyText = (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((ErrMsg -> ValidationResult) -> ValidationRule ErrMsg)
-> (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a b. (a -> b) -> a -> b
$ \ErrMsg
actual ->
if ErrMsg -> Bool
T.null ErrMsg
actual
then [ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure [String -> ErrMsg
T.pack String
"Text was expected to be non-empty"]
else ValidationResult
forall err. Validation err
Success
isTextOfLength :: Int -> ValidationRule Text
isTextOfLength :: Int -> ValidationRule ErrMsg
isTextOfLength Int
ruleValue = (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((ErrMsg -> ValidationResult) -> ValidationRule ErrMsg)
-> (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a b. (a -> b) -> a -> b
$ \ErrMsg
actual -> do
let actualTextLength :: Int
actualTextLength = ErrMsg -> Int
T.length ErrMsg
actual
if Int
actualTextLength Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
ruleValue
then
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[ String -> ErrMsg
T.pack
( String
"Text was expected to be of size "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
ruleValue
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" but was "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
actualTextLength
)
]
else ValidationResult
forall err. Validation err
Success
isTextSmallerThan :: Int -> ValidationRule Text
isTextSmallerThan :: Int -> ValidationRule ErrMsg
isTextSmallerThan Int
ruleValue = (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((ErrMsg -> ValidationResult) -> ValidationRule ErrMsg)
-> (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a b. (a -> b) -> a -> b
$ \ErrMsg
actual -> do
let actualTextLength :: Int
actualTextLength = ErrMsg -> Int
T.length ErrMsg
actual
if Int
actualTextLength Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
ruleValue
then
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[ String -> ErrMsg
T.pack
( String
"Text was expected to be smaller than "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
ruleValue
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" but was "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
actualTextLength
)
]
else ValidationResult
forall err. Validation err
Success
isTextSmallerThanOrEqual :: Int -> ValidationRule Text
isTextSmallerThanOrEqual :: Int -> ValidationRule ErrMsg
isTextSmallerThanOrEqual Int
ruleValue = (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a. (a -> ValidationResult) -> ValidationRule a
ValidationRule ((ErrMsg -> ValidationResult) -> ValidationRule ErrMsg)
-> (ErrMsg -> ValidationResult) -> ValidationRule ErrMsg
forall a b. (a -> b) -> a -> b
$ \ErrMsg
actual -> do
let actualTextLength :: Int
actualTextLength = ErrMsg -> Int
T.length ErrMsg
actual
if Int
actualTextLength Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ruleValue
then
[ErrMsg] -> ValidationResult
forall err. err -> Validation err
Failure
[ String -> ErrMsg
T.pack
( String
"Text was expected to be smaller than "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
ruleValue
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" but was "
String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
actualTextLength
)
]
else ValidationResult
forall err. Validation err
Success
isNegative :: ValidationRule Int
isNegative :: ValidationRule Int
isNegative = Int -> ValidationRule Int
forall a. (Show a, Ord a) => a -> ValidationRule a
isLesserThan Int
0
isPositive :: ValidationRule Int
isPositive :: ValidationRule Int
isPositive = Int -> ValidationRule Int
forall a. (Show a, Ord a) => a -> ValidationRule a
isGreaterThan Int
0
isPositiveOrZero :: ValidationRule Int
isPositiveOrZero :: ValidationRule Int
isPositiveOrZero = ValidationRule Int
isPositive ValidationRule Int -> ValidationRule Int -> ValidationRule Int
forall a. ValidationRule a -> ValidationRule a -> ValidationRule a
*||* Int -> ValidationRule Int
forall a. (Show a, Eq a) => a -> ValidationRule a
isEqualTo Int
0
isNegativeOrZero :: ValidationRule Int
isNegativeOrZero :: ValidationRule Int
isNegativeOrZero = ValidationRule Int
isNegative ValidationRule Int -> ValidationRule Int -> ValidationRule Int
forall a. ValidationRule a -> ValidationRule a -> ValidationRule a
*||* Int -> ValidationRule Int
forall a. (Show a, Eq a) => a -> ValidationRule a
isEqualTo Int
0
filterFailedValidations :: Map Text (Validation err) -> Map Text (Validation err)
filterFailedValidations :: forall err.
Map ErrMsg (Validation err) -> Map ErrMsg (Validation err)
filterFailedValidations =
(Validation err -> Bool)
-> Map ErrMsg (Validation err) -> Map ErrMsg (Validation err)
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter
( \Validation err
x -> do
case Validation err
x of
Failure err
_ -> Bool
True
Validation err
Success -> Bool
False
)