@@ -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.
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
65+-- | Validate JSON @'Value'@ against Swagger @'Schema'@.
67+-- prop> validateJSON mempty (toSchema (Proxy :: Proxy Int)) (toJSON (x :: Int)) == []
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)
74+-- | Validate JSON @'Value'@ agains Swagger @'ToSchema'@ for a given value and pattern checker.
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
67− (defs, sch) = runDeclare (declareSchema (Proxy :: Proxy a)) mempty
69 cfg = defaultConfig
83 cfg = defaultConfig
70 { configPatternChecker = checker
84 { configPatternChecker = checker
71 , configDefinitions = defs }
85 , configDefinitions = defs }