Skip to content

Commit 0c785a2

Browse files
author
Artyom Kazak
committed
Print errors in 'validateEveryToJSON'
Fixes #81
1 parent f00aacc commit 0c785a2

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
next
2+
-----
3+
4+
* Fixes:
5+
* `validateEveryToJSON` now prints validation errors
6+
17
1.1.5
28
-----
39

src/Servant/Swagger/Internal/Test.hs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Data.Text (Text)
1212
import Data.Typeable
1313
import Test.Hspec
1414
import Test.Hspec.QuickCheck
15-
import Test.QuickCheck (Arbitrary)
15+
import Test.QuickCheck (Arbitrary, Property, property, counterexample)
1616

1717
import Servant.API
1818
import Servant.Swagger.Internal.TypeLevel
@@ -76,7 +76,7 @@ validateEveryToJSON :: forall proxy api. TMap (Every [Typeable, Show, Arbitrary,
7676
-> Spec
7777
validateEveryToJSON _ = props
7878
(Proxy :: Proxy [ToJSON, ToSchema])
79-
(null . validateToJSON)
79+
(reportErrors . validateToJSON)
8080
(Proxy :: Proxy (BodyTypes JSON api))
8181

8282
-- | Verify that every type used with @'JSON'@ content type in a servant API
@@ -89,7 +89,7 @@ validateEveryToJSONWithPatternChecker :: forall proxy api. TMap (Every [Typeable
8989
-> Spec
9090
validateEveryToJSONWithPatternChecker checker _ = props
9191
(Proxy :: Proxy [ToJSON, ToSchema])
92-
(null . validateToJSONWithPatternChecker checker)
92+
(reportErrors . validateToJSONWithPatternChecker checker)
9393
(Proxy :: Proxy (BodyTypes JSON api))
9494

9595
-- * QuickCheck-related stuff
@@ -102,7 +102,7 @@ validateEveryToJSONWithPatternChecker checker _ = props
102102
-- context "read . show == id" $
103103
-- props
104104
-- (Proxy :: Proxy [Eq, Show, Read])
105-
-- (\x -> read (show x) == x)
105+
-- (\x -> read (show x) === x)
106106
-- (Proxy :: Proxy [Bool, Int, String])
107107
-- :}
108108
-- <BLANKLINE>
@@ -116,15 +116,23 @@ validateEveryToJSONWithPatternChecker checker _ = props
116116
-- Finished in ... seconds
117117
-- 3 examples, 0 failures
118118
props :: forall p p'' cs xs. TMap (Every (Typeable ': Show ': Arbitrary ': cs)) xs =>
119-
p cs -- ^ A list of constraints.
120-
-> (forall x. EveryTF cs x => x -> Bool) -- ^ Property predicate.
121-
-> p'' xs -- ^ A list of types.
119+
p cs -- ^ A list of constraints.
120+
-> (forall x. EveryTF cs x => x -> Property) -- ^ Property predicate.
121+
-> p'' xs -- ^ A list of types.
122122
-> Spec
123123
props _ f px = sequence_ specs
124124
where
125125
specs :: [Spec]
126126
specs = tmapEvery (Proxy :: Proxy (Typeable ': Show ': Arbitrary ': cs)) aprop px
127127

128128
aprop :: forall p' a. (EveryTF cs a, Typeable a, Show a, Arbitrary a) => p' a -> Spec
129-
aprop _ = prop (show (typeOf (undefined :: a))) (f :: a -> Bool)
129+
aprop _ = prop (show (typeOf (undefined :: a))) (f :: a -> Property)
130130

131+
-- | A property that prints a nicely formatted list of errors if there are
132+
-- any.
133+
reportErrors :: [ValidationError] -> Property
134+
reportErrors [] = property True
135+
reportErrors ex = counterexample errString (property False)
136+
where
137+
errString = unlines $ "Validation against the schema fails:"
138+
: map (" * " ++) ex

0 commit comments

Comments
 (0)