validity 0.3.0.4 → 0.3.1.1
raw patch · 2 files changed
+42/−25 lines, 2 files
Files
- src/Data/Validity.hs +33/−21
- validity.cabal +9/−4
src/Data/Validity.hs view
@@ -25,7 +25,6 @@ > instance Validity Prime where > isValid (Prime n) = isPrime n -}- module Data.Validity ( Validity(..) , isInvalid@@ -33,24 +32,32 @@ , constructValidUnsafe ) where -import Data.Maybe (fromMaybe)-+import Data.Maybe (fromMaybe) -- | A class of types that have additional invariants defined upon them -- that aren't enforced by the type system class Validity a where- -- | Check whether a given value is a valid value.- isValid :: a -> Bool+ isValid :: a -> Bool -- ^ Check whether a given value is a valid value. -isInvalid :: Validity a => a -> Bool+isInvalid+ :: Validity a+ => a -> Bool isInvalid = not . isValid -- | Any tuple of things is valid if both of its elements are valid-instance (Validity a, Validity b) => Validity (a, b) where+instance (Validity a, Validity b) =>+ Validity (a, b) where isValid (a, b) = isValid a && isValid b +-- | Any Either of things is valid if the contents are valid in either of the cases.+instance (Validity a, Validity b) =>+ Validity (Either a b) where+ isValid (Left a) = isValid a+ isValid (Right b) = isValid b+ -- | Any tuple of things is valid if all three of its elements are valid-instance (Validity a, Validity b, Validity c) => Validity (a, b, c) where+instance (Validity a, Validity b, Validity c) =>+ Validity (a, b, c) where isValid (a, b, c) = isValid a && isValid b && isValid c -- | A list of things is valid if all of the things are valid.@@ -58,14 +65,16 @@ -- This means that the empty list is considered valid. -- If the empty list should not be considered valid as part of your custom data -- type, make sure to write a custom @Validity instance@-instance Validity a => Validity [a] where+instance Validity a =>+ Validity [a] where isValid = all isValid -- | A Maybe thing is valid if the thing inside is valid or it's nothing -- It makes sense to assume that 'Nothing' is valid. -- If Nothing wasn't valid, you wouldn't have used a Maybe -- in the datastructure.-instance Validity a => Validity (Maybe a) where+instance Validity a =>+ Validity (Maybe a) where isValid Nothing = True isValid (Just a) = isValid a @@ -98,29 +107,32 @@ -- * NaN is not valid. -- * Infinite values are not valid. instance Validity Float where- isValid d- = not (isNaN d)- && not (isInfinite d)+ isValid d = not (isNaN d) && not (isInfinite d) -- | NOT trivially valid: -- -- * NaN is not valid. -- * Infinite values are not valid. instance Validity Double where- isValid d- = not (isNaN d)- && not (isInfinite d)+ isValid d = not (isNaN d) && not (isInfinite d) -- | Trivially valid instance Validity Integer where isValid = const True -- | Construct a valid element from an unchecked element-constructValid :: Validity a => a -> Maybe a-constructValid p = if isValid p then Just p else Nothing+constructValid+ :: Validity a+ => a -> Maybe a+constructValid p =+ if isValid p+ then Just p+ else Nothing -- | Construct a valid element from an unchecked element, throwing 'error' -- on invalid elements.-constructValidUnsafe :: (Show a, Validity a) => a -> a-constructValidUnsafe p = fromMaybe (error $ show p ++ " is not valid") $ constructValid p-+constructValidUnsafe+ :: (Show a, Validity a)+ => a -> a+constructValidUnsafe p =+ fromMaybe (error $ show p ++ " is not valid") $ constructValid p
validity.cabal view
@@ -1,12 +1,18 @@ name: validity-version: 0.3.0.4+version: 0.3.1.1 synopsis: Validity typeclass description:+ For more info, see <https://github.com/NorfairKing/validity the readme>.+ . Note: There are companion instance packages for this library: . * <https://hackage.haskell.org/package/validity-text validity-text> .+ * <https://hackage.haskell.org/package/validity-path validity-path>+ . * <https://hackage.haskell.org/package/validity-containers validity-containers>+ .+ * <https://hackage.haskell.org/package/validity-bytestring validity-bytestring> homepage: https://github.com/NorfairKing/validity#readme license: MIT@@ -14,16 +20,15 @@ author: Tom Sydney Kerckhove maintainer: syd.kerckhove@gmail.com copyright: Copyright: (c) 2016 Tom Sydney Kerckhove-category: Web+category: Validity build-type: Simple--- extra-source-files: cabal-version: >=1.10 library hs-source-dirs: src exposed-modules: Data.Validity , Data.RelativeValidity- build-depends: base >= 4 && < 5+ build-depends: base >= 4.8 && < 5 default-language: Haskell2010 source-repository head