git.iko.soy/typeable/schematic
on master help sign in
Add jsonschema test case
0df72dbDenis Redozubov
README.md+2
@@ -127,6 +127,8 @@ There are two ways of working with named fields in the objects:
127* Using the `fget` and `fset` functions: `fget fooProxy (fput newFooVal objectData) == newFooVal`
127* Using the `fget` and `fset` functions: `fget fooProxy (fput newFooVal objectData) == newFooVal`
128* Using the lens library: `set (flens (Proxy @"foo")) newFooVal objectData ^. flens (Proxy @"foo") == newFooVal`
128* Using the lens library: `set (flens (Proxy @"foo")) newFooVal objectData ^. flens (Proxy @"foo") == newFooVal`
129
129
130+## Export to json-schema (draft 4)
131+
130
132
131## TODO
133## TODO
132
134
schematic.cabal+2−1
@@ -96,6 +96,7 @@ test-suite spec
96 , GADTs
96 , GADTs
97 , KindSignatures
97 , KindSignatures
98 , InstanceSigs
98 , InstanceSigs
99+ , LambdaCase
99 , MultiParamTypeClasses
100 , MultiParamTypeClasses
100 , OverloadedLists
101 , OverloadedLists
101 , OverloadedStrings
102 , OverloadedStrings
@@ -125,7 +126,7 @@ test-suite spec
125 , schematic
126 , schematic
126 , smallcheck
127 , smallcheck
127 , smallcheck-series
128 , smallcheck-series
128− , singletons >= 2.3.1
129+ , singletons
129 , tagged
130 , tagged
130 , text
131 , text
131 , unordered-containers
132 , unordered-containers
test/JsonSchemaSpec.hsnew file+43
@@ -0,0 +1,43 @@
1+module JsonSchemaSpec (spec, main) where
2+
3+import Data.Aeson as J
4+import Data.Proxy
5+import Data.Schematic
6+import Data.Vinyl
7+import JSONSchema.Draft4 as D4
8+import Test.Hspec
9+
10+
11+type ArraySchema = 'SchemaArray '[ 'AEq 1] ('SchemaNumber '[ 'NGt 10])
12+
13+type ArrayField = '("foo", ArraySchema)
14+
15+type FieldsSchema =
16+ '[ ArrayField, '("bar", 'SchemaOptional ('SchemaText '[ 'TEnum '["foo", "bar"]]))]
17+
18+type SchemaExample = 'SchemaObject FieldsSchema
19+
20+arrayData :: JsonRepr ArraySchema
21+arrayData = ReprArray [ReprNumber 13]
22+
23+arrayField :: FieldRepr ArrayField
24+arrayField = FieldRepr arrayData
25+
26+objectData :: Rec FieldRepr FieldsSchema
27+objectData = FieldRepr arrayData
28+ :& FieldRepr (ReprOptional (Just (ReprText "foo")))
29+ :& RNil
30+
31+exampleData :: JsonRepr SchemaExample
32+exampleData = ReprObject objectData
33+
34+spec :: Spec
35+spec = do
36+ it "validates simple schema" $ do
37+ let schema = D4.SchemaWithURI (toJsonSchema (Proxy @SchemaExample)) Nothing
38+ fetchHTTPAndValidate schema (toJSON exampleData) >>= \case
39+ Left _ -> fail "failed to validate test example"
40+ Right _ -> pure ()
41+
42+main :: IO ()
43+main = hspec spec
test/LensSpec.hs+4−4
@@ -305,16 +305,16 @@ spec = do
305 newFooVal = FieldRepr $ ReprArray [ReprNumber 15]
305 newFooVal = FieldRepr $ ReprArray [ReprNumber 15]
306 fooProxy = Proxy @"foo"
306 fooProxy = Proxy @"foo"
307 it "gets the field from an object" $ do
307 it "gets the field from an object" $ do
308− fget fooProxy objectData == arrayField
308+ fget fooProxy objectData `shouldBe` arrayField
309 it "sets the object field" $ do
309 it "sets the object field" $ do
310− fget fooProxy (fput newFooVal objectData) == newFooVal
310+ fget fooProxy (fput newFooVal objectData) `shouldBe` newFooVal
311
311
312 describe "(using lens library) " $ do
312 describe "(using lens library) " $ do
313 it "get the field from an object" $ do
313 it "get the field from an object" $ do
314− objectData ^. flens (Proxy @"foo") == arrayField
314+ objectData ^. flens (Proxy @"foo") `shouldBe` arrayField
315 it "sets the object field" $ do
315 it "sets the object field" $ do
316 set (flens (Proxy @"foo")) newFooVal objectData ^. flens (Proxy @"foo")
316 set (flens (Proxy @"foo")) newFooVal objectData ^. flens (Proxy @"foo")
317− == newFooVal
317+ `shouldBe` newFooVal
318
318
319main :: IO ()
319main :: IO ()
320main = hspec spec
320main = hspec spec