@@ -1,6 +1,7 @@
1module SchemaSpec (spec, main) where
1module SchemaSpec (spec, main) where
3import Control.Monad
3import Control.Monad
4+import Data.ByteString.Lazy
4import Data.Aeson
5import Data.Aeson
5import Data.Proxy
6import Data.Proxy
6import Data.Schematic
7import Data.Schematic
@@ -15,7 +16,7 @@ import Test.SmallCheck.Series.Instances
15type SchemaExample
16type SchemaExample
16 = SchemaObject
17 = SchemaObject
17 '[ '("foo", SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
18 '[ '("foo", SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
18− , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+"]))]
19+ , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+", TEnum '["foo", "bar"]]))]
20exampleTest :: JsonRepr (SchemaOptional (SchemaText '[TEq 3]))
21exampleTest :: JsonRepr (SchemaOptional (SchemaText '[TEq 3]))
21exampleTest = ReprOptional (Just (ReprText "lil"))
22exampleTest = ReprOptional (Just (ReprText "lil"))
@@ -26,19 +27,17 @@ exampleNumber = ReprNumber 12
26exampleArray :: JsonRepr (SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
27exampleArray :: JsonRepr (SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
27exampleArray = ReprArray [exampleNumber]
28exampleArray = ReprArray [exampleNumber]
29−exampleObject :: JsonRepr SchemaExample
30−exampleObject = ReprObject $ FieldRepr exampleArray
31− :& FieldRepr (ReprOptional (Just (ReprText "barval")))
34jsonExample :: JsonRepr SchemaExample
30jsonExample :: JsonRepr SchemaExample
35jsonExample = ReprObject $
31jsonExample = ReprObject $
36 FieldRepr (ReprArray [ReprNumber 12])
32 FieldRepr (ReprArray [ReprNumber 12])
37− :& FieldRepr (ReprOptional (Just (ReprText "tes")))
33+ :& FieldRepr (ReprOptional (Just (ReprText "bar")))
40−-- schemaExample :: Sing SchemaExample
36+schemaJson :: ByteString
41−-- schemaExample = known
37+schemaJson = "{\"foo\": [13], \"bar\": null}"
39+schemaJson2 :: ByteString
40+schemaJson2 = "{\"foo\": [3], \"bar\": null}"
43spec :: Spec
42spec :: Spec
@@ -46,6 +45,15 @@ spec = do
46 -- read (show example) == example
45 -- read (show example) == example
47 it "decode/encode JsonRepr properly" $
46 it "decode/encode JsonRepr properly" $
48 decode (encode jsonExample) == Just jsonExample
47 decode (encode jsonExample) == Just jsonExample
48+ it "validates correct representation" $
49+ ((decodeAndValidateJson schemaJson) :: ParseResult (JsonRepr SchemaExample))
50+ `shouldSatisfy` isValid
51+ it "returns decoding error on structurally incorrect input" $
52+ ((decodeAndValidateJson "{}") :: ParseResult (JsonRepr SchemaExample))
53+ `shouldSatisfy` isDecodingError
54+ it "validates incorrect representation" $
55+ ((decodeAndValidateJson schemaJson2) :: ParseResult (JsonRepr SchemaExample))
56+ `shouldSatisfy` isValidationError
50main :: IO ()
58main :: IO ()
51main = hspec spec
59main = hspec spec