Skip to content

Commit

Permalink
Add validateToJSONWithPatternChecker helper and ValidationError type …
Browse files Browse the repository at this point in the history
…synonym
  • Loading branch information
fizruk committed Feb 1, 2016
1 parent 434e26b commit cbf93da
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Data/Swagger/Schema/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,35 @@ import Data.Swagger.Lens
-- This can be used with QuickCheck to ensure those instances are coherent:
--
-- prop> validateToJSON (x :: Int) == []
validateToJSON :: forall a. (ToJSON a, ToSchema a) => a -> [String]
validateToJSON x = case runValidation (validateWithSchema js) cfg schema of
--
-- /NOTE:/ @'validateToJSON'@ does not perform string pattern validation.
-- See @'validateToJSONWithPatternChecker'@.
validateToJSON :: forall a. (ToJSON a, ToSchema a) => a -> [ValidationError]
validateToJSON = validateToJSONWithPatternChecker (\_pattern _str -> True)

-- | Validate @'ToJSON'@ instance matches @'ToSchema'@ for a given value and pattern checker.
-- This can be used with QuickCheck to ensure those instances are coherent.
--
-- For validation without patterns see @'validateToJSON'@.
validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) =>
(Pattern -> Text -> Bool) -> a -> [ValidationError]
validateToJSONWithPatternChecker checker x = case runValidation (validateWithSchema js) cfg schema of
Failed xs -> xs
Passed _ -> mempty
where
js = toJSON x
cfg = defaultConfig { configDefinitions = defs }
(defs, schema) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
js = toJSON x
cfg = defaultConfig
{ configPatternChecker = checker
, configDefinitions = defs }

-- | Validation error message.
type ValidationError = String

-- | Validation result type.
data Result a
= Failed [String] -- ^ Validation failed with a list of error messages.
| Passed a -- ^ Validation passed.
= Failed [ValidationError] -- ^ Validation failed with a list of error messages.
| Passed a -- ^ Validation passed.
deriving (Eq, Show, Functor)

instance Applicative Result where
Expand Down

0 comments on commit cbf93da

Please sign in to comment.