git.iko.soy/typeable/schematic
on union-mashup help sign in
typeable/schematic/test/JsonSchemaSpec.hs
43 lines 1.1 kB history raw
1{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE TypeApplications #-}
5{-# LANGUAGE OverloadedLists #-}
6{-# LANGUAGE OverloadedStrings #-}
8module JsonSchemaSpec (spec, main) where
10import Data.Aeson as J
11import Data.Maybe
12import Data.Proxy
13import Data.Schematic
14import Data.Vinyl
15import JSONSchema.Draft4 as D4
16import Test.Hspec
19type ArraySchema = 'SchemaArray '[ 'AEq 1] ('SchemaNumber '[ 'NGt 10])
21type ArrayField = '("foo", ArraySchema)
23type FieldsSchema =
24 '[ ArrayField, '("bar", 'SchemaOptional ('SchemaText '[ 'TEnum '["foo", "bar"]]))]
26type SchemaExample = 'SchemaObject FieldsSchema
28exampleData :: JsonRepr SchemaExample
29exampleData = withRepr @SchemaExample $
30 field @"foo" [ReprNumber 13]
31 :& field @"bar" (pure (ReprText "foo"))
32 :& RNil
34spec :: Spec
35spec = do
36 it "validates simple schema" $ do
37 let schema = D4.SchemaWithURI (fromJust $ toJsonSchema (Proxy @SchemaExample)) Nothing
38 fetchHTTPAndValidate schema (toJSON exampleData) >>= \case
39 Left _ -> fail "failed to validate test example"
40 Right _ -> pure ()
42main :: IO ()
43main = hspec spec