packages feed

json-schema 0.7.0.2 → 0.7.1.0

raw patch · 6 files changed

+103/−17 lines, 6 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Data.JSON.Schema.Generic: instance [overlap ok] (Constructor c, GJSONSCHEMA f) => GJSONSCHEMA (M1 C c f)
- Data.JSON.Schema.Generic: instance [overlap ok] (GJSONSCHEMA f, GJSONSCHEMA g) => GJSONSCHEMA (f :*: g)
- Data.JSON.Schema.Generic: instance [overlap ok] (GJSONSCHEMA f, GJSONSCHEMA g) => GJSONSCHEMA (f :+: g)
- Data.JSON.Schema.Generic: instance [overlap ok] (Selector c, GJSONSCHEMA f) => GJSONSCHEMA (M1 S c f)
- Data.JSON.Schema.Generic: instance [overlap ok] (Selector c, JSONSchema a) => GJSONSCHEMA (M1 S c (K1 i (Maybe a)))
- Data.JSON.Schema.Generic: instance [overlap ok] GJSONSCHEMA (K1 i String)
- Data.JSON.Schema.Generic: instance [overlap ok] GJSONSCHEMA U1
- Data.JSON.Schema.Generic: instance [overlap ok] GJSONSCHEMA f => GJSONSCHEMA (M1 D c f)
- Data.JSON.Schema.Generic: instance [overlap ok] JSONSchema c => GJSONSCHEMA (K1 i c)
- Data.JSON.Schema.Generic: instance [overlap ok] Selector c => GJSONSCHEMA (M1 S c (K1 i (Maybe String)))
+ Data.JSON.Schema.Generic: class GJSONSchema f
+ Data.JSON.Schema.Generic: instance [overlap ok] (Constructor c, GJSONSchema f) => GJSONSchema (M1 C c f)
+ Data.JSON.Schema.Generic: instance [overlap ok] (GJSONSchema f, GJSONSchema g) => GJSONSchema (f :*: g)
+ Data.JSON.Schema.Generic: instance [overlap ok] (GJSONSchema f, GJSONSchema g) => GJSONSchema (f :+: g)
+ Data.JSON.Schema.Generic: instance [overlap ok] (Selector c, GJSONSchema f) => GJSONSchema (M1 S c f)
+ Data.JSON.Schema.Generic: instance [overlap ok] (Selector c, JSONSchema a) => GJSONSchema (M1 S c (K1 i (Maybe a)))
+ Data.JSON.Schema.Generic: instance [overlap ok] GJSONSchema (K1 i String)
+ Data.JSON.Schema.Generic: instance [overlap ok] GJSONSchema U1
+ Data.JSON.Schema.Generic: instance [overlap ok] GJSONSchema f => GJSONSchema (M1 D c f)
+ Data.JSON.Schema.Generic: instance [overlap ok] JSONSchema c => GJSONSchema (K1 i c)
+ Data.JSON.Schema.Generic: instance [overlap ok] Selector c => GJSONSchema (M1 S c (K1 i (Maybe String)))
- Data.JSON.Schema.Generic: gSchema :: (Generic a, GJSONSCHEMA (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Proxy a -> Schema
+ Data.JSON.Schema.Generic: gSchema :: (Generic a, GJSONSchema (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Proxy a -> Schema
- Data.JSON.Schema.Generic: gSchemaWithSettings :: (Generic a, GJSONSCHEMA (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Settings -> Proxy a -> Schema
+ Data.JSON.Schema.Generic: gSchemaWithSettings :: (Generic a, GJSONSchema (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Settings -> Proxy a -> Schema

Files

CHANGELOG.md view
@@ -1,5 +1,9 @@ # Changelog +### 0.7.1.0++* Export `GJSONSchema` type to allow clients to write type signatures for `gSchema` and `gSchemaWithSettings`.+ #### 0.7.0.2  * Allow time 1.5.*
README.md view
@@ -1,3 +1,30 @@ # json-schema  [![Build Status](https://travis-ci.org/silkapp/json-schema.svg?branch=master)](https://travis-ci.org/silkapp/json-schema)++`json-schema` is like XML schemas, but for JSON.++The package provides++* An AST type `Schema` defining the structure of a JSON object.+* A type class `JSONSchema` to define a schema for a type.+* Combinators for defining schemas.+* A module for validating Aeson values against schemas.+* Built-in instances matching [aeson's](http://hackage.haskell.org/package/aeson).+* A Generics module matching aeson instances generated by [generics-aeson](http://hackage.haskell.org/package/generic-aeson)++If you use another library for generating JSON instances (such as aeson's built-in generics/template haskell) or write aeson instances by hand you will not get matching JSONSchema instances if you use json-schema's generics module.++This package assumes that your `ToJSON` and `FromJSON` instances match.++## Installation++This package is available on [hackage](http://hackage.haskell.org/package/json-schema)++```shell+$ cabal install json-schema+```++## Examples++see [the examples folder](https://github.com/silkapp/json-schema/blob/master/examples/Example.hs) or [The test suite](https://github.com/silkapp/json-schema/tree/master/tests).
+ examples/Example.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE+    DeriveGeneric+  , OverloadedStrings+  #-}+module Example where++import Control.Applicative+import Control.Monad+import Data.Aeson hiding (Object)+import Data.Text (Text)+import GHC.Generics+import Generics.Generic.Aeson+import qualified Data.Aeson as A++import Data.JSON.Schema++-- Manually defined instances++data User = User { name :: Text, age :: Int }++instance ToJSON User where+  toJSON u = object [ "name" .= name u+                    , "age"  .= age u+                    ]++instance FromJSON User where+  parseJSON (A.Object u) = User <$> u .: "name" <*> u .: "age"+  parseJSON _            = mzero++instance JSONSchema User where+  schema pu = Object [ Field { key = "name", required = True, content = schema $ fmap name pu }+                     , Field { key = "age" , required = True, content = schema $ fmap age  pu }+                     ]++-- Using generic-aeson++data User2 = User2 { name2 :: Text, age2 :: Text }+  deriving Generic++instance ToJSON User2 where+  toJSON = gtoJson++instance FromJSON User2 where+  parseJSON = gparseJson++instance JSONSchema User2 where+  schema = gSchema
json-schema.cabal view
@@ -1,7 +1,12 @@ name:                json-schema-version:             0.7.0.2+version:             0.7.1.0 synopsis:            Types and type classes for defining JSON schemas. description:         Types and type classes for defining JSON schemas.+                     .+                     /Documentation/+                     .+                     See <https://github.com/silkapp/json-schema/blob/master/README.md>+                     . license:             BSD3 license-file:        LICENSE author:              Silk@@ -44,10 +49,11 @@  test-suite json-schema-generic-aeson-tests   ghc-options:       -Wall-  hs-source-dirs:    tests+  hs-source-dirs:    tests, examples   main-is:           Main.hs   type:              exitcode-stdio-1.0   other-modules:+    Example     Test.Util     Test.Validate   build-depends:
src/Data/JSON/Schema/Generic.hs view
@@ -12,7 +12,8 @@ -- JSON generated by type 'generic-aeson' package. See that package -- for documentation on the format and examples of it. module Data.JSON.Schema.Generic-  ( gSchema+  ( GJSONSchema+  , gSchema   , gSchemaWithSettings   ) where @@ -29,31 +30,31 @@ import qualified Data.Text        as T  -- | Derive a JSON schema for types with an instance of 'Generic'.-gSchema :: (Generic a, GJSONSCHEMA (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Proxy a -> Schema+gSchema :: (Generic a, GJSONSchema (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Proxy a -> Schema gSchema = gSchemaWithSettings defaultSettings -gSchemaWithSettings :: (Generic a, GJSONSCHEMA (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Settings -> Proxy a -> Schema+gSchemaWithSettings :: (Generic a, GJSONSchema (Rep a), ConNames (Rep a), GIsEnum (Rep a)) => Settings -> Proxy a -> Schema gSchemaWithSettings set p = gSchema' set (isEnum p) ((map T.pack . conNames . pv) p) (fmap from p) -class GJSONSCHEMA f where+class GJSONSchema f where   gSchema' :: Settings -> Bool -> [Text] -> Proxy (f a) -> Schema  -- Recursive positions disabled for now, it causes infintite data structures. This is a problem to be solved! {--instance GJSONSCHEMA I where+instance GJSONSchema I where   gSchema' _ f = f . fmap unI -} -instance JSONSchema c => GJSONSCHEMA (K1 i c) where+instance JSONSchema c => GJSONSchema (K1 i c) where   gSchema' _ _ _ = schema . fmap unK1 -instance GJSONSCHEMA (K1 i String) where+instance GJSONSchema (K1 i String) where   gSchema' _ _ _ _ = Value unboundedLength -instance GJSONSCHEMA U1 where+instance GJSONSchema U1 where   gSchema' _ _ _ _ = empty -instance (GJSONSCHEMA f, GJSONSCHEMA g) => GJSONSCHEMA (f :+: g) where+instance (GJSONSchema f, GJSONSchema g) => GJSONSchema (f :+: g) where   gSchema' set enm names p =         gSchema' set enm names (gL <$> p)     <|> gSchema' set enm names (gR <$> p)@@ -63,10 +64,10 @@       gR :: (f :+: g) r -> g r       gR _ = undefined -instance (GJSONSCHEMA f, GJSONSCHEMA g) => GJSONSCHEMA (f :*: g) where+instance (GJSONSchema f, GJSONSchema g) => GJSONSchema (f :*: g) where   gSchema' set enm names p = gSchema' set enm names (gFst <$> p) `merge` gSchema' set enm names (gSnd <$> p) -instance (Constructor c, GJSONSCHEMA f) => GJSONSCHEMA (M1 C c f) where+instance (Constructor c, GJSONSchema f) => GJSONSchema (M1 C c f) where   gSchema' set True _ = toConstant set . conNameT set . pv   gSchema' set enm names = wrap . gSchema' set enm names . fmap unM1     where@@ -74,11 +75,11 @@              then field (conNameT set (undefined :: M1 C c f p)) True              else id -instance GJSONSCHEMA f => GJSONSCHEMA (M1 D c f) where+instance GJSONSchema f => GJSONSchema (M1 D c f) where   gSchema' set True names p | multipleCons names = const (Choice . fmap (toConstant set) $ names) $ p   gSchema' set enm names p = gSchema' set enm names . fmap unM1 $ p -instance (Selector c, JSONSchema a) => GJSONSCHEMA (M1 S c (K1 i (Maybe a))) where+instance (Selector c, JSONSchema a) => GJSONSchema (M1 S c (K1 i (Maybe a))) where   gSchema' set _ _ =     case selNameT set (undefined :: M1 S c f p) of       Nothing -> nullable      . maybeElemSchema -- C (Maybe a)        => [a]       or [null]@@ -88,13 +89,13 @@       maybeElemSchema = s          where s = schema . fmap (fromJust . unK1 . unM1) -instance Selector c => GJSONSCHEMA (M1 S c (K1 i (Maybe String))) where+instance Selector c => GJSONSchema (M1 S c (K1 i (Maybe String))) where   gSchema' set _ _ _ =     case selNameT set (undefined :: M1 S c f p) of       Nothing -> nullable value       Just n  -> field n False value -instance (Selector c, GJSONSCHEMA f) => GJSONSCHEMA (M1 S c f) where+instance (Selector c, GJSONSchema f) => GJSONSchema (M1 S c f) where   gSchema' set enm names = wrap . gSchema' set enm names . fmap unM1     where       wrap = maybe id (\s -> field s True) $ selNameT set (undefined :: M1 S c f p)
tests/Main.hs view
@@ -19,6 +19,7 @@  import Data.JSON.Schema import Data.JSON.Schema.Combinators+import Example () import Test.Util import qualified Data.JSON.Schema as S import qualified Test.Validate as Validate