git.iko.soy/typeable/hjsonschema
on 0.6.0.0 help sign in
61 lines 1.8 kB history raw
1# Intro
3An implementation of [JSON Schema](http://json-schema.org/) Draft 4 in haskell.
5# Status
7Still in development. Lacks solid code to handle changing resolution scope.
9# Example
11```haskell
12{-# LANGUAGE OverloadedStrings #-}
14module Main where
16import Data.Aeson
17import qualified Data.HashMap.Strict as H
18import qualified Data.JsonSchema as JS
19import qualified Data.Vector as V
21main :: IO ()
22main = do
23 eitherGraph <- JS.fetchRefs JS.draft4 rawSchema H.empty
24 case eitherGraph of
25 Left e -> print e
26 Right graph ->
27 case JS.compileDraft4 graph rawSchema of
28 Left e2 -> print e2
29 Right schema -> print $ JS.validate schema invalidData
31rawSchema :: JS.RawSchema
32rawSchema = JS.RawSchema
33 { JS._rsURI = ""
34 , JS._rsObject = H.singleton "uniqueItems" (Bool True) -- Schema JSON goes here.
35 }
37invalidData :: Value
38invalidData = Array (V.fromList ["foo", "foo"])
39```
41Output:
42```
43fromList ["Val error against uniqueItems True for: Array (fromList [String \"foo\",String \"foo\"])"]
44```
47# Install Tests
49 git submodule update --init
51# Notes
53+ This uses the [regexpr](https://hackage.haskell.org/package/regexpr-0.5.4) regular expression library for the "pattern" validator. I have no idea if this is compatible with the ECMA 262 regex dialect, which the [spec](http://json-schema.org/latest/json-schema-validation.html#anchor33) requires.
55+ `draft4.json` is from commit # cc8ec81ce0abe2385ebd6c2a6f2d6deb646f874a [here](https://github.com/json-schema/json-schema).
57# Credits
59Thanks to [Julian Berman](https://github.com/Julian) for the fantastic test suite.
61Also thanks to Tim Baumann for his [aeson-schema](https://hackage.haskell.org/package/aeson-schema) library. Hjsonschema's test code and its implementation of `Graph` both come from Aeson-Schema.