@hackage hjsonschema0.6.0.2

JSON Schema Draft 4 library

Intro

An implementation of JSON Schema Draft 4 in haskell.

Status

Still in development. Lacks solid code to handle changing resolution scope.

Example

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Data.Aeson
import qualified Data.HashMap.Strict as H
import qualified Data.JsonSchema as JS
import qualified Data.Vector as V

main :: IO ()
main = do
  eitherGraph <- JS.fetchRefs JS.draft4 rawSchema H.empty
  case eitherGraph of
    Left e      -> print e
    Right graph ->
      case JS.compileDraft4 graph rawSchema of
        Left e2      -> print e2
        Right schema -> print $ JS.validate schema invalidData

rawSchema :: JS.RawSchema
rawSchema = JS.RawSchema
  { JS._rsURI = ""
  , JS._rsObject = H.singleton "uniqueItems" (Bool True) -- Schema JSON goes here.
  }

invalidData :: Value
invalidData = Array (V.fromList ["foo", "foo"])

Output:

Left (fromList ["Val error against uniqueItems True for: Array (fromList [String \"foo\",String \"foo\"])"])

Install Tests

git submodule update --init

Notes

  • This uses the regexpr regular expression library for the "pattern" validator. I have no idea if this is compatible with the ECMA 262 regex dialect, which the spec requires.

  • draft4.json is from commit # cc8ec81ce0abe2385ebd6c2a6f2d6deb646f874a here.

Credits

Thanks to Julian Berman for the fantastic test suite.

Also thanks to Tim Baumann for his aeson-schema library. Hjsonschema's test code and its implementation of Graph both come from Aeson-Schema.