@@ -82,7 +82,7 @@ validateEveryToJSON :: forall proxy api. TMap (Every [Typeable, Show, Arbitrary,
8282 -> Spec
8383validateEveryToJSON _ = props
8484 (Proxy :: Proxy [ToJSON , ToSchema ])
85- (maybeCounterExample . prettyValidateWith validateToJSON)
85+ (maybeCounterExample . renderValidationErrors validateToJSON)
8686 (Proxy :: Proxy (BodyTypes JSON api ))
8787
8888-- | Verify that every type used with @'JSON'@ content type in a servant API
@@ -95,7 +95,7 @@ validateEveryToJSONWithPatternChecker :: forall proxy api. TMap (Every [Typeable
9595 -> Spec
9696validateEveryToJSONWithPatternChecker checker _ = props
9797 (Proxy :: Proxy [ToJSON , ToSchema ])
98- (maybeCounterExample . prettyValidateWith (validateToJSONWithPatternChecker checker))
98+ (maybeCounterExample . renderValidationErrors (validateToJSONWithPatternChecker checker))
9999 (Proxy :: Proxy (BodyTypes JSON api ))
100100
101101-- * QuickCheck-related stuff
@@ -134,65 +134,6 @@ props _ f px = sequence_ specs
134134 aprop :: forall p' a . (EveryTF cs a , Typeable a , Show a , Arbitrary a ) => p' a -> Spec
135135 aprop _ = prop (show (typeOf (undefined :: a ))) (f :: a -> Property )
136136
137- -- | Pretty print validation errors
138- -- together with actual JSON and Swagger Schema
139- -- (using 'encodePretty').
140- --
141- -- >>> import Data.Aeson
142- -- >>> import Data.Foldable (traverse_)
143- -- >>> data Person = Person { name :: String, phone :: Integer } deriving (Generic)
144- -- >>> instance ToJSON Person where toJSON p = object [ "name" .= name p ]
145- -- >>> instance ToSchema Person
146- -- >>> let person = Person { name = "John", phone = 123456 }
147- -- >>> traverse_ putStrLn $ prettyValidateWith validateToJSON person
148- -- Validation against the schema fails:
149- -- * property "phone" is required, but not found in "{\"name\":\"John\"}"
150- -- <BLANKLINE>
151- -- JSON value:
152- -- {
153- -- "name": "John"
154- -- }
155- -- <BLANKLINE>
156- -- Swagger Schema:
157- -- {
158- -- "required": [
159- -- "name",
160- -- "phone"
161- -- ],
162- -- "type": "object",
163- -- "properties": {
164- -- "phone": {
165- -- "type": "integer"
166- -- },
167- -- "name": {
168- -- "type": "string"
169- -- }
170- -- }
171- -- }
172- -- <BLANKLINE>
173- --
174- -- FIXME: this belongs in "Data.Swagger.Schema.Validation" (in @swagger2@).
175- prettyValidateWith
176- :: forall a . (ToJSON a , ToSchema a )
177- => (a -> [ValidationError ]) -> a -> Maybe String
178- prettyValidateWith f x =
179- case f x of
180- [] -> Nothing
181- errors -> Just $ unlines
182- [ " Validation against the schema fails:"
183- , unlines (map (" * " ++ ) errors)
184- , " JSON value:"
185- , ppJSONString json
186- , " "
187- , " Swagger Schema:"
188- , ppJSONString (toJSON schema)
189- ]
190- where
191- ppJSONString = TL. unpack . TL. decodeUtf8 . encodePretty
192-
193- json = toJSON x
194- schema = toSchema (Proxy :: Proxy a )
195-
196137-- | Provide a counterexample if there is any.
197138maybeCounterExample :: Maybe String -> Property
198139maybeCounterExample Nothing = property True
0 commit comments