keuringsdienst 0.1.0.4 → 0.1.0.5
raw patch · 3 files changed
+42/−12 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Keuringsdienst: orThisOrThat :: ValidationRule a -> ValidationRule a -> ValidationRule a
Files
- keuringsdienst.cabal +1/−1
- src/Keuringsdienst.hs +28/−11
- src/Keuringsdienst/Helpers.hs +13/−0
keuringsdienst.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack name: keuringsdienst-version: 0.1.0.4+version: 0.1.0.5 synopsis: Data validation in Haskell made easy. description: Data validation in Haskell made easy and clean, with user rules that can be combined. category: Validation, Data
src/Keuringsdienst.hs view
@@ -22,17 +22,21 @@ {-# LANGUAGE UndecidableInstances #-} module Keuringsdienst- ( (*||*),- (|??|),- (|?|),+ ( -- * Core types ValidationResult, ValidationRule (..), Validation (..),++ -- * Operators keuren, misschienKeuren, validate, maybeValidate, ofDitOfDat,+ orThisOrThat,+ (*||*),+ (|??|),+ (|?|), ) where @@ -40,13 +44,19 @@ import Data.Text as T import GHC.Generics +-- | Data type that represents the error messages. Currently is set to a simple Text,+-- futurely this could be expanded to contain more metadata. type ErrMsg = Text +-- | Core data type representing the outcome of a validation, which can be a success,+-- or a failure with more information about the failure. data Validation err = Success | Failure err deriving (Eq, Show, Generic, FromJSON, ToJSON) +-- | ValidationResult is the data type of choice for now, since we have chosen to implement+-- the library in a way that it will return a [Text] as context for validation failures. type ValidationResult = Validation [ErrMsg] instance Semigroup ValidationResult where@@ -61,6 +71,7 @@ instance Monoid ValidationResult where mempty = Success +-- | Data type representing a composable validation rule to be applied on a certain data. newtype ValidationRule a = ValidationRule { performValidation :: a -> ValidationResult }@@ -74,35 +85,37 @@ instance Monoid (ValidationRule a) where mempty = ValidationRule {performValidation = const Success} --- keuren operator+-- | This function applies a validation rule to a value. Stands for validate/judge in Dutch. keuren :: a -> ValidationRule a -> ValidationResult keuren x rule = performValidation rule x infixl 8 |?| --- keuren operator+-- | keuren operator (|?|) :: a -> ValidationRule a -> ValidationResult (|?|) = keuren --- keuren operator+-- | Alias to the keuren operator validate :: a -> ValidationRule a -> ValidationResult validate = keuren --- misschienKeuren operator+-- | This function applies a validation rule to a value when it is a Just and+-- defaults to a Success in case of a Nothing. Stands for maybe validate/judge in Dutch. misschienKeuren :: Maybe a -> ValidationRule a -> ValidationResult misschienKeuren x rule = maybe Success (performValidation rule) x infixl 8 |??| --- misschienKeuren operator+-- | misschienKeuren operator (|??|) :: Maybe a -> ValidationRule a -> ValidationResult (|??|) = misschienKeuren --- misschienKeuren operator+-- | Alias to the misschienKeuren operator maybeValidate :: Maybe a -> ValidationRule a -> ValidationResult maybeValidate = misschienKeuren --- if one of the validations has a successful result, then the validation is a success+-- | If one of the validations has a successful result, then the validation is a success.+-- Stands for or this or that in Dutch. ofDitOfDat :: ValidationRule a -> ValidationRule a -> ValidationRule a ofDitOfDat rule1 rule2 = ValidationRule $ \actual -> case (performValidation rule1 actual, performValidation rule2 actual) of@@ -112,6 +125,10 @@ infixl 6 *||* --- ofDitOfDat operator+-- | ofDitOfDat operator (*||*) :: ValidationRule a -> ValidationRule a -> ValidationRule a (*||*) = ofDitOfDat++-- | Alias to the ofDitOfDat operator+orThisOrThat :: ValidationRule a -> ValidationRule a -> ValidationRule a+orThisOrThat = ofDitOfDat
src/Keuringsdienst/Helpers.hs view
@@ -40,6 +40,7 @@ import Data.Text as T import Keuringsdienst as K +-- | Validate that a value is equal to another. isEqualTo :: (Show a, Eq a) => a -> ValidationRule a isEqualTo value = ValidationRule $ \actual -> if actual == value@@ -48,6 +49,7 @@ Failure [pack $ "Expected " <> show actual <> " to equal " <> show value] +-- | Validate that a value is different to another. isNotEqualTo :: (Show a, Eq a) => a -> ValidationRule a isNotEqualTo value = ValidationRule $ \actual -> if actual /= value@@ -56,6 +58,7 @@ 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 a isGreaterThan ruleValue = ValidationRule $ \actual -> if actual > ruleValue@@ -65,6 +68,7 @@ [ 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 a isLesserThan ruleValue = ValidationRule $ \actual -> if actual < ruleValue@@ -74,12 +78,14 @@ [ pack (show actual <> " was expected to be lesser than " <> show ruleValue) ] +-- | Validate that a value is a non empty @Text@. isNonEmptyText :: ValidationRule Text isNonEmptyText = 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 Text isTextOfLength ruleValue = ValidationRule $ \actual -> do let actualTextLength = T.length actual@@ -95,6 +101,7 @@ ] else Success +-- | Validate that a value is a @Text@ of length smaller than n. isTextSmallerThan :: Int -> ValidationRule Text isTextSmallerThan ruleValue = ValidationRule $ \actual -> do let actualTextLength = T.length actual@@ -110,6 +117,7 @@ ] else Success +-- | Validate that a value is a @Text@ of length smaller or equal to n. isTextSmallerThanOrEqual :: Int -> ValidationRule Text isTextSmallerThanOrEqual ruleValue = ValidationRule $ \actual -> do let actualTextLength = T.length actual@@ -125,18 +133,23 @@ ] else Success +-- | Validate that a value is lesser than 0. isNegative :: ValidationRule Int isNegative = isLesserThan 0 +-- | Validate that a value is greater than 0. isPositive :: ValidationRule Int isPositive = isGreaterThan 0 +-- | Validate that a value is positive or zero. isPositiveOrZero :: ValidationRule Int isPositiveOrZero = isPositive *||* isEqualTo 0 +-- | Validate that a value is negative or zero. isNegativeOrZero :: ValidationRule Int isNegativeOrZero = 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