This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Simple.hs
37 lines (31 loc) · 1.53 KB
/
Simple.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Simple where
import Protolude
import Data.Aeson (Value(..), decodeStrict, toJSON)
import qualified Data.ByteString as BS
import qualified Data.List.NonEmpty as NE
import Data.Maybe (fromMaybe)
import qualified JSONSchema.Draft4 as D4
badData :: Value
badData = toJSON [True, True]
example :: IO ()
example = do
bts <- BS.readFile "./examples/json/unique.json"
let schema = fromMaybe (panic "Invalid schema JSON.") (decodeStrict bts)
schemaWithURI = D4.SchemaWithURI
schema
Nothing -- This would be the URI of the schema
-- if it had one. It's used if the schema
-- has relative references to other
-- schemas.
-- Fetch any referenced schemas over HTTP, check that our original schema
-- itself is valid, and validate the data.
res <- D4.fetchHTTPAndValidate schemaWithURI badData
case res of
Right () -> panic "We validated bad data."
Left (D4.HVRequest _) -> panic ("Error fetching a referenced schema"
<> " (either during IO or parsing).")
Left (D4.HVSchema _) -> panic "Our 'schema' itself was invalid."
Left (D4.HVData (D4.Invalid _ _ failures)) ->
case NE.toList failures of
[D4.FailureUniqueItems _] -> pure () -- Success.
_ -> panic "We got a different failure than expected."