@@ -22,7 +22,6 @@ module Data.Swagger.Internal.Schema.Validation where
23import Control.Applicative
23import Control.Applicative
24import Control.Lens
24import Control.Lens
25−import Control.Lens.TH
26import Control.Monad (when)
25import Control.Monad (when)
28import Data.Aeson hiding (Result)
27import Data.Aeson hiding (Result)
@@ -30,7 +29,6 @@ import Data.Foldable (traverse_, for_, sequenceA_)
30import Data.HashMap.Strict (HashMap)
29import Data.HashMap.Strict (HashMap)
31import qualified Data.HashMap.Strict as HashMap
30import qualified Data.HashMap.Strict as HashMap
32import qualified "unordered-containers" Data.HashSet as HashSet
31import qualified "unordered-containers" Data.HashSet as HashSet
33−import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
34import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
32import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
35import Data.Monoid
33import Data.Monoid
36import Data.Proxy
34import Data.Proxy
@@ -61,11 +59,12 @@ validateToJSON = validateToJSONWithPatternChecker (\_pattern _str -> True)
61-- For validation without patterns see @'validateToJSON'@.
59-- For validation without patterns see @'validateToJSON'@.
62validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) =>
60validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) =>
63 (Pattern -> Text -> Bool) -> a -> [ValidationError]
61 (Pattern -> Text -> Bool) -> a -> [ValidationError]
62+validateToJSONWithPatternChecker checker x =
64−validateToJSONWithPatternChecker checker x = case runValidation (validateWithSchema js) cfg schema of
63+ case runValidation (validateWithSchema js) cfg sch of
65 Failed xs -> xs
64 Failed xs -> xs
66 Passed _ -> mempty
65 Passed _ -> mempty
68− (defs, schema) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
67+ (defs, sch) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
69 js = toJSON x
68 js = toJSON x
70 cfg = defaultConfig
69 cfg = defaultConfig
71 { configPatternChecker = checker
70 { configPatternChecker = checker
@@ -95,7 +94,7 @@ instance Alternative Result where
95instance Monad Result where
94instance Monad Result where
96 return = pure
95 return = pure
97 Passed x >>= f = f x
96 Passed x >>= f = f x
98− Failed xs >>= f = Failed xs
97+ Failed xs >>= _ = Failed xs
100-- | Validation configuration.
99-- | Validation configuration.
101data Config = Config
100data Config = Config
@@ -140,7 +139,7 @@ instance Choice Validation where
141instance Monad (Validation s) where
140instance Monad (Validation s) where
142 return = pure
141 return = pure
143− Validation x >>= f = Validation (\c s -> x c s >>= \x -> runValidation (f x) c s)
142+ Validation x >>= f = Validation (\c s -> x c s >>= \y -> runValidation (f y) c s)
144 (>>) = (*>)
143 (>>) = (*>)
146withConfig :: (Config -> Validation s a) -> Validation s a
145withConfig :: (Config -> Validation s a) -> Validation s a
@@ -160,8 +159,8 @@ valid = pure ()
160-- | Validate schema's property given a lens into that property
159-- | Validate schema's property given a lens into that property
161-- and property checker.
160-- and property checker.
162check :: Lens' s (Maybe a) -> (a -> Validation s ()) -> Validation s ()
161check :: Lens' s (Maybe a) -> (a -> Validation s ()) -> Validation s ()
163−check l g = withSchema $ \schema ->
162+check l g = withSchema $ \sch ->
164− case schema ^. l of
163+ case sch ^. l of
165 Nothing -> valid
164 Nothing -> valid
166 Just x -> g x
165 Just x -> g x
@@ -181,7 +180,7 @@ withRef (Reference ref) f = withConfig $ \cfg ->
181 Just s -> f s
180 Just s -> f s
183validateWithSchemaRef :: Referenced Schema -> Value -> Validation s ()
182validateWithSchemaRef :: Referenced Schema -> Value -> Validation s ()
184−validateWithSchemaRef (Ref ref) js = withRef ref $ \schema -> sub schema (validateWithSchema js)
183+validateWithSchemaRef (Ref ref) js = withRef ref $ \sch -> sub sch (validateWithSchema js)
185validateWithSchemaRef (Inline s) js = sub s (validateWithSchema js)
184validateWithSchemaRef (Inline s) js = sub s (validateWithSchema js)
187-- | Validate JSON @'Value'@ with Swagger @'Schema'@.
186-- | Validate JSON @'Value'@ with Swagger @'Schema'@.
@@ -203,9 +202,9 @@ validateInteger n = do
203 validateNumber n
202 validateNumber n
205validateNumber :: Scientific -> Validation (ParamSchema t) ()
204validateNumber :: Scientific -> Validation (ParamSchema t) ()
206−validateNumber n = withConfig $ \cfg -> withSchema $ \schema -> do
205+validateNumber n = withConfig $ \_cfg -> withSchema $ \sch -> do
207− let exMax = Just True == schema ^. exclusiveMaximum
206+ let exMax = Just True == sch ^. exclusiveMaximum
208− exMin = Just True == schema ^. exclusiveMinimum
207+ exMin = Just True == sch ^. exclusiveMinimum
210 check maximum_ $ \m ->
209 check maximum_ $ \m ->
211 when (if exMax then (n >= m) else (n > m)) $
210 when (if exMax then (n >= m) else (n > m)) $
@@ -262,8 +261,8 @@ validateArray xs = do
262 allUnique = len == HashSet.size (HashSet.fromList (Vector.toList xs))
261 allUnique = len == HashSet.size (HashSet.fromList (Vector.toList xs))
264validateObject :: HashMap Text Value -> Validation Schema ()
263validateObject :: HashMap Text Value -> Validation Schema ()
265−validateObject o = withSchema $ \schema ->
264+validateObject o = withSchema $ \sch ->
266− case schema ^. discriminator of
265+ case sch ^. discriminator of
267 Just pname -> case fromJSON <$> HashMap.lookup pname o of
266 Just pname -> case fromJSON <$> HashMap.lookup pname o of
268 Just (Success ref) -> validateWithSchemaRef ref (Object o)
267 Just (Success ref) -> validateWithSchemaRef ref (Object o)
269 Just (Error msg) -> invalid ("failed to parse discriminator property " ++ show pname ++ ": " ++ show msg)
268 Just (Error msg) -> invalid ("failed to parse discriminator property " ++ show pname ++ ": " ++ show msg)
@@ -282,17 +281,17 @@ validateObject o = withSchema $ \schema ->
283 size = fromIntegral (HashMap.size o)
282 size = fromIntegral (HashMap.size o)
285− validateRequired = withSchema $ \schema -> traverse_ validateReq (schema ^. required)
284+ validateRequired = withSchema $ \sch -> traverse_ validateReq (sch ^. required)
286− validateReq name =
285+ validateReq n =
287− when (not (HashMap.member name o)) $
286+ when (not (HashMap.member n o)) $
288− invalid ("property " ++ show name ++ " is required, but not found in " ++ show (encode o))
287+ invalid ("property " ++ show n ++ " is required, but not found in " ++ show (encode o))
290− validateProps = withSchema $ \schema -> do
289+ validateProps = withSchema $ \sch -> do
291 for_ (HashMap.toList o) $ \(k, v) ->
290 for_ (HashMap.toList o) $ \(k, v) ->
292 case v of
291 case v of
293− Null | not (k `elem` (schema ^. required)) -> valid -- null is fine for non-required property
292+ Null | not (k `elem` (sch ^. required)) -> valid -- null is fine for non-required property
295− case InsOrdHashMap.lookup k (schema ^. properties) of
294+ case InsOrdHashMap.lookup k (sch ^. properties) of
296 Nothing -> check additionalProperties $ \s -> validateWithSchemaRef s v
295 Nothing -> check additionalProperties $ \s -> validateWithSchemaRef s v
297 Just s -> validateWithSchemaRef s v
296 Just s -> validateWithSchemaRef s v
@@ -303,8 +302,8 @@ validateEnum value = do
303 invalid ("expected one of " ++ show (encode xs) ++ " but got " ++ show value)
302 invalid ("expected one of " ++ show (encode xs) ++ " but got " ++ show value)
305validateSchemaType :: Value -> Validation Schema ()
304validateSchemaType :: Value -> Validation Schema ()
306−validateSchemaType value = withSchema $ \schema ->
305+validateSchemaType value = withSchema $ \sch ->
307− case (schema ^. type_, value) of
306+ case (sch ^. type_, value) of
308 (SwaggerNull, Null) -> valid
307 (SwaggerNull, Null) -> valid
309 (SwaggerBoolean, Bool _) -> valid
308 (SwaggerBoolean, Bool _) -> valid
310 (SwaggerInteger, Number n) -> sub_ paramSchema (validateInteger n)
309 (SwaggerInteger, Number n) -> sub_ paramSchema (validateInteger n)
@@ -315,8 +314,8 @@ validateSchemaType value = withSchema $ \schema ->
315 (t, _) -> invalid $ "expected JSON value of type " ++ show t
314 (t, _) -> invalid $ "expected JSON value of type " ++ show t
317validateParamSchemaType :: Value -> Validation (ParamSchema t) ()
316validateParamSchemaType :: Value -> Validation (ParamSchema t) ()
318−validateParamSchemaType value = withSchema $ \schema ->
317+validateParamSchemaType value = withSchema $ \sch ->
319− case (schema ^. type_, value) of
318+ case (sch ^. type_, value) of
320 (SwaggerBoolean, Bool _) -> valid
319 (SwaggerBoolean, Bool _) -> valid
321 (SwaggerInteger, Number n) -> validateInteger n
320 (SwaggerInteger, Number n) -> validateInteger n
322 (SwaggerNumber, Number n) -> validateNumber n
321 (SwaggerNumber, Number n) -> validateNumber n