git.iko.soy/typeable/swagger2
on master help sign in
Add validateJSON* functions for values without classes
80e7390Nickolay Kudasov
src/Data/Swagger/Internal/Schema/Validation.hs+19−5
@@ -57,15 +57,29 @@ validateToJSON = validateToJSONWithPatternChecker (\_pattern _str -> True)
57-- This can be used with QuickCheck to ensure those instances are coherent.
57-- This can be used with QuickCheck to ensure those instances are coherent.
58--
58--
59-- For validation without patterns see @'validateToJSON'@.
59-- For validation without patterns see @'validateToJSON'@.
60−validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) =>
60+validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) => (Pattern -> Text -> Bool) -> a -> [ValidationError]
61− (Pattern -> Text -> Bool) -> a -> [ValidationError]
61+validateToJSONWithPatternChecker checker = validateJSONWithPatternChecker checker defs sch . toJSON
62−validateToJSONWithPatternChecker checker x =
62+ where
63+ (defs, sch) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
64+
65+-- | Validate JSON @'Value'@ against Swagger @'Schema'@.
66+--
67+-- prop> validateJSON mempty (toSchema (Proxy :: Proxy Int)) (toJSON (x :: Int)) == []
68+--
69+-- /NOTE:/ @'validateJSON'@ does not perform string pattern validation.
70+-- See @'validateJSONWithPatternChecker'@.
71+validateJSON :: Definitions Schema -> Schema -> Value -> [ValidationError]
72+validateJSON = validateJSONWithPatternChecker (\_pattern _str -> True)
73+
74+-- | Validate JSON @'Value'@ agains Swagger @'ToSchema'@ for a given value and pattern checker.
75+--
76+-- For validation without patterns see @'validateJSON'@.
77+validateJSONWithPatternChecker :: (Pattern -> Text -> Bool) -> Definitions Schema -> Schema -> Value -> [ValidationError]
78+validateJSONWithPatternChecker checker defs sch js =
63 case runValidation (validateWithSchema js) cfg sch of
79 case runValidation (validateWithSchema js) cfg sch of
64 Failed xs -> xs
80 Failed xs -> xs
65 Passed _ -> mempty
81 Passed _ -> mempty
66 where
82 where
67− (defs, sch) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
68− js = toJSON x
69 cfg = defaultConfig
83 cfg = defaultConfig
70 { configPatternChecker = checker
84 { configPatternChecker = checker
71 , configDefinitions = defs }
85 , configDefinitions = defs }
src/Data/Swagger/Schema/Validation.hs+8−1
@@ -15,9 +15,16 @@ module Data.Swagger.Schema.Validation (
15 -- $maybe
15 -- $maybe
16
16
17 -- * JSON validation
17 -- * JSON validation
18+
19+ ValidationError,
20+
21+ -- ** Using 'ToJSON' and 'ToSchema'
18 validateToJSON,
22 validateToJSON,
19 validateToJSONWithPatternChecker,
23 validateToJSONWithPatternChecker,
20− ValidationError,
24+
25+ -- ** Using 'Value' and 'Schema'
26+ validateJSON,
27+ validateJSONWithPatternChecker,
21) where
28) where
22
29
23import Data.Swagger.Internal.Schema.Validation
30import Data.Swagger.Internal.Schema.Validation