-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A library for creating type safe validations. -- -- A library for creating type safe validations using typeclasses. @package data-validation @version 0.1.2.0 module Data.Validation.Internal -- | A type that holds aggregated validation failures. data VCtx f a -- | A value that is assumed to be valid. ValidCtx :: a -> VCtx f a -- | A value that has failures but can continue to be validated. DisputedCtx :: [f] -> Map [Name] [f] -> a -> VCtx f a -- | A value that has failures and cannot be validated further. RefutedCtx :: [f] -> Map [Name] [f] -> VCtx f a -- | Takes the failures from the second parameter and adds them to the -- first. aggregateFailures :: VCtx f a -> VCtx f b -> VCtx f a -- | Takes the failures from the right-hand-side, if any, and adds them to -- the left-hand-side. ( VCtx f b -> VCtx f a testMatch :: Eq a => f -> a -> a -> Maybe f instance (GHC.Classes.Eq a, GHC.Classes.Eq f) => GHC.Classes.Eq (Data.Validation.Internal.VCtx f a) instance (GHC.Show.Show a, GHC.Show.Show f) => GHC.Show.Show (Data.Validation.Internal.VCtx f a) instance GHC.Base.Semigroup a => GHC.Base.Semigroup (Data.Validation.Internal.VCtx f a) instance GHC.Base.Monoid a => GHC.Base.Monoid (Data.Validation.Internal.VCtx f a) instance GHC.Base.Functor (Data.Validation.Internal.VCtx f) instance GHC.Base.Applicative (Data.Validation.Internal.VCtx f) instance GHC.Base.Monad (Data.Validation.Internal.VCtx f) module Data.Validation -- | A type that holds either validation failures or a validated value. data Proof f a -- | A validated value. Valid :: a -> Proof f a -- | Global and field validation failures. Invalid :: [f] -> Map [Name] [f] -> Proof f a -- | Converts a VCtx to a Proof. -- -- Internally, this library uses the VCtx type to track validation -- failures. This is because a validation failure can be partial. For -- instance, checking that a password has a special character can happen -- even if the check for a numeric character has already failed. This -- allows validation to discover as many failures as possible. -- -- However, once validation is complete, the result becomes binary. The -- validation has either succeeded or failed. In order to convert from a -- VCtx to a Proof, use the fromVCtx function. fromVCtx :: VCtx f a -> Proof f a -- | A type for storing a value to validate and, optionally, its field -- name. data ValueCtx a -- | The Field constructor represents a value that is named. Field :: Name -> a -> ValueCtx a -- | The Global constructor represents a value that is not named. Global :: a -> ValueCtx a -- | Accessor for a ValueCtx's value. getValue :: ValueCtx a -> a -- | Replaces the existing value with a new one without changing the name, -- if one exists. setValue :: ValueCtx a -> b -> ValueCtx b -- | Performs some given validation using a Field with a given name -- and value. withField :: Name -> a -> (ValueCtx a -> VCtx f (ValueCtx b)) -> VCtx f b -- | Performs some given validation using a Global with a given -- value. withValue :: a -> (ValueCtx a -> VCtx f (ValueCtx b)) -> VCtx f b -- | A type class that represents a value that can be validated. -- -- The parameters represent the following: -- -- class Validatable f a b | a -> f b validation :: Validatable f a b => a -> VCtx f b -- | Runs the validations for a given value and returns the proof. validate :: Validatable f a b => a -> Proof f b -- | Adds a validation failure to the result and ends validation. refute :: ValueCtx a -> f -> VCtx f b -- | Adds validation failures to the result and ends validation. refuteMany :: ValueCtx a -> [f] -> VCtx f b -- | Performs a validation using a given function and handles the result. -- If the result is `Left f`, a validation failure is added to the result -- and validation ends. If the result is `Right b`, validation continues -- with the new value. refuteWith :: (a -> Either f b) -> ValueCtx a -> VCtx f (ValueCtx b) -- | Performs a validation using a given function and handles the result. -- If the result is Invalid, the validation failures are added to -- the result and validation ends. If the result is `Valid b`, validation -- continues with the new value. refuteWithProof :: (a -> Proof f b) -> ValueCtx a -> VCtx f (ValueCtx b) -- | Adds a validation failure to the result and continues validation. dispute :: ValueCtx a -> f -> VCtx f (ValueCtx a) -- | Adds validation failures to the result and continues validation. disputeMany :: ValueCtx a -> [f] -> VCtx f (ValueCtx a) -- | Performs a validation using a given function and handles the result. -- If the result is `Just f`, a validation failure is added to the result -- and validation continues. If the result is Nothing, validation -- continues with no failure. disputeWith :: (a -> Maybe f) -> ValueCtx a -> VCtx f (ValueCtx a) -- | Similar to disputeWith except that the given failure is added -- if the given function returns False. disputeWithFact :: f -> (a -> Bool) -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a Maybe value is a Just. If not, it adds the -- given failure to the result and validation end. isRequired :: f -> ValueCtx (Maybe a) -> VCtx f (ValueCtx a) -- | Checks that a Maybe value is a Just when some condition -- is true. If the condition is met and the value is Just, it adds -- the given failure to the result and validation continues. isRequiredWhen :: f -> Bool -> ValueCtx (Maybe a) -> VCtx f (ValueCtx (Maybe a)) -- | Checks that a Maybe value is a Just when some condition -- is false. If the condition is not met and the value is Just, it -- adds the given failure to the result and validation continues. isRequiredUnless :: f -> Bool -> ValueCtx (Maybe a) -> VCtx f (ValueCtx (Maybe a)) -- | Checks that a Either value is a Left. If not, it adds -- the given failure to the result and validation end. isLeft :: f -> ValueCtx (Either a b) -> VCtx f (ValueCtx a) -- | Checks that a Either value is a Right. If not, it adds -- the given failure to the result and validation end. isRight :: f -> ValueCtx (Either a b) -> VCtx f (ValueCtx b) -- | Checks that the Foldable is empty. If not, it adds the given -- failure to the result and validation continues. isNull :: Foldable t => f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | Checks that the Foldable is not empty. If empty, it adds the -- given failure to the result and validation continues. isNotNull :: Foldable t => f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | Checks that a Foldable has a length equal to or greater than -- the given value. If not, it adds the given failure to the result and -- validation continues. minLength :: Foldable t => Int -> f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | Checks that a Foldable has a length equal to or less than the -- given value. If not, it adds the given failure to the result and -- validation continues. maxLength :: Foldable t => Int -> f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | Checks that a Foldable has a length equal to the given value. -- If not, it adds the given failure to the result and validation -- continues. isLength :: Foldable t => Int -> f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | Checks that a value is equal to another. If not, it adds the given -- failure to the result and validation continues. isEqual :: Eq a => a -> f -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a value is not equal to another. If equal, it adds the -- given failure to the result and validation continues. isNotEqual :: Eq a => a -> f -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a value is less than another. If not, it adds the given -- failure to the result and validation continues. isLessThan :: Ord a => a -> f -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a value is less than or equal to another. If not, it adds -- the given failure to the result and validation continues. isLessThanOrEqual :: Ord a => a -> f -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a value is greater than another. If not, it adds the given -- failure to the result and validation continues. isGreaterThan :: Ord a => a -> f -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a value is greater than or equal to another. If not, it -- adds the given failure to the result and validation continues. isGreaterThanOrEqual :: Ord a => a -> f -> ValueCtx a -> VCtx f (ValueCtx a) -- | Checks that a Foldable has a given element. If not, it adds the -- given failure to the result and validation continues. hasElem :: (Foldable t, Eq a) => a -> f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | Checks that a Foldable does not have a given element. If it has -- element, it adds the given failure to the result and validation -- continues. doesNotHaveElem :: (Foldable t, Eq a) => a -> f -> ValueCtx (t a) -> VCtx f (ValueCtx (t a)) -- | If any element is valid, the entire value is valid. ifAny :: (a -> Maybe f) -> ValueCtx [a] -> VCtx f (ValueCtx [a]) -- | Every element must be valid. ifAll :: (a -> Maybe f) -> ValueCtx [a] -> VCtx f (ValueCtx [a]) -- | Validate each element with a given function. ifEach :: (a -> Either f b) -> ValueCtx [a] -> VCtx f (ValueCtx [b]) -- | Validate each element with a given function. ifEachProven :: (a -> Proof f b) -> ValueCtx [a] -> VCtx f (ValueCtx [b]) -- | Checks that two fields are equal. If not, it adds the given failure to -- the result and validation continues. isMatch :: Eq a => f -> VCtx f a -> ValueCtx a -> VCtx f (ValueCtx a) -- | Validates a value that implements Validatable and includes any -- failures under the parent field. -- -- Consider the following example: -- --
--   data ContactVM = ContactVM { phoneNumber :: String }
--   data Contact = ...
--   instance Validatable MyFailureType ContactVM Contact where
--     ...
--   
--   data UserCreatableVM
--     = UserCreatableVM
--     { userCreatableVMEmailAddress         :: String
--     , userCreatableVMConfirmEmailAddress  :: String
--     , userCreatableVMPassword             :: String
--     , userCreatableVMConfirmPassword      :: String
--     , userCreatableVMContact              :: ContactVM
--     }
--   data UserCreatable = ...
--   
--   instance Validatable MyFailureType UserCreatableVM UserCreatable where
--     validation u =
--       let vc = withField 'userCreatableVMContact (userCreatableVMContact u) $
--             validateField                                                      -- (1)
--           ...
--       in pure UserCreatable <*> ve <*> vp <*> vc <! vce <! vcp
--   
--   
-- -- In line (1), the validateField function uses the -- Validatable instance on ContactVM to validate the -- type. All field specific validation failures are stored in a map where -- the key is the name of the field. However, in this case, there are the -- fields in the ContactVM and the parent field in -- UserCreatableVM. These names need to be combined so that the -- consumer can see if any errors came from nested fields. Using the -- validateField function, any validation failures found in the -- ContactVM value have field names that include the parent -- field. A ContactVM with an invalid phone number might have a -- result like this: `Invalid [] [(['phoneNumber], -- [InvalidPhoneNumber])]` where `['phoneNumber]` is the key to the map. -- The validationField merges this with the -- UserCreatable result to create something like this: `Invalid -- [] [(['contact, 'phoneNumber], [InvalidPhoneNumber])]`. This allows -- the consumer to determine exactly what field caused the failure. validateField :: Validatable f a b => ValueCtx a -> VCtx f (ValueCtx b) -- | Allows for validation of an optional value. See `Validating Complex -- Types` for an example. optional :: Maybe a -> (a -> VCtx f b) -> VCtx f (Maybe b) -- | Allows for validation of an optional value. See `Validating Complex -- Types` for an example. whenJust :: (ValueCtx a -> VCtx f (ValueCtx b)) -> ValueCtx (Maybe a) -> VCtx f (ValueCtx (Maybe b)) -- | Takes the failures from the second parameter and adds them to the -- first. aggregateFailures :: VCtx f a -> VCtx f b -> VCtx f a -- | Takes the failures from the right-hand-side, if any, and adds them to -- the left-hand-side. ( VCtx f b -> VCtx f a -- | tests if a Proof is valid. isValid :: Proof f a -> Bool -- | tests if a Proof is invalid. isInvalid :: Proof f a -> Bool -- | Flatten a list of proofs into a proof of the list flattenProofs :: [Proof f a] -> Proof f [a] -- | A type that holds aggregated validation failures. data VCtx f a -- | An abstract type representing names in the syntax tree. -- -- Names can be constructed in several ways, which come with -- different name-capture guarantees (see -- Language.Haskell.TH.Syntax#namecapture for an explanation of -- name capture): -- -- -- -- Names constructed using newName and mkName may be -- used in bindings (such as let x = ... or x -> -- ...), but names constructed using lookupValueName, -- lookupTypeName, 'f, ''T may not. data Name -- | Generate a capturable name. Occurrences of such names will be resolved -- according to the Haskell scoping rules at the occurrence site. -- -- For example: -- --
--   f = [| pi + $(varE (mkName "pi")) |]
--   ...
--   g = let pi = 3 in $f
--   
-- -- In this case, g is desugared to -- --
--   g = Prelude.pi + 3
--   
-- -- Note that mkName may be used with qualified names: -- --
--   mkName "Prelude.pi"
--   
-- -- See also dyn for a useful combinator. The above example could -- be rewritten using dyn as -- --
--   f = [| pi + $(dyn "pi") |]
--   
mkName :: String -> Name -- | The name without its module prefix. -- --

Examples

-- --
--   >>> nameBase ''Data.Either.Either
--   "Either"
--   
--   >>> nameBase (mkName "foo")
--   "foo"
--   
--   >>> nameBase (mkName "Module.foo")
--   "foo"
--   
nameBase :: Name -> String instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Validation.ValueCtx a) instance GHC.Show.Show a => GHC.Show.Show (Data.Validation.ValueCtx a) instance (GHC.Classes.Eq a, GHC.Classes.Eq f) => GHC.Classes.Eq (Data.Validation.Proof f a) instance (GHC.Show.Show a, GHC.Show.Show f) => GHC.Show.Show (Data.Validation.Proof f a) instance GHC.Base.Functor Data.Validation.ValueCtx instance GHC.Base.Semigroup a => GHC.Base.Semigroup (Data.Validation.Proof f a) instance GHC.Base.Monoid a => GHC.Base.Monoid (Data.Validation.Proof f a) instance GHC.Base.Functor (Data.Validation.Proof f) instance GHC.Base.Applicative (Data.Validation.Proof f) instance GHC.Base.Monad (Data.Validation.Proof f) module Data.Validation.Transforms -- | A type that represents a validated type. data V -- | A type that represents an unvalidated type, often called a View Model. data VM -- | A type that represents a validation transformaion. The unvalidated -- type is the first parameter which is used when VM is passed in. -- The second parameter is the validated type which is used when V -- is passed in. -- --

Examples

-- -- Basic usage: -- --
--   data ThingV v 
--     = Thing 
--     { emailAddress :: VT v String EmailAddress
--     , confirmEmailAddress :: VT v String () 
--     }
--   type ThingVM = ThingV VM -- A Thing view model.
--   type Thing = ThingV V    -- A validated Thing.
--   
type family VT v a b