-- 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: -- --
-- 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):
--
-- -- 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. -- --
-- >>> 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. -- --
-- 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