@@ -22,7 +22,6 @@ module Data.Swagger.Internal.Schema.Validation where
2222
2323import Control.Applicative
2424import Control.Lens
25−import Control.Lens.TH
2625import Control.Monad (when)
2726
2827import Data.Aeson hiding (Result)
@@ -30,7 +29,6 @@ import Data.Foldable (traverse_, for_, sequenceA_)
3029import Data.HashMap.Strict (HashMap)
3130import qualified Data.HashMap.Strict as HashMap
3231import qualified "unordered-containers" Data.HashSet as HashSet
33−import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
3432import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
3533import Data.Monoid
3634import Data.Proxy
@@ -61,11 +59,12 @@ validateToJSON = validateToJSONWithPatternChecker (\_pattern _str -> True)
6159-- For validation without patterns see @'validateToJSON'@.
6260validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) =>
6361 (Pattern -> Text -> Bool) -> a -> [ValidationError]
64−validateToJSONWithPatternChecker checker x = case runValidation (validateWithSchema js) cfg schema of
62+validateToJSONWithPatternChecker checker x =
63+ case runValidation (validateWithSchema js) cfg sch of
6564 Failed xs -> xs
6665 Passed _ -> mempty
6766 where
68− (defs, schema) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
67+ (defs, sch) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
6968 js = toJSON x
7069 cfg = defaultConfig
7170 { configPatternChecker = checker
@@ -95,7 +94,7 @@ instance Alternative Result where
9594instance Monad Result where
9695 return = pure
9796 Passed x >>= f = f x
98− Failed xs >>= f = Failed xs
97+ Failed xs >>= _ = Failed xs
9998
10099-- | Validation configuration.
101100data Config = Config
@@ -140,7 +139,7 @@ instance Choice Validation where
140139
141140instance Monad (Validation s) where
142141 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)
144143 (>>) = (*>)
145144
146145withConfig :: (Config -> Validation s a) -> Validation s a
@@ -160,8 +159,8 @@ valid = pure ()
160159-- | Validate schema's property given a lens into that property
161160-- and property checker.
162161check :: Lens' s (Maybe a) -> (a -> Validation s ()) -> Validation s ()
163−check l g = withSchema $ \schema ->
164− case schema ^. l of
162+check l g = withSchema $ \sch ->
163+ case sch ^. l of
165164 Nothing -> valid
166165 Just x -> g x
167166
@@ -181,7 +180,7 @@ withRef (Reference ref) f = withConfig $ \cfg ->
181180 Just s -> f s
182181
183182validateWithSchemaRef :: 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)
185184validateWithSchemaRef (Inline s) js = sub s (validateWithSchema js)
186185
187186-- | Validate JSON @'Value'@ with Swagger @'Schema'@.
@@ -203,9 +202,9 @@ validateInteger n = do
203202 validateNumber n
204203
205204validateNumber :: Scientific -> Validation (ParamSchema t) ()
206−validateNumber n = withConfig $ \cfg -> withSchema $ \schema -> do
207− let exMax = Just True == schema ^. exclusiveMaximum
208− exMin = Just True == schema ^. exclusiveMinimum
205+validateNumber n = withConfig $ \_cfg -> withSchema $ \sch -> do
206+ let exMax = Just True == sch ^. exclusiveMaximum
207+ exMin = Just True == sch ^. exclusiveMinimum
209208
210209 check maximum_ $ \m ->
211210 when (if exMax then (n >= m) else (n > m)) $
@@ -262,8 +261,8 @@ validateArray xs = do
262261 allUnique = len == HashSet.size (HashSet.fromList (Vector.toList xs))
263262
264263validateObject :: HashMap Text Value -> Validation Schema ()
265−validateObject o = withSchema $ \schema ->
266− case schema ^. discriminator of
264+validateObject o = withSchema $ \sch ->
265+ case sch ^. discriminator of
267266 Just pname -> case fromJSON <$> HashMap.lookup pname o of
268267 Just (Success ref) -> validateWithSchemaRef ref (Object o)
269268 Just (Error msg) -> invalid ("failed to parse discriminator property " ++ show pname ++ ": " ++ show msg)
@@ -282,17 +281,17 @@ validateObject o = withSchema $ \schema ->
282281 where
283282 size = fromIntegral (HashMap.size o)
284283
285− validateRequired = withSchema $ \schema -> traverse_ validateReq (schema ^. required)
286− validateReq name =
287− when (not (HashMap.member name o)) $
288− invalid ("property " ++ show name ++ " is required, but not found in " ++ show (encode o))
284+ validateRequired = withSchema $ \sch -> traverse_ validateReq (sch ^. required)
285+ validateReq n =
286+ when (not (HashMap.member n o)) $
287+ invalid ("property " ++ show n ++ " is required, but not found in " ++ show (encode o))
289288
290− validateProps = withSchema $ \schema -> do
289+ validateProps = withSchema $ \sch -> do
291290 for_ (HashMap.toList o) $ \(k, v) ->
292291 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
294293 _ ->
295− case InsOrdHashMap.lookup k (schema ^. properties) of
294+ case InsOrdHashMap.lookup k (sch ^. properties) of
296295 Nothing -> check additionalProperties $ \s -> validateWithSchemaRef s v
297296 Just s -> validateWithSchemaRef s v
298297
@@ -303,8 +302,8 @@ validateEnum value = do
303302 invalid ("expected one of " ++ show (encode xs) ++ " but got " ++ show value)
304303
305304validateSchemaType :: Value -> Validation Schema ()
306−validateSchemaType value = withSchema $ \schema ->
307− case (schema ^. type_, value) of
305+validateSchemaType value = withSchema $ \sch ->
306+ case (sch ^. type_, value) of
308307 (SwaggerNull, Null) -> valid
309308 (SwaggerBoolean, Bool _) -> valid
310309 (SwaggerInteger, Number n) -> sub_ paramSchema (validateInteger n)
@@ -315,8 +314,8 @@ validateSchemaType value = withSchema $ \schema ->
315314 (t, _) -> invalid $ "expected JSON value of type " ++ show t
316315
317316validateParamSchemaType :: Value -> Validation (ParamSchema t) ()
318−validateParamSchemaType value = withSchema $ \schema ->
319− case (schema ^. type_, value) of
317+validateParamSchemaType value = withSchema $ \sch ->
318+ case (sch ^. type_, value) of
320319 (SwaggerBoolean, Bool _) -> valid
321320 (SwaggerInteger, Number n) -> validateInteger n
322321 (SwaggerNumber, Number n) -> validateNumber n