1{-# LANGUAGE OverloadedStrings #-}
5import Control.Applicative
7import Data.HashMap.Strict (HashMap)
8import qualified Data.HashMap.Strict as H
10import Data.Text (Text)
11import qualified Data.Text as T
12import qualified Data.Vector as V
14import qualified Data.JsonSchema as JS
16-- Data.JsonSchema is designed to be imported unqualified.
17-- We're just being explicit here for example purposes.
24--------------------------------------------------
25-- * Example Schema and Data
26--------------------------------------------------
28schemaData :: JS.RawSchema
29schemaData = JS.RawSchema
31 -- ^ If your schema has relative references to other schemas
32 -- then you'll need to include this.
33 , JS._rsData = schemaJSON
36 schemaJSON :: HashMap Text Value
37 schemaJSON = H.singleton "uniqueItems" (Bool True)
40badData = Array $ V.fromList ["foo", "foo"]
42--------------------------------------------------
44--------------------------------------------------
46-- | Since our schema contains no references to other schemas,
47-- we can skip the fetchGraph step.
50 schema <- compileSchema (JS.SchemaGraph schemaData H.empty) schemaData
51 checkResults (JS.validate schema badData)
53--------------------------------------------------
55--------------------------------------------------
57-- | For a schema that might contain references to other schemas.
60 let currentSchemaCache = H.empty
61 schemaGraph <- fetchGraph currentSchemaCache schemaData
63 -- We aren't going to use it for anything this time, but if we were caching
64 -- remote schemas this would be our new cache.
66 -- let newSchemaCache = _cachedSchemas schemaData <> currentSchemaGraph
68 schema <- compileSchema schemaGraph schemaData
69 checkResults (JS.validate schema badData)
72 fetchGraph :: HashMap Text (HashMap Text Value) -> JS.RawSchema -> IO JS.SchemaGraph
73 fetchGraph graph rs = do
74 eitherGraph <- JS.fetchReferencedSchemas JS.draft4 graph rs
76 Left e -> error $ "Failed to fetch graph with error: " <> T.unpack e
79--------------------------------------------------
81--------------------------------------------------
83compileSchema :: JS.SchemaGraph -> JS.RawSchema -> IO (JS.Schema JS.Draft4Failure)
84compileSchema graph rs =
85 case JS.compileDraft4 graph rs of
86 Left failure -> error $ "Not a valid schema: " <> show failure
87 Right schema -> return schema
89checkResults :: [JS.ValidationFailure JS.Draft4Failure] -> IO ()
90checkResults [] = error "OHNO we validated bad data!"
91checkResults [JS.ValidationFailure JS.UniqueItems (JS.FailureInfo x y)] =
94 , "Success. We got a UniqueItems error as expected."
95 , "Here's the relevant part of the bad JSON document:"
99 , "And here's the content of the validator that caught it:"
103checkResults x = error $ "OHNO we got a different failure than we expected: " <> show x
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list