-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Lighweight pure data validation based on Applicative and Selective functors -- -- Lighweight pure data validation based on Applicative and Selective -- functors. The library builds validation interface around the following -- data type: -- --
--   data Validation e a
--       = Failure e
--       | Success a
--   
@package validation-selective @version 0.0.0.0 -- | Lightweight pure data validation based on Applicative and -- Selective functors. -- -- Validation allows to accumulate all errors instead of -- short-circuting on the first error so you can display all possible -- errors at once. -- -- Common use-cases include: -- --
    --
  1. Validating each input of a form with multiple inputs.
  2. --
  3. Performing multiple validations of a single value.
  4. --
-- -- Validation provides modular and composable -- interface which means that you can implement validations for different -- pieces of your data independently, and then combine smaller parts into -- the validation of a bigger type. The below table illustrates main ways -- to combine two Validations: -- -- TODO: table -- -- In other words, instances of different standard typeclasses provide -- various semantics which can be useful in different use-cases: -- --
    --
  1. Semigroup: accumulate both Failure and -- Success with <>.
  2. --
  3. Monoid: Success that stores mempty.
  4. --
  5. Functor: change the type inside Success.
  6. --
  7. Bifunctor: change both Failure and -- Success.
  8. --
  9. Applicative: apply function to values inside Success -- and accumulate errors inside Failure.
  10. --
  11. Alternative: return the first Success or accumulate -- all errors inside Failure.
  12. --
  13. Selective: choose which validations to apply based on the -- value inside.
  14. --
