git.iko.soy/typeable/hjsonschema
on master help sign in
Add convenience function.
commitd19367a10a49c862b85fac1bf147c631d7490744 authorIan Grant Jeffries <ian@housejeffries.com> dateWed 25 Mar 2015, 21:11 (12 years ago) parent163b87c
3 files changed+25−9
README.md+8−8 hjsonschema.cabal+1−1 src/Data/JsonSchema.hs+16
README.md+8−8
@@ -19,14 +19,14 @@ import qualified Data.JsonSchema as JS
1919import qualified Data.Vector as V
2020
2121main :: IO ()
22−main =
23− case V.length (JS.isValidSchema rawSchema) of
24− 0 -> do
25− eitherGraph <- JS.fetchRefs JS.draft4 rawSchema H.empty
26− case eitherGraph of
27− Left e -> print e
28− Right g -> print $ JS.validate (JS.compile JS.draft4 g $ rawSchema) invalidData
29− _ -> putStrLn "The schema you tried to use doesn't follow the draft 4 layout."
22+main = do
23+ eitherGraph <- JS.fetchRefs JS.draft4 rawSchema H.empty
24+ case eitherGraph of
25+ Left e -> print e
26+ Right g ->
27+ case JS.compileDraft4 g rawSchema of
28+ Left e2 -> print e2
29+ Right a -> print $ JS.validate a invalidData
3030
3131rawSchema :: JS.RawSchema
3232rawSchema = JS.RawSchema
hjsonschema.cabal+1−1
@@ -1,5 +1,5 @@
11name: hjsonschema
2−version: 0.5.1.2
2+version: 0.5.2.0
33synopsis: JSON Schema Draft 4 library
44homepage: https://github.com/seagreen/hjsonschema
55license: MIT
src/Data/JsonSchema.hs+16
@@ -60,6 +60,10 @@ draft4 = Spec $ H.fromList
6060--
6161-- This is just a convenience function built by preloading 'validate'
6262-- with the spec schema that describes valid Draft 4 schemas.
63+--
64+-- NOTE: It's not actually required to run 'isValidSchema' on
65+-- prospective draft 4 schemas at all. However, it's a good way to
66+-- catch unintentional mistakes in schema documents.
6367isValidSchema :: RawSchema -> Vector ValErr
6468isValidSchema r =
6569 case decode . fromStrict $ $(embedFile "draft4.json") of
@@ -70,6 +74,18 @@ isValidSchema r =
7074 (compile draft4 H.empty draft4Schema)
7175 (Object . _rsObject $ r)
7276
77+-- | Check that a 'RawSchema' conforms to the JSON Schema Draft 4
78+-- master schema document. Compile it if it does.
79+--
80+-- This is just a convenience function built by combining
81+-- 'isValidSchema' and 'compile'.
82+compileDraft4 :: Graph -> RawSchema -> Either (Vector ValErr) Schema
83+compileDraft4 g r =
84+ let es = isValidSchema r
85+ in case V.length es of
86+ 0 -> Right $ compile draft4 g r
87+ _ -> Left es
88+
7389--------------------------------------------------
7490-- * Graph Builder
7591--------------------------------------------------