@@ -1,6 +1,7 @@
11module SchemaSpec (spec, main) where
22
33import Control.Monad
4+import Data.ByteString.Lazy
45import Data.Aeson
56import Data.Proxy
67import Data.Schematic
@@ -15,7 +16,7 @@ import Test.SmallCheck.Series.Instances
1516type SchemaExample
1617 = SchemaObject
1718 '[ '("foo", SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
18− , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+"]))]
19+ , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+", TEnum '["foo", "bar"]]))]
1920
2021exampleTest :: JsonRepr (SchemaOptional (SchemaText '[TEq 3]))
2122exampleTest = ReprOptional (Just (ReprText "lil"))
@@ -26,19 +27,17 @@ exampleNumber = ReprNumber 12
2627exampleArray :: JsonRepr (SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))
2728exampleArray = ReprArray [exampleNumber]
2829
29−exampleObject :: JsonRepr SchemaExample
30−exampleObject = ReprObject $ FieldRepr exampleArray
31− :& FieldRepr (ReprOptional (Just (ReprText "barval")))
32− :& RNil
33−
3430jsonExample :: JsonRepr SchemaExample
3531jsonExample = ReprObject $
3632 FieldRepr (ReprArray [ReprNumber 12])
37− :& FieldRepr (ReprOptional (Just (ReprText "tes")))
33+ :& FieldRepr (ReprOptional (Just (ReprText "bar")))
3834 :& RNil
3935
40−-- schemaExample :: Sing SchemaExample
41−-- schemaExample = known
36+schemaJson :: ByteString
37+schemaJson = "{\"foo\": [13], \"bar\": null}"
38+
39+schemaJson2 :: ByteString
40+schemaJson2 = "{\"foo\": [3], \"bar\": null}"
4241
4342spec :: Spec
4443spec = do
@@ -46,6 +45,15 @@ spec = do
4645 -- read (show example) == example
4746 it "decode/encode JsonRepr properly" $
4847 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
4957
5058main :: IO ()
5159main = hspec spec