Skip to content

Commit

Permalink
Merge pull request #141 from parsonsmatt/master
Browse files Browse the repository at this point in the history
NonEmpty ToSchema instance

* parsonsmatt-master:
  Remove unused instance code
  use quickcheck-instances
  Fix tests
  Add semigroups build dep
  (squash-me) support GHC 7.x
  Add ToSchema NonEmpty instance for GHC 8.
  • Loading branch information
fizruk committed Mar 9, 2018
2 parents 5275f62 + 7765211 commit 22e975e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
7 changes: 7 additions & 0 deletions src/Data/Swagger/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import Data.Int
import Data.IntSet (IntSet)
import Data.IntMap (IntMap)
import Data.List.NonEmpty (NonEmpty)
import Data.Map (Map)
import Data.Proxy
import Data.Scientific (Scientific)
Expand Down Expand Up @@ -561,6 +562,12 @@ instance ToSchema a => ToSchema (Set a) where

instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))

instance ToSchema a => ToSchema (NonEmpty a) where
declareNamedSchema _ = do
schema <- declareSchema (Proxy :: Proxy [a])
return $ unnamed $ schema
& minItems .~ Just 1

instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema

Expand Down
6 changes: 6 additions & 0 deletions swagger2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ library
, uuid-types >=1.0.2 && <1.1
if !impl(ghc >= 7.10)
build-depends: nats >=1.1.1 && <1.2
if !impl(ghc >= 8.0)
build-depends: semigroups >= 0.18.3 && <0.19
default-language: Haskell2010

test-suite spec
Expand All @@ -90,12 +92,16 @@ test-suite spec
, HUnit
, mtl
, QuickCheck >=2.8.2
, quickcheck-instances
, swagger2
, text
, time
, unordered-containers
, vector
, lens
if !impl(ghc >= 8.0)
build-depends: semigroups

other-modules:
SpecCommon
Data.SwaggerSpec
Expand Down
33 changes: 3 additions & 30 deletions test/Data/Swagger/Schema/ValidationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "unordered-containers" Data.HashSet (HashSet)
import qualified "unordered-containers" Data.HashSet as HashSet
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.List.NonEmpty (NonEmpty(..), nonEmpty)
import Data.Map (Map)
import Data.Monoid (mempty)
import Data.Proxy
Expand All @@ -32,6 +33,7 @@ import Data.Swagger.Declare
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
import Test.QuickCheck.Instances ()

shouldValidate :: (ToJSON a, ToSchema a) => Proxy a -> a -> Bool
shouldValidate _ x = validateToJSON x == []
Expand Down Expand Up @@ -69,6 +71,7 @@ spec = do
-- prop "(Maybe [Int])" $ shouldValidate (Proxy :: Proxy (Maybe [Int]))
prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
prop "(NonEmpty Bool)" $ shouldValidate (Proxy :: Proxy (NonEmpty Bool))
prop "(HashSet Bool)" $ shouldValidate (Proxy :: Proxy (HashSet Bool))
prop "(Either Int String)" $ shouldValidate (Proxy :: Proxy (Either Int String))
prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
Expand Down Expand Up @@ -228,35 +231,5 @@ invalidButtonImagesToJSON = genericToJSON defaultOptions
instance Arbitrary ButtonImages where
arbitrary = ButtonImages <$> arbitrary

-- Arbitrary instances for common types

instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => Arbitrary (HashMap k v) where
arbitrary = HashMap.fromList <$> arbitrary

instance (Eq a, Hashable a, Arbitrary a) => Arbitrary (HashSet a) where
arbitrary = HashSet.fromList <$> arbitrary

instance Arbitrary T.Text where
arbitrary = T.pack <$> arbitrary

instance Arbitrary TL.Text where
arbitrary = TL.pack <$> arbitrary

instance Arbitrary Day where
arbitrary = liftA3 fromGregorian (fmap ((+ 1) . abs) arbitrary) arbitrary arbitrary

instance Arbitrary LocalTime where
arbitrary = LocalTime
<$> arbitrary
<*> liftA3 TimeOfDay (choose (0, 23)) (choose (0, 59)) (fromInteger <$> choose (0, 60))

instance Eq ZonedTime where
ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y

instance Arbitrary ZonedTime where
arbitrary = ZonedTime
<$> arbitrary
<*> liftA3 TimeZone arbitrary arbitrary (vectorOf 3 (elements ['A'..'Z']))

instance Arbitrary UTCTime where
arbitrary = UTCTime <$> arbitrary <*> fmap fromInteger (choose (0, 86400))

0 comments on commit 22e975e

Please sign in to comment.