packages feed

keuringsdienst-1.0.0.11 / src / Keuringsdienst / Helpers.hs

{-  Copyright © 2023 Josep Bigorra  This file is part of Keuringsdienst.  Keuringsdienst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,    either version 3 of the License, or (at your option) any later version.  Keuringsdienst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  You should have received a copy of the GNU General Public License along with Keuringsdienst.  If not, see <https://www.gnu.org/licenses/>.-}{-# 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,  )whereimport Data.Map as Mapimport Data.Text as Timport Keuringsdienst as K-- | Validate that a value is equal to another.isEqualTo :: (Show a, Eq a) => a -> ValidationRule aisEqualTo value = ValidationRule $ \actual ->  if actual == value    then Success    else      Failure        [pack $ "Expected " <> show actual <> " to equal " <> show value]-- | Validate that a value is different to another.isNotEqualTo :: (Show a, Eq a) => a -> ValidationRule aisNotEqualTo value = ValidationRule $ \actual ->  if actual /= value    then Success    else      Failure        [pack $ "Expected " <> show actual <> " to not equal " <> show value]-- | Validate that a value is greater than another.isGreaterThan :: (Show a, Ord a) => a -> ValidationRule aisGreaterThan ruleValue = ValidationRule $ \actual ->  if actual > ruleValue    then Success    else      Failure        [ pack (show actual <> " was expected to be greater than " <> show ruleValue)        ]-- | Validate that a value is lesser than another.isLesserThan :: (Show a, Ord a) => a -> ValidationRule aisLesserThan ruleValue = ValidationRule $ \actual ->  if actual < ruleValue    then Success    else      Failure        [ pack (show actual <> " was expected to be lesser than " <> show ruleValue)        ]-- | Validate that a value is a non empty @Text@.isNonEmptyText :: ValidationRule TextisNonEmptyText = ValidationRule $ \actual ->  if T.null actual    then Failure [T.pack "Text was expected to be non-empty"]    else Success-- | Validate that a value is a @Text@ of certain length.isTextOfLength :: Int -> ValidationRule TextisTextOfLength ruleValue = ValidationRule $ \actual -> do  let actualTextLength = T.length actual  if actualTextLength /= ruleValue    then      Failure        [ T.pack            ( "Text was expected to be of size "                <> show ruleValue                <> " but was "                <> show actualTextLength            )        ]    else Success-- | Validate that a value is a @Text@ of length smaller than n.isTextSmallerThan :: Int -> ValidationRule TextisTextSmallerThan ruleValue = ValidationRule $ \actual -> do  let actualTextLength = T.length actual  if actualTextLength >= ruleValue    then      Failure        [ T.pack            ( "Text was expected to be smaller than "                <> show ruleValue                <> " but was "                <> show actualTextLength            )        ]    else Success-- | Validate that a value is a @Text@ of length smaller or equal to n.isTextSmallerThanOrEqual :: Int -> ValidationRule TextisTextSmallerThanOrEqual ruleValue = ValidationRule $ \actual -> do  let actualTextLength = T.length actual  if actualTextLength > ruleValue    then      Failure        [ T.pack            ( "Text was expected to be smaller than "                <> show ruleValue                <> " but was "                <> show actualTextLength            )        ]    else Success-- | Validate that a value is lesser than 0.isNegative :: ValidationRule IntisNegative = isLesserThan 0-- | Validate that a value is greater than 0.isPositive :: ValidationRule IntisPositive = isGreaterThan 0-- | Validate that a value is positive or zero.isPositiveOrZero :: ValidationRule IntisPositiveOrZero = isPositive *||* isEqualTo 0-- | Validate that a value is negative or zero.isNegativeOrZero :: ValidationRule IntisNegativeOrZero = isNegative *||* isEqualTo 0-- | Filter a Map of @Validation err@ and keep only the failures.filterFailedValidations :: Map Text (Validation err) -> Map Text (Validation err)filterFailedValidations =  Map.filter    ( \x -> do        case x of          Failure _ -> True          Success -> False    )