git.iko.soy/typeable/hjsonschema
on master help sign in
Make an ArbitraryValue newtype.
commitafbe67ba83e463a20afa02cdd21a969ff7264290 authorIan Grant Jeffries <ian@housejeffries.com> dateSat 30 Jul 2016, 15:15 (10 years ago) parentce5ebb7
3 files changed+29−27
src/Data/JsonSchema/Draft4.hs+1−3 src/Data/Validator/Draft4/Any.hs+2−2 src/Data/Validator/Utils.hs+26−22
src/Data/JsonSchema/Draft4.hs+1−3
@@ -42,7 +42,6 @@ import Prelude
4242
4343import Control.Arrow (first, left)
4444import qualified Data.ByteString as BS
45−import qualified Data.ByteString.Lazy as LBS
4645import Data.FileEmbed (embedFile,
4746 makeRelativeToProject)
4847import qualified Data.HashMap.Strict as HM
@@ -150,8 +149,7 @@ checkSchema sm sw =
150149metaSchema :: Schema
151150metaSchema =
152151 fromMaybe (error "Schema decode failed (this should never happen)")
153− . decode
154− . LBS.fromStrict
152+ . decodeStrict
155153 $ metaSchemaBytes
156154
157155metaSchemaBytes :: BS.ByteString
src/Data/Validator/Draft4/Any.hs+2−2
@@ -96,9 +96,9 @@ instance ToJSON EnumVal where
9696
9797instance Arbitrary EnumVal where
9898 arbitrary = do
99− xs <- traverse (const UT.arbitraryValue) =<< (arbitrary :: Gen [()])
99+ xs <- (fmap.fmap) UT._unArbitraryValue arbitrary
100100 case NE.nonEmpty (toUnique xs) of
101− Nothing -> EnumVal . pure <$> UT.arbitraryValue
101+ Nothing -> EnumVal . pure . UT._unArbitraryValue <$> arbitrary
102102 Just ne -> pure (EnumVal ne)
103103 where
104104 toUnique :: [Value] -> [Value]
src/Data/Validator/Utils.hs+26−22
@@ -29,28 +29,32 @@ arbitraryPositiveScientific = (fromFloatDigits :: Double -> Scientific)
2929 . getPositive
3030 <$> arbitrary
3131
32−arbitraryValue :: Gen Value
33−arbitraryValue = sized f
34− where
35− f :: Int -> Gen Value
36− f n | n <= 1 = oneof nonRecursive
37− | otherwise = oneof $
38− fmap (Array . V.fromList) (traverse (const (f (n `div` 10)))
39− =<< (arbitrary :: Gen [()]))
40− : fmap (Object . HM.fromList) (traverse (const (g (n `div` 10)))
41− =<< (arbitrary :: Gen [()]))
42− : nonRecursive
43−
44− g :: Int -> Gen (Text, Value)
45− g n = (,) <$> arbitraryText <*> f n
46−
47− nonRecursive :: [Gen Value]
48− nonRecursive =
49− [ pure Null
50− , Bool <$> arbitrary
51− , String <$> arbitraryText
52− , Number <$> arbitraryScientific
53− ]
32+newtype ArbitraryValue
33+ = ArbitraryValue { _unArbitraryValue :: Value }
34+ deriving (Eq, Show)
35+
36+instance Arbitrary ArbitraryValue where
37+ arbitrary = ArbitraryValue <$> sized f
38+ where
39+ f :: Int -> Gen Value
40+ f n | n <= 1 = oneof nonRecursive
41+ | otherwise = oneof $
42+ fmap (Array . V.fromList) (traverse (const (f (n `div` 10)))
43+ =<< (arbitrary :: Gen [()]))
44+ : fmap (Object . HM.fromList) (traverse (const (g (n `div` 10)))
45+ =<< (arbitrary :: Gen [()]))
46+ : nonRecursive
47+
48+ g :: Int -> Gen (Text, Value)
49+ g n = (,) <$> arbitraryText <*> f n
50+
51+ nonRecursive :: [Gen Value]
52+ nonRecursive =
53+ [ pure Null
54+ , Bool <$> arbitrary
55+ , String <$> arbitraryText
56+ , Number <$> arbitraryScientific
57+ ]
5458
5559arbitraryHashMap :: Arbitrary a => Gen (HashMap Text a)
5660arbitraryHashMap = HM.fromList . fmap (first T.pack) <$> arbitrary