@@ -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 }