module Validation -- | Validation is a polymorphic sum type for storing either all -- validation failures or validation success. Unlike Either, which -- returns only the first error, Validation accumulates all errors -- using the Semigroup typeclass. -- -- Usually type variables in Validation e a are used as -- follows: -- -- -- -- Some typical use-cases: -- -- data Validation e a -- | Validation failure. The e type is supposed to implement the -- Semigroup instance. Failure :: e -> Validation e a -- | Successful validation result of type a. Success :: a -> Validation e a -- | Predicate on if the given Validation is Failure. -- --
--   >>> isFailure (Failure 'e')
--   True
--   
--   >>> isFailure (Success 'a')
--   False
--   
isFailure :: Validation e a -> Bool -- | Predicate on if the given Validation is Success. -- --
--   >>> isSuccess (Success 'a')
--   True
--   
--   >>> isSuccess (Failure 'e')
--   False
--   
isSuccess :: Validation e a -> Bool -- | Transforms the value of the given Validation into x -- using provided functions that can transform Failure and -- Success value into the resulting type respectively. -- --
--   >>> let myValidation = validation (<> " world!") (show . (* 10))
--   
--   >>> myValidation (Success 100)
--   "1000"
--   
--   >>> myValidation (Failure "Hello")
--   "Hello world!"
--   
validation :: (e -> x) -> (a -> x) -> Validation e a -> x -- | Filters out all Failure values into the new list of es -- from the given list of Validations. -- -- Note that the order is preserved. -- --
--   >>> failures [Failure "Hello", Success 1, Failure "world", Success 2, Failure "!" ]
--   ["Hello","world","!"]
--   
failures :: [Validation e a] -> [e] -- | Filters out all Success values into the new list of as -- from the given list of Validations. -- -- Note that the order is preserved. -- --
--   >>> successes [Failure "Hello", Success 1, Failure "world", Success 2, Failure "!" ]
--   [1,2]
--   
successes :: [Validation e a] -> [a] -- | Redistributes the given list of Validations into two lists of -- es and es, where the first list contains all values -- of Failures and the second one — Successes -- correspondingly. -- -- Note that the order is preserved. -- --
--   >>> partitionValidations [Failure "Hello", Success 1, Failure "world", Success 2, Failure "!" ]
--   (["Hello","world","!"],[1,2])
--   
partitionValidations :: [Validation e a] -> ([e], [a]) -- | Returns the contents of a Failure-value or a default value -- otherwise. -- --
--   >>> fromFailure "default" (Failure "failure")
--   "failure"
--   
--   >>> fromFailure "default" (Success 1)
--   "default"
--   
fromFailure :: e -> Validation e a -> e -- | Returns the contents of a Success-value or a default value -- otherwise. -- --
--   >>> fromSuccess 42 (Success 1)
--   1
--   
--   >>> fromSuccess 42 (Failure "failure")
--   42
--   
fromSuccess :: a -> Validation e a -> a -- | Create a Failure of NonEmpty list with a single given -- error. -- --
--   >>> failure "I am a failure"
--   Failure ("I am a failure" :| [])
--   
failure :: e -> Validation (NonEmpty e) a -- | Returns a Failure in case of the given predicate is -- True. Returns Success () otherwise. -- --
--   >>> let shouldFail = (==) "I am a failure"
--   
--   >>> failureIf (shouldFail "I am a failure") "I told you so"
--   Failure ("I told you so" :| [])
--   
--   >>> failureIf (shouldFail "I am NOT a failure") "okay"
--   Success ()
--   
failureIf :: Bool -> e -> Validation (NonEmpty e) () -- | Returns a Failure unless the given predicate is True. -- Returns Success () in case of the predicate -- is satisfied. -- -- Similar to failureIf with the reversed predicate. -- --
--   failureUnless p ≡ failureIf (not p)
--   
-- --
--   >>> let shouldFail = (==) "I am a failure"
--   
--   >>> failureUnless (shouldFail "I am a failure") "doesn't matter"
--   Success ()
--   
--   >>> failureUnless (shouldFail "I am NOT a failure") "I told you so"
--   Failure ("I told you so" :| [])
--   
failureUnless :: Bool -> e -> Validation (NonEmpty e) () -- | Transform a Validation into an Either. -- --
--   >>> validationToEither (Success "whoop")
--   Right "whoop"
--   
-- --
--   >>> validationToEither (Failure "nahh")
--   Left "nahh"
--   
validationToEither :: Validation e a -> Either e a -- | Transform an Either into a Validation. -- --
--   >>> eitherToValidation (Right "whoop")
--   Success "whoop"
--   
-- --
--   >>> eitherToValidation (Left "nahh")
--   Failure "nahh"
--   
eitherToValidation :: Either e a -> Validation e a instance Control.DeepSeq.NFData e => Control.DeepSeq.NFData1 (Validation.Validation e) instance (Control.DeepSeq.NFData e, Control.DeepSeq.NFData a) => Control.DeepSeq.NFData (Validation.Validation e a) instance (Data.Data.Data e, Data.Data.Data a) => Data.Data.Data (Validation.Validation e a) instance GHC.Generics.Generic1 (Validation.Validation e) instance GHC.Generics.Generic (Validation.Validation e a) instance (GHC.Show.Show e, GHC.Show.Show a) => GHC.Show.Show (Validation.Validation e a) instance (GHC.Classes.Ord e, GHC.Classes.Ord a) => GHC.Classes.Ord (Validation.Validation e a) instance (GHC.Classes.Eq e, GHC.Classes.Eq a) => GHC.Classes.Eq (Validation.Validation e a) instance (Validation.NoValidationMonadError, GHC.Base.Semigroup e) => GHC.Base.Monad (Validation.Validation e) instance GHC.Base.Functor (Validation.Validation e) instance (GHC.Base.Semigroup e, GHC.Base.Semigroup a) => GHC.Base.Semigroup (Validation.Validation e a) instance (GHC.Base.Semigroup e, GHC.Base.Semigroup a, GHC.Base.Monoid a) => GHC.Base.Monoid (Validation.Validation e a) instance GHC.Base.Semigroup e => GHC.Base.Applicative (Validation.Validation e) instance GHC.Base.Semigroup e => Control.Selective.Selective (Validation.Validation e) instance (GHC.Base.Semigroup e, GHC.Base.Monoid e) => GHC.Base.Alternative (Validation.Validation e) instance Data.Foldable.Foldable (Validation.Validation e) instance Data.Traversable.Traversable (Validation.Validation e) instance Data.Bifunctor.Bifunctor Validation.Validation instance Data.Bifoldable.Bifoldable Validation.Validation instance Data.Bitraversable.Bitraversable Validation.Validation instance Control.DeepSeq.NFData2 Validation.Validation