packages feed

validity 0.6.0.0 → 0.7.0.0

raw patch · 3 files changed

+41/−25 lines, 3 files

Files

src/Data/Validity.hs view
@@ -4,6 +4,9 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeOperators #-}+#if MIN_VERSION_base(4,9,0)+{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}+#endif  {-| @@ -37,9 +40,9 @@     -} module Data.Validity     ( Validity(..)-    , isValid     -- * Helper functions to define 'validate'     , trivialValidation+    , genericValidate     , check     , declare     , annotate@@ -49,6 +52,7 @@     , valid     -- * Utilities     -- ** Utilities for validity checking+    , isValid     , isInvalid     , constructValid     , constructValidUnsafe@@ -70,22 +74,26 @@ #if MIN_VERSION_base(4,9,0) import Data.List.NonEmpty (NonEmpty((:|))) #endif-import Data.Maybe (Maybe, fromMaybe)+import Data.Maybe (fromMaybe) #if MIN_VERSION_base(4,8,0) #else import Data.Monoid import Data.Ratio #endif-import Data.Int (Int, Int16, Int32, Int64, Int8)+import Data.Int (Int16, Int32, Int64, Int8)+#if MIN_VERSION_base(4,8,0)+import Data.Word (Word16, Word32, Word64, Word8)+#else import Data.Word (Word, Word16, Word32, Word64, Word8)+#endif import GHC.Generics #if MIN_VERSION_base(4,8,0)-import GHC.Natural (Natural, isValidNatural)+import GHC.Natural #endif import GHC.Real (Ratio(..))  -- | A class of types that have additional invariants defined upon them--- that aren't enforced by the type system+ -- -- === Purpose --@@ -112,7 +120,7 @@ -- -- === Semantics ----- 'isValid' should be an underapproximation of actual validity.+-- 'validate' should be an underapproximation of actual validity. -- -- This means that if 'isValid' is not a perfect representation of actual -- validity, for safety reasons, it should never return 'True' for invalid@@ -120,18 +128,18 @@ -- -- For example: ----- > isValid = const False+-- > validate = const $ invalid "always" ----- is a valid implementation for any type, because it never returns 'True'--- for invalid values.+-- is a valid implementation for any type, because now 'isValid' never returns+-- 'True' for invalid values. ----- > isValid (Even i) = i == 2+-- > validate (Even i) = declare "The integer is equal to two" $ i == 2 -- -- is a valid implementation for @newtype Even = Even Int@, but ----- > isValid (Even i) = even i || i == 1+-- > validate (Even i) = declare "The integer is even or equal to one" $ even i || i == 1 ----- is not because it returns 'True' for an invalid value: '1'.+-- is not because then `isValid` returns 'True' for an invalid value: '1'. -- -- === Automatic instances with 'Generic' --@@ -154,16 +162,16 @@ -- -- > instance Validity MyType where -- >     validate (MyType d s)--- >         = d <?!> "myDouble"--- >        <> s <?!> "myString"+-- >         = annotate d "myDouble"+-- >        <> annotate s "myString" class Validity a where     validate :: a -> Validation     default validate :: (Generic a, GValidity (Rep a)) =>         a -> Validation-    validate = gValidate . from+    validate = genericValidate -isValid :: Validity a => a -> Bool-isValid = isRight . checkValidity+genericValidate :: (Generic a, GValidity (Rep a)) => a -> Validation+genericValidate = gValidate . from  data ValidationChain     = Violated String@@ -426,6 +434,7 @@         mconcat             [ declare "The Float is not Nan." $ not (isNaN f)             , declare "The Float is not infinite." $ not (isInfinite f)+            , declare "The Float is not zegative zero." $ not (isNegativeZero f)             ]  -- | NOT trivially valid:@@ -437,6 +446,7 @@         mconcat             [ declare "The Double is not NaN." $ not (isNaN d)             , declare "The Double is not infinite." $ not (isInfinite d)+            , declare "The Double is not zegative zero." $ not (isNegativeZero d)             ]  -- | Trivially valid@@ -454,7 +464,7 @@ -- -- Only available with @base >= 4.8@. instance Validity Natural where-    validate n = check (isValidNatural n) "The Natural is valid."+    validate = declare "The Natural is valid." . isValidNatural #endif -- | Valid if the contained numbers are valid and the denominator is -- strictly positive.@@ -503,7 +513,11 @@ instance (Validity a) => GValidity (K1 R a) where     gValidate (K1 x) = validate x --- | Check whether 'isInvalid' is not valid.+-- | Check whether a value is valid.+isValid :: Validity a => a -> Bool+isValid = isRight . checkValidity++-- | Check whether a value is not valid. -- -- > isInvalid = not . isValid isInvalid :: Validity a => a -> Bool
test/Data/ValiditySpec.hs view
@@ -1,13 +1,14 @@ {-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE CPP #-}  module Data.ValiditySpec     ( spec     ) where  import GHC.Generics (Generic)-+#if !MIN_VERSION_base(4,11,0) import Data.Monoid-+#endif import Data.Validity  import Test.Hspec
validity.cabal view
@@ -1,11 +1,11 @@--- This file has been generated from package.yaml by hpack version 0.20.0.+-- This file has been generated from package.yaml by hpack version 0.28.2. -- -- see: https://github.com/sol/hpack ----- hash: 0352fc096299ba22bf145acdce658a27d3bf5898a040f8b0829d6aaa82332fd2+-- hash: bf53a19ba2c00de66db95f83937a6950f4b805b648cadb0e7f4a224b954970f6  name:           validity-version:        0.6.0.0+version:        0.7.0.0 synopsis:       Validity typeclass description:    For more info, see <https://github.com/NorfairKing/validity the readme>.                 .@@ -34,7 +34,8 @@ homepage:       https://github.com/NorfairKing/validity#readme bug-reports:    https://github.com/NorfairKing/validity/issues author:         Tom Sydney Kerckhove-maintainer:     syd.kerckhove@gmail.com+maintainer:     syd.kerckhove@gmail.com,+                nick.van.den.broeck666@gmail.com copyright:      Copyright: (c) 2016-2018 Tom Sydney Kerckhove license:        MIT license-file:   LICENSE