git.iko.soy/typeable/swagger2
on master help sign in
Add validateJSON* functions for values without classes
commit80e73907c6f3ef2a86dc51e991d15da985d7ecca authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateFri 6 Oct 2017, 19:04 (9 years ago) parentffd2d00
2 files changed+27−6
src/Data/Swagger/Internal/Schema/Validation.hs+19−5 src/Data/Swagger/Schema/Validation.hs+8−1
src/Data/Swagger/Internal/Schema/Validation.hs+19−5
@@ -57,15 +57,29 @@ validateToJSON = validateToJSONWithPatternChecker (\_pattern _str -> True)
5757-- This can be used with QuickCheck to ensure those instances are coherent.
5858--
5959-- For validation without patterns see @'validateToJSON'@.
60−validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) =>
61− (Pattern -> Text -> Bool) -> a -> [ValidationError]
62−validateToJSONWithPatternChecker checker x =
60+validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) => (Pattern -> Text -> Bool) -> a -> [ValidationError]
61+validateToJSONWithPatternChecker checker = validateJSONWithPatternChecker checker defs sch . toJSON
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 =
6379 case runValidation (validateWithSchema js) cfg sch of
6480 Failed xs -> xs
6581 Passed _ -> mempty
6682 where
67− (defs, sch) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
68− js = toJSON x
6983 cfg = defaultConfig
7084 { configPatternChecker = checker
7185 , configDefinitions = defs }
src/Data/Swagger/Schema/Validation.hs+8−1
@@ -15,9 +15,16 @@ module Data.Swagger.Schema.Validation (
1515 -- $maybe
1616
1717 -- * JSON validation
18+
19+ ValidationError,
20+
21+ -- ** Using 'ToJSON' and 'ToSchema'
1822 validateToJSON,
1923 validateToJSONWithPatternChecker,
20− ValidationError,
24+
25+ -- ** Using 'Value' and 'Schema'
26+ validateJSON,
27+ validateJSONWithPatternChecker,
2128) where
2229
2330import Data.Swagger.Internal.Schema.Validation