@@ -82,7 +82,7 @@ validateEveryToJSON :: forall proxy api. TMap (Every [Typeable, Show, Arbitrary,
82
82
-> Spec
83
83
validateEveryToJSON _ = props
84
84
(Proxy :: Proxy [ToJSON , ToSchema ])
85
- (maybeCounterExample . prettyValidateWith validateToJSON)
85
+ (maybeCounterExample . renderValidationErrors validateToJSON)
86
86
(Proxy :: Proxy (BodyTypes JSON api ))
87
87
88
88
-- | Verify that every type used with @'JSON'@ content type in a servant API
@@ -95,7 +95,7 @@ validateEveryToJSONWithPatternChecker :: forall proxy api. TMap (Every [Typeable
95
95
-> Spec
96
96
validateEveryToJSONWithPatternChecker checker _ = props
97
97
(Proxy :: Proxy [ToJSON , ToSchema ])
98
- (maybeCounterExample . prettyValidateWith (validateToJSONWithPatternChecker checker))
98
+ (maybeCounterExample . renderValidationErrors (validateToJSONWithPatternChecker checker))
99
99
(Proxy :: Proxy (BodyTypes JSON api ))
100
100
101
101
-- * QuickCheck-related stuff
@@ -134,65 +134,6 @@ props _ f px = sequence_ specs
134
134
aprop :: forall p' a . (EveryTF cs a , Typeable a , Show a , Arbitrary a ) => p' a -> Spec
135
135
aprop _ = prop (show (typeOf (undefined :: a ))) (f :: a -> Property )
136
136
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
-
196
137
-- | Provide a counterexample if there is any.
197
138
maybeCounterExample :: Maybe String -> Property
198
139
maybeCounterExample Nothing = property True
0 commit comments