git.iko.soy/typeable/hjsonschema
on 96ffacc help sign in
34 files changed+294−251
since splitsnapshots
unifiedsplit
.travis.yml+5−5 README.md+50−8 changelog.md−15 examples/AlternateSchema.hs+18−13 examples/Simple.hs+5−4 examples/TwoStep.hs+4−3 hjsonschema.cabal+26−24 logo.jpg run-stack-haddock+1 shell.nix−1 src/Import.hs+7−7 src/JSONSchema/Draft4.hs+15−12 src/JSONSchema/Draft4/Failure.hs+2−1 src/JSONSchema/Draft4/Schema.hs+7−6 src/JSONSchema/Draft4/Spec.hs+13−9 src/JSONSchema/Fetch.hs+15−16 src/JSONSchema/Types.hs+1 src/JSONSchema/Validator/Draft4.hs+8−7 src/JSONSchema/Validator/Draft4/Any.hs+24−46 src/JSONSchema/Validator/Draft4/Array.hs+6−7 src/JSONSchema/Validator/Draft4/Number.hs+2−1 src/JSONSchema/Validator/Draft4/Object.hs+6−5 src/JSONSchema/Validator/Draft4/Object/Properties.hs+6−5 src/JSONSchema/Validator/Draft4/String.hs+3−2 src/JSONSchema/Validator/Reference.hs+7−7 src/JSONSchema/Validator/Types.hs+1 src/JSONSchema/Validator/Utils.hs+8−7 stack.yaml+7−2 test/Local.hs+10−7 test/Local/Failure.hs+4−3 test/Local/Reference.hs+1 test/Local/Validation.hs+5−4 test/Remote.hs+7−6 test/Shared.hs+20−18
.travis.yml+5−5
@@ -26,15 +26,15 @@ before_cache:
2626
2727matrix:
2828 include:
29+ - compiler: "ghc-7.10.3"
30+ # env: TEST=--disable-tests BENCH=--disable-benchmarks
31+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-7.10.3], sources: [hvr-ghc]}}
2932 - compiler: "ghc-8.0.2"
3033 # env: TEST=--disable-tests BENCH=--disable-benchmarks
3134 addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.0.2], sources: [hvr-ghc]}}
32− - compiler: "ghc-8.2.2"
33− # env: TEST=--disable-tests BENCH=--disable-benchmarks
34− addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.2.2], sources: [hvr-ghc]}}
35− - compiler: "ghc-8.4.1"
35+ - compiler: "ghc-8.2.1"
3636 # env: TEST=--disable-tests BENCH=--disable-benchmarks
37− addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.4.1], sources: [hvr-ghc]}}
37+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.2.1], sources: [hvr-ghc]}}
3838
3939before_install:
4040 - HC=${CC}
README.md+50−8
@@ -1,25 +1,67 @@
1−# DEPRECATION NOTICE
1+![hjsonschema logo](./logo.jpg)
22
3−`hjsonschema` was an attempt to build a very modular JSON Schema library. Validators have [a concrete type](src/JSONSchema/Validator/Types.hs) and can be mixed and matched into new [Specs](src/JSONSchema/Types.hs).
3+A Haskell implementation of the most commonly used [JSON Schema](http://json-schema.org/) specification ([Draft 4](https://github.com/json-schema-org/json-schema-spec/wiki/Specification-Links#draft-4)).
44
5−However this flexibility came at the price of complicating the code. I don't think it was the right tradeoff, especially since situations where you'd want to change what validators make up a `Spec` at runtime seem rare.
5+[Hackage](https://hackage.haskell.org/package/hjsonschema) / [GitHub](https://github.com/seagreen/hjsonschema) / [Travis CI](https://travis-ci.org/seagreen/hjsonschema)
66
7−Also, there are many parts of JSON Schema that `hjsonschema` doesn't implement properly (as you can see from the issue tracker). I'm hoping that a new JSON Schema library will come along that handles these correctly. In the meantime I'm happy to merge working fixes into here.
7+# Notes
88
9−# Links
9++ As of 2017 [json-schema-org](https://github.com/json-schema-org/json-schema-spec) has begun releasing new drafts of the standard. Once this work stabilizes `hjsonschema` will add coverage for the latest draft.
1010
11−[Hackage](https://hackage.haskell.org/package/hjsonschema) / [GitHub](https://github.com/seagreen/hjsonschema) / [Travis CI](https://travis-ci.org/seagreen/hjsonschema)
11++ Requires [pcre](http://www.pcre.org/) (`pkgs.pcre` in Nixpkgs).
12+
13++ Schemas with circular references can cause infinite loops. hjsonschema does loop detection but it may not be solid yet -- please open an issue if you find a situation where it fails.
1214
1315# Example
1416
1517See [here](https://github.com/seagreen/hjsonschema/blob/master/examples/Simple.hs).
1618
17−# System dependencies
19+# Tests
1820
19−+ Requires [pcre](http://www.pcre.org/) (`pkgs.pcre` in Nixpkgs).
21+Run all tests:
22+
23+`stack test`
24+
25+Run only local tests:
26+
27+`stack test hjsonschema:local`
28+
29+Run remote tests (makes GETs to json-schema.org, also temporarily starts an HTTP server on port 1234):
30+
31+`stack test hjsonschema:remote`
32+
33+# Details
34+
35+## Goals
36+
37++ Be a correct and fast implementation of the spec.
38+
39++ Be a useful reference for implementers in other languages. Haskell's high level nature, expressive type system and referential transparency suit this purpose well.
40+
41+## Good Parts
42+
43++ Passes all the required tests in the [language agnostic test suite](https://github.com/json-schema/JSON-Schema-Test-Suite). NOTE: due to an issue with the test suite this isn't true at the moment, see [#175](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/175).
44+
45++ Very modular, which should make it easy to support future versions of the specification.
46+
47+## Bad Parts
48+
49++ Uses the [pcre-heavy](https://hackage.haskell.org/package/pcre-heavy) regular expression library for the "pattern" validator. It should use a library based on the ECMA 262 regex dialect, which the [spec](http://json-schema.org/latest/json-schema-validation.html#anchor33) requires.
50+
51++ Currently doesn't support the optional `"format"` validators.
2052
2153## Vendoring
2254
2355+ `JSON-Schema-Test-Suite` is vendored from commit # c1b12bf699f29a04b4286711c6e3bbfba66f21e5 [here](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
2456
2557+ `src/draft4.json` is from commit # c1b12bf699f29a04b4286711c6e3bbfba66f21e5 [here](https://github.com/json-schema/json-schema). The [root ref in remote ref](./JSON-Schema-Test-Suite/tests/draft4/refRemote.json) test has been modified to fix [#175](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/175).
58+
59++ `.travis.yml` was created with `make_travis_yml_2.hs` commit # ea6c7d177a97bfbfb2fdc4deba943d60d2aff199.
60+
61+## Credits
62+
63+[TJ Weigel](http://tjweigel.com/) created the logo.
64+
65+[Tim Baumann](https://github.com/timjb) wrote [aeson-schema](https://hackage.haskell.org/package/aeson-schema), on which hjsonschema's test code and its implementation of `SchemaGraph` were based.
66+
67+[Julian Berman](https://github.com/Julian) maintains the fantastic [language agnostic test suite](https://github.com/json-schema/JSON-Schema-Test-Suite).
changelog.md−15
@@ -1,18 +1,3 @@
1−# 1.8.0
2−
3−+ Add GHC 8.4 support (thanks @4e6 !).
4−+ Drop GHC 7.10 support.
5−+ Fix JSON Pointer resolution error.
6−
7−# 1.8.0
8−
9−+ Allow HTTPS references (thanks @creichert!).
10−+ Use `safe-exceptions` to eliminate the risk of catching asynchronous exceptions.
11−
12−# 1.7.2
13−
14−+ Remove upper bounds.
15−
161# 1.7.1
172
183+ Bump http-types.
examples/AlternateSchema.hs+18−13
@@ -5,23 +5,28 @@
55-- to copy this module instead of the 'JSONSchema.Draft4'. While it's
66-- less convenient to write schemas in Haskell without a record type, you
77-- can get the implementation finished with far fewer lines of code.
8+
89module AlternateSchema where
910
1011import Protolude
1112
12−import Data.Aeson (FromJSON(..), Value(..), decodeStrict)
13−import qualified Data.Aeson as AE
14−import qualified Data.HashMap.Strict as HM
15−import Data.Maybe (fromMaybe)
16−import Data.Profunctor (Profunctor(..))
17−
18−import JSONSchema.Draft4 (ValidatorFailure(..), metaSchemaBytes)
19−import JSONSchema.Fetch (SchemaWithURI(..), URISchemaMap(..))
20−import qualified JSONSchema.Fetch as FE
21−import JSONSchema.Types (Schema(..), Spec(..))
22−import qualified JSONSchema.Types as JT
23−import qualified JSONSchema.Validator.Draft4 as D4
24−import JSONSchema.Validator.Reference (BaseURI(..), Scope(..),
13+import Data.Aeson (FromJSON(..), Value(..),
14+ decodeStrict)
15+import qualified Data.Aeson as AE
16+import qualified Data.HashMap.Strict as HM
17+import Data.Maybe (fromMaybe)
18+import Data.Profunctor (Profunctor (..))
19+
20+import JSONSchema.Draft4 (ValidatorFailure(..),
21+ metaSchemaBytes)
22+import JSONSchema.Fetch (SchemaWithURI(..),
23+ URISchemaMap(..))
24+import qualified JSONSchema.Fetch as FE
25+import JSONSchema.Types (Schema(..), Spec(..))
26+import qualified JSONSchema.Types as JT
27+import qualified JSONSchema.Validator.Draft4 as D4
28+import JSONSchema.Validator.Reference (BaseURI(..),
29+ Scope(..),
2530 updateResolutionScope)
2631
2732--------------------------------------------------
examples/Simple.hs+5−4
@@ -1,13 +1,14 @@
1+
12module Simple where
23
34import Protolude
45
5−import Data.Aeson (Value(..), decodeStrict, toJSON)
6−import qualified Data.ByteString as BS
6+import Data.Aeson (Value(..), decodeStrict, toJSON)
7+import qualified Data.ByteString as BS
78import qualified Data.List.NonEmpty as NE
8−import Data.Maybe (fromMaybe)
9+import Data.Maybe (fromMaybe)
910
10−import qualified JSONSchema.Draft4 as D4
11+import qualified JSONSchema.Draft4 as D4
1112
1213badData :: Value
1314badData = toJSON [True, True]
examples/TwoStep.hs+4−3
@@ -10,14 +10,15 @@
1010-- 'fetchHTTPAndValidate' which fetched references over HTTP (though it
1111-- didn't matter because the last schema didn't actually have any).
1212-- This time we need to get them from the filesystem.
13+
1314module TwoStep where
1415
1516import Protolude
1617
17−import Data.Aeson (Value(..), toJSON)
18−import qualified Data.List.NonEmpty as NE
18+import Data.Aeson (Value (..), toJSON)
19+import qualified Data.List.NonEmpty as NE
1920
20−import qualified JSONSchema.Draft4 as D4
21+import qualified JSONSchema.Draft4 as D4
2122import qualified JSONSchema.Validator.Draft4 as VAL
2223
2324schema :: D4.Schema
hjsonschema.cabal+26−24
@@ -1,5 +1,5 @@
11name: hjsonschema
2−version: 1.9.0
2+version: 1.7.1
33synopsis: JSON Schema library
44homepage: https://github.com/seagreen/hjsonschema
55license: MIT
@@ -10,7 +10,7 @@ category: Data
1010build-type: Simple
1111cabal-version: >=1.10
1212-- Rerun multi-ghc-travis (executable make-travis-yml-2) after changing:
13−Tested-With: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.1
13+Tested-With: GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.1
1414extra-source-files:
1515 changelog.md
1616 JSON-Schema-Test-Suite/remotes/*.json
@@ -34,6 +34,7 @@ library
3434 NoImplicitPrelude
3535 OverloadedStrings
3636 ScopedTypeVariables
37+ TupleSections
3738 if impl(ghc >= 8)
3839 default-extensions: StrictData
3940 ghc-options:
@@ -58,28 +59,27 @@ library
5859 JSONSchema.Validator.Draft4.Object.Properties
5960 , Import
6061 build-depends:
61− base >= 4.7 && < 5
62+ base >= 4.7 && < 4.11
6263 -- 0.11 is for `.:!`:
63− , aeson >= 0.11
64− , bytestring >= 0.10
65− , containers >= 0.5
66− , file-embed >= 0.0.8
67− , filepath >= 1.3
68− , hashable >= 1.2
69− , hjsonpointer >= 1.1
64+ , aeson >= 0.11 && < 1.3
65+ , bytestring >= 0.10 && < 0.11
66+ , containers >= 0.5 && < 0.6
67+ , file-embed >= 0.0.8 && < 0.1
68+ , filepath >= 1.3 && < 1.5
69+ , hashable >= 1.2 && < 1.3
70+ , hjsonpointer >= 1.1 && < 1.4
7071 -- 0.4.30 is for parseUrlThrow:
71− , http-client >= 0.4.30
72− , http-client-tls >= 0.3
73− , http-types >= 0.8
74− , pcre-heavy >= 1.0
75− , profunctors >= 5.0
76− , protolude >= 0.1.10
77− , QuickCheck >= 2.8
78− , safe-exceptions >= 0.1.6
79− , scientific >= 0.3
80− , unordered-containers >= 0.2
81− , text >= 1.1
82− , vector >= 0.10
72+ , http-client >= 0.4.30 && < 0.6
73+ , http-types >= 0.8 && < 0.11
74+ , pcre-heavy >= 1.0 && < 1.1
75+ , profunctors >= 5.0 && < 5.3
76+ , protolude >= 0.1.10 && < 1.2
77+ , QuickCheck >= 2.8 && < 2.11
78+ , scientific >= 0.3 && < 0.4
79+ , semigroups >= 0.18 && < 0.19
80+ , unordered-containers >= 0.2 && < 0.3
81+ , text >= 1.1 && < 1.3
82+ , vector >= 0.10 && < 1.0
8383
8484test-suite local
8585 hs-source-dirs:
@@ -114,14 +114,15 @@ test-suite local
114114 , hjsonschema
115115 , profunctors
116116 , protolude
117+ , semigroups
117118 , text
118119 , QuickCheck
119120 , unordered-containers
120121 , vector
121122
122123 -- directory-1.2.5 required for `listDirectory`:
123− , directory >= 1.2.5
124− , hspec >= 2.2
124+ , directory >= 1.2.5 && < 2.0
125+ , hspec >= 2.2 && < 3.0
125126
126127test-suite remote
127128 hs-source-dirs:
@@ -152,6 +153,7 @@ test-suite remote
152153 , hjsonschema
153154 , profunctors
154155 , protolude
156+ , semigroups
155157 , text
156158 , unordered-containers
157159 , vector
logo.jpgnew file

Binary file not shown.

run-stack-haddocknew file+1
@@ -0,0 +1 @@
1+../stack-haddock-upload/stack-haddock-upload hjsonschema # 1.2.0.1
shell.nix−1
@@ -8,7 +8,6 @@ haskell.lib.buildStackProject {
88 pcre
99
1010 git # for `git:` references in stack.yml.
11− ncurses # for intero
1211 pkgconfig
1312 zlib
1413 ];
src/Import.hs+7−7
@@ -1,16 +1,16 @@
11
22module Import (module Export, fromJSONEither) where
33
4−import Protolude as Export
4+import Protolude as Export
55
6−import Data.Aeson as Export
6+import Data.Aeson as Export
77import Data.HashMap.Strict as Export (HashMap)
8−import Data.List.NonEmpty as Export (NonEmpty)
9−import Data.Vector as Export (Vector)
10−import Test.QuickCheck as Export hiding (Failure, Result, Success,
11− (.&.))
8+import Data.List.NonEmpty as Export (NonEmpty)
9+import Data.Vector as Export (Vector)
10+import Test.QuickCheck as Export hiding ((.&.), Failure,
11+ Result, Success)
1212
13−import qualified Data.Text as T
13+import qualified Data.Text as T
1414
1515fromJSONEither :: FromJSON a => Value -> Either Text a
1616fromJSONEither a =
src/JSONSchema/Draft4.hs+15−12
@@ -37,19 +37,22 @@ module JSONSchema.Draft4
3737
3838import Import
3939
40−import qualified Data.ByteString as BS
41−import Data.FileEmbed (embedFile, makeRelativeToProject)
42−import qualified Data.HashMap.Strict as HM
43−import qualified Data.List.NonEmpty as NE
44−import Data.Maybe (fromMaybe)
45−
46−import JSONSchema.Draft4.Failure (Invalid(..), SchemaInvalid(..),
40+import qualified Data.ByteString as BS
41+import Data.FileEmbed (embedFile,
42+ makeRelativeToProject)
43+import qualified Data.HashMap.Strict as HM
44+import qualified Data.List.NonEmpty as NE
45+import Data.Maybe (fromMaybe)
46+
47+import JSONSchema.Draft4.Failure (Invalid(..),
48+ SchemaInvalid(..),
4749 ValidatorFailure(..))
48−import JSONSchema.Draft4.Schema (Schema)
49−import qualified JSONSchema.Draft4.Schema as SC
50−import qualified JSONSchema.Draft4.Spec as Spec
51−import JSONSchema.Fetch (SchemaWithURI(..), URISchemaMap(..))
52−import qualified JSONSchema.Fetch as FE
50+import JSONSchema.Draft4.Schema (Schema)
51+import qualified JSONSchema.Draft4.Schema as SC
52+import qualified JSONSchema.Draft4.Spec as Spec
53+import JSONSchema.Fetch (SchemaWithURI(..),
54+ URISchemaMap(..))
55+import qualified JSONSchema.Fetch as FE
5356
5457data HTTPValidationFailure
5558 = HVRequest FE.HTTPFailure
src/JSONSchema/Draft4/Failure.hs+2−1
@@ -1,8 +1,9 @@
1+
12module JSONSchema.Draft4.Failure where
23
34import Import
45
5−import JSONSchema.Draft4.Schema (Schema)
6+import JSONSchema.Draft4.Schema (Schema)
67import qualified JSONSchema.Validator.Draft4 as VAL
78
89-- | Used to report an entire instance being invalidated, as opposed
src/JSONSchema/Draft4/Schema.hs+7−6
@@ -1,13 +1,14 @@
1+
12module JSONSchema.Draft4.Schema where
23
3−import Import hiding (mapMaybe)
4+import Import hiding (mapMaybe)
45
5−import qualified Data.HashMap.Strict as HM
6−import Data.List.NonEmpty (NonEmpty)
7−import Data.Maybe (fromJust, isJust)
6+import qualified Data.HashMap.Strict as HM
7+import Data.List.NonEmpty (NonEmpty)
8+import Data.Maybe (fromJust, isJust)
89import Data.Scientific
9−import qualified Data.Set as Set
10−import qualified Data.Text as T
10+import qualified Data.Set as Set
11+import qualified Data.Text as T
1112
1213import qualified JSONSchema.Validator.Draft4 as D4
1314import JSONSchema.Validator.Utils
src/JSONSchema/Draft4/Spec.hs+13−9
@@ -1,18 +1,22 @@
1+
12module JSONSchema.Draft4.Spec where
23
34import Import
45
5−import Data.Maybe (fromMaybe)
6−import Data.Profunctor (Profunctor(..))
6+import Data.Maybe (fromMaybe)
7+import Data.Profunctor (Profunctor(..))
78
89import JSONSchema.Draft4.Failure
9−import JSONSchema.Draft4.Schema (Schema(..), emptySchema)
10−import JSONSchema.Fetch (SchemaWithURI(..), URISchemaMap(..))
11−import qualified JSONSchema.Fetch as FE
12−import JSONSchema.Types (Spec(..))
13−import qualified JSONSchema.Types as JT
10+import JSONSchema.Draft4.Schema (Schema(..),
11+ emptySchema)
12+import JSONSchema.Fetch (SchemaWithURI(..),
13+ URISchemaMap(..))
14+import qualified JSONSchema.Fetch as FE
15+import JSONSchema.Types (Spec(..))
16+import qualified JSONSchema.Types as JT
1417import JSONSchema.Validator.Draft4
15−import JSONSchema.Validator.Reference (BaseURI(..), Scope(..),
18+import JSONSchema.Validator.Reference (BaseURI(..),
19+ Scope(..),
1620 updateResolutionScope)
1721
1822-- | An implementation of 'JT.embedded'.
@@ -39,7 +43,7 @@ specValidate schemaMap sw =
3943 (_schemaId (_swSchema sw))
4044 }
4145
42−validateSubschema
46+validateSubschema
4347 :: URISchemaMap Schema
4448 -> VisitedSchemas
4549 -> Scope Schema
src/JSONSchema/Fetch.hs+15−16
@@ -1,18 +1,18 @@
1+
12module JSONSchema.Fetch where
23
34import Import
45
5−import qualified Control.Exception.Safe as Safe
6−import Control.Monad (foldM)
7−import qualified Data.ByteString as BS
8−import qualified Data.ByteString.Lazy as LBS
9−import qualified Data.HashMap.Strict as HM
10−import qualified Data.Semigroup as S
11−import qualified Data.Text as T
12−import qualified Network.HTTP.Client as NC
13−import qualified Network.HTTP.Client.TLS as NCTLS
14−
15−import JSONSchema.Validator.Reference (BaseURI(..), resolveReference,
6+import Control.Exception (IOException, catch)
7+import Control.Monad (foldM)
8+import qualified Data.ByteString as BS
9+import qualified Data.ByteString.Lazy as LBS
10+import qualified Data.HashMap.Strict as HM
11+import qualified Data.Text as T
12+import qualified Network.HTTP.Client as NC
13+
14+import JSONSchema.Validator.Reference (BaseURI(..),
15+ resolveReference,
1616 updateResolutionScope)
1717
1818--------------------------------------------------
@@ -30,7 +30,7 @@ data FetchInfo schema = FetchInfo
3030-- | Keys are URIs (without URI fragments).
3131newtype URISchemaMap schema
3232 = URISchemaMap { _unURISchemaMap :: HashMap Text schema }
33− deriving (Eq, Show, S.Semigroup, Monoid)
33+ deriving (Eq, Show, Monoid)
3434
3535-- | A top-level schema along with its location.
3636data SchemaWithURI schema = SchemaWithURI
@@ -61,9 +61,9 @@ referencesViaHTTP'
6161 -> SchemaWithURI schema
6262 -> IO (Either HTTPFailure (URISchemaMap schema))
6363referencesViaHTTP' info sw = do
64− manager <- NC.newManager NCTLS.tlsManagerSettings
64+ manager <- NC.newManager NC.defaultManagerSettings
6565 let f = referencesMethodAgnostic (getURL manager) info sw
66− Safe.catch (first HTTPParseFailure <$> f) handler
66+ catch (first HTTPParseFailure <$> f) handler
6767 where
6868 getURL :: NC.Manager -> Text -> IO BS.ByteString
6969 getURL man url = do
@@ -89,8 +89,7 @@ referencesViaFilesystem'
8989 => FetchInfo schema
9090 -> SchemaWithURI schema
9191 -> IO (Either FilesystemFailure (URISchemaMap schema))
92−referencesViaFilesystem' info sw =
93− Safe.catch (first FSParseFailure <$> f) handler
92+referencesViaFilesystem' info sw = catch (first FSParseFailure <$> f) handler
9493 where
9594 f :: IO (Either Text (URISchemaMap schema))
9695 f = referencesMethodAgnostic (BS.readFile . T.unpack) info sw
src/JSONSchema/Types.hs+1
@@ -1,3 +1,4 @@
1+
12module JSONSchema.Types where
23
34import Import
src/JSONSchema/Validator/Draft4.hs+8−7
@@ -2,6 +2,7 @@
22--
33-- This is frankly a lot of busywork. It can perhaps be moved into the
44-- validator modules themselves once we're sure this is the right design.
5+
56module JSONSchema.Validator.Draft4
67 ( module JSONSchema.Validator.Draft4
78 , module Export
@@ -9,16 +10,16 @@ module JSONSchema.Validator.Draft4
910
1011import Import
1112
12−import qualified Data.HashMap.Strict as HM
13−import qualified Data.List.NonEmpty as NE
13+import qualified Data.HashMap.Strict as HM
14+import qualified Data.List.NonEmpty as NE
1415
15−import JSONSchema.Validator.Draft4.Any as Export
16−import JSONSchema.Validator.Draft4.Array as Export
16+import JSONSchema.Validator.Draft4.Any as Export
17+import JSONSchema.Validator.Draft4.Array as Export
1718import JSONSchema.Validator.Draft4.Number as Export
1819import JSONSchema.Validator.Draft4.Object as Export
1920import JSONSchema.Validator.Draft4.String as Export
20−import JSONSchema.Validator.Reference (BaseURI(..), Scope(..))
21−import JSONSchema.Validator.Types (Validator(..))
21+import JSONSchema.Validator.Reference (BaseURI(..), Scope(..))
22+import JSONSchema.Validator.Types (Validator(..))
2223
2324-- | For internal use.
2425--
@@ -147,7 +148,7 @@ propertiesRelatedValidator f =
147148 <> HM.elems (fromMaybe mempty (_propPattern a))
148149 <> case _propAdditional a of
149150 Just (AdditionalPropertiesObject b) -> [b]
150− _ -> mempty
151+ _ -> mempty
151152 ))
152153 (\a b -> case fromJSONEither b of
153154 Left _ -> mempty
src/JSONSchema/Validator/Draft4/Any.hs+24−46
@@ -1,24 +1,25 @@
1+
12module JSONSchema.Validator.Draft4.Any where
23
3−import Import hiding ((<>))
4+import Import hiding ((<>))
45
5−import Data.Aeson.TH (constructorTagModifier)
6−import Data.Char (toLower)
7−import qualified Data.HashMap.Strict as HM
8−import Data.List.NonEmpty (NonEmpty((:|)))
9−import qualified Data.List.NonEmpty as NE
10−import qualified Data.Scientific as SCI
6+import Data.Aeson.TH (constructorTagModifier)
7+import Data.Char (toLower)
8+import Data.List.NonEmpty (NonEmpty((:|)))
9+import qualified Data.List.NonEmpty as NE
10+import qualified Data.Scientific as SCI
1111import Data.Semigroup
12−import Data.Set (Set)
13−import qualified Data.Set as Set
14−import Data.Text.Encoding.Error (UnicodeException)
15−import qualified JSONPointer as JP
16−import Network.HTTP.Types.URI (urlDecode)
17−
18−import JSONSchema.Validator.Reference (BaseURI(..), Scope(..),
12+import Data.Set (Set)
13+import qualified Data.Set as Set
14+import Data.Text.Encoding.Error (UnicodeException)
15+import qualified JSONPointer as JP
16+import Network.HTTP.Types.URI (urlDecode)
17+
18+import qualified JSONSchema.Validator.Utils as UT
19+import JSONSchema.Validator.Reference (BaseURI(..),
20+ Scope(..),
1921 URIAndFragment,
2022 resolveReference)
21−import qualified JSONSchema.Validator.Utils as UT
2223
2324--------------------------------------------------
2425-- * $ref
@@ -134,7 +135,7 @@ data JSONPointerError
134135 deriving (Eq, Show)
135136
136137resolveFragment
137− :: (FromJSON schema, ToJSON schema)
138+ :: forall schema. (FromJSON schema, ToJSON schema)
138139 => (BaseURI -> schema -> BaseURI)
139140 -> Scope schema
140141 -> Text
@@ -159,40 +160,17 @@ resolveFragment updateScope scope fragment = do
159160 -- as property keys that aren't meant to change scope?
160161 -- Perhaps this should be added to the language agnostic
161162 -- test suite as well.
162− --
163− -- In the meantime 'newBaseURIFromFragment' drops all keys
164− -- from JSON objects except "id", which at least prevents
165− -- SubschemaDecodingError in a situation where one of the
166− -- values we step through isn't a valid schema.
167163 go :: (Value, BaseURI)
168164 -> JP.Token
169165 -> Either JSONPointerError (Value, BaseURI)
170− go (lastVal, baseURI) tok = do
166+ go (lastVal, uri) tok = do
171167 v <- first ResolutionError (JP.resolveToken tok lastVal)
172− newBase <- newBaseURIFromFragment updateScope baseURI v
173− Right (v, newBase)
174−
175−-- | Update the 'BaseURI' (the store of the current "id" value)
176−-- after resolving one token of a JSON Pointer and stepping into
177−-- a new 'Value'.
178−newBaseURIFromFragment
179− :: FromJSON schema
180− => (BaseURI -> schema -> BaseURI)
181− -> BaseURI
182− -> Value
183− -> Either JSONPointerError BaseURI
184−newBaseURIFromFragment updateScope baseURI v =
185− case v of
186− Object hm -> do
187− let hmWithOnlyId = case HM.lookup idKey hm of
188− Nothing -> mempty
189− Just idVal -> HM.singleton idKey idVal
190− schema <- first SubschemaDecodingError (fromJSONEither (Object hmWithOnlyId))
191− Right (updateScope baseURI schema)
192− _ -> Right baseURI
193− where
194− idKey :: Text
195− idKey = "id"
168+ case v of
169+ Array _ -> pure (v, uri)
170+ _ -> do
171+ -- PERFORMANCE: Avoid deserializing subschemas.
172+ schema <- first SubschemaDecodingError (fromJSONEither v)
173+ pure (v, updateScope uri schema)
196174
197175--------------------------------------------------
198176-- * enum
src/JSONSchema/Validator/Draft4/Array.hs+6−7
@@ -1,10 +1,11 @@
1+
12module JSONSchema.Validator.Draft4.Array where
23
34import Import
45
5−import qualified Data.List.NonEmpty as NE
6−import qualified Data.Vector as V
7−import qualified JSONPointer as JP
6+import qualified Data.List.NonEmpty as NE
7+import qualified Data.Vector as V
8+import qualified JSONPointer as JP
89
910import JSONSchema.Validator.Utils (allUniqueValues)
1011
@@ -173,8 +174,7 @@ itemsVal f a xs =
173174 :: schema
174175 -> (JP.Index, Value)
175176 -> Maybe (JP.Index, NonEmpty err)
176− validateElem schema (index,x) =
177− (\v -> (index, v)) <$> NE.nonEmpty (f schema x)
177+ validateElem schema (index,x) = (index,) <$> NE.nonEmpty (f schema x)
178178
179179--------------------------------------------------
180180-- * additionalItems
@@ -219,7 +219,6 @@ additionalItemsVal _ (AdditionalBool False) xs =
219219 AdditionalItemsBoolInvalid <$> NE.nonEmpty xs
220220additionalItemsVal f (AdditionalObject subSchema) xs =
221221 let res = mapMaybe
222− (\(index,x) -> (\v -> (index, v))
223− <$> NE.nonEmpty (f subSchema x))
222+ (\(index,x) -> (index,) <$> NE.nonEmpty (f subSchema x))
224223 xs
225224 in AdditionalItemsObjectInvalid <$> NE.nonEmpty res
src/JSONSchema/Validator/Draft4/Number.hs+2−1
@@ -1,8 +1,9 @@
1+
12module JSONSchema.Validator.Draft4.Number where
23
34import Import
45
5−import Data.Fixed (mod')
6+import Data.Fixed (mod')
67import Data.Scientific (Scientific)
78
89--------------------------------------------------
src/JSONSchema/Validator/Draft4/Object.hs+6−5
@@ -1,3 +1,4 @@
1+
12module JSONSchema.Validator.Draft4.Object
23 ( module JSONSchema.Validator.Draft4.Object
34 , module JSONSchema.Validator.Draft4.Object.Properties
@@ -5,11 +6,11 @@ module JSONSchema.Validator.Draft4.Object
56
67import Import
78
8−import qualified Data.HashMap.Strict as HM
9−import qualified Data.List.NonEmpty as NE
10−import Data.Set (Set)
11−import qualified Data.Set as Set
12−import qualified Data.Text as T
9+import qualified Data.HashMap.Strict as HM
10+import qualified Data.List.NonEmpty as NE
11+import Data.Set (Set)
12+import qualified Data.Set as Set
13+import qualified Data.Text as T
1314
1415import JSONSchema.Validator.Draft4.Object.Properties
1516import JSONSchema.Validator.Utils
src/JSONSchema/Validator/Draft4/Object/Properties.hs+6−5
@@ -1,12 +1,13 @@
1+
12module JSONSchema.Validator.Draft4.Object.Properties where
23
34import Import
45
5−import qualified Data.Hashable as HA
6−import qualified Data.HashMap.Strict as HM
7−import qualified Data.List.NonEmpty as NE
8−import Data.Text.Encoding (encodeUtf8)
9−import qualified JSONPointer as JP
6+import qualified Data.Hashable as HA
7+import qualified Data.HashMap.Strict as HM
8+import qualified Data.List.NonEmpty as NE
9+import Data.Text.Encoding (encodeUtf8)
10+import qualified JSONPointer as JP
1011import qualified Text.Regex.PCRE.Heavy as RE
1112
1213data PropertiesRelated schema = PropertiesRelated
src/JSONSchema/Validator/Draft4/String.hs+3−2
@@ -1,9 +1,10 @@
1+
12module JSONSchema.Validator.Draft4.String where
23
34import Import
45
5−import qualified Data.Text as T
6−import Data.Text.Encoding (encodeUtf8)
6+import qualified Data.Text as T
7+import Data.Text.Encoding (encodeUtf8)
78import qualified Text.Regex.PCRE.Heavy as RE
89
910--------------------------------------------------
src/JSONSchema/Validator/Reference.hs+7−7
@@ -3,14 +3,15 @@
33--
44-- And is extended for JSON Schema here:
55-- <http://json-schema.org/latest/json-schema-core.html#anchor26>
6+
67module JSONSchema.Validator.Reference where
78
89import Import
910
10−import qualified Data.Text as T
11−import System.FilePath (dropFileName, (</>))
11+import qualified Data.Text as T
12+import System.FilePath ((</>), dropFileName)
1213
13−data Scope schema = Scope
14+data Scope schema = Scope
1415 { _topLevelDocument :: schema
1516 , _documentURI :: Maybe Text
1617 , _currentBaseURI :: BaseURI
@@ -24,10 +25,9 @@ newtype BaseURI
2425type URIAndFragment = (Maybe Text, Maybe Text)
2526
2627updateResolutionScope :: BaseURI -> Maybe Text -> BaseURI
27−updateResolutionScope base idKeyword =
28− case idKeyword of
29− Just t -> BaseURI . fst . baseAndFragment $ resolveScopeAgainst base t
30− Nothing -> base
28+updateResolutionScope base idKeyword
29+ | Just t <- idKeyword = BaseURI . fst . baseAndFragment $ resolveScopeAgainst base t
30+ | otherwise = base
3131
3232resolveReference :: BaseURI -> Text -> URIAndFragment
3333resolveReference base t = baseAndFragment (resolveScopeAgainst base t)
src/JSONSchema/Validator/Types.hs+1
@@ -1,3 +1,4 @@
1+
12module JSONSchema.Validator.Types where
23
34import Import
src/JSONSchema/Validator/Utils.hs+8−7
@@ -1,15 +1,16 @@
1+
12module JSONSchema.Validator.Utils where
23
34import Import
45
5−import Control.Monad (fail)
6+import Control.Monad (fail)
67import qualified Data.HashMap.Strict as HM
7−import qualified Data.List.NonEmpty as NE
8−import Data.Scientific (Scientific, fromFloatDigits)
9−import Data.Set (Set)
10−import qualified Data.Set as S
11−import qualified Data.Text as T
12−import qualified Data.Vector as V
8+import qualified Data.List.NonEmpty as NE
9+import Data.Scientific (Scientific, fromFloatDigits)
10+import Data.Set (Set)
11+import qualified Data.Set as S
12+import qualified Data.Text as T
13+import qualified Data.Vector as V
1314
1415--------------------------------------------------
1516-- * QuickCheck
stack.yaml+7−2
@@ -1,2 +1,7 @@
1−resolver: lts-10.8
2−pvp-bounds: upper-revision
1+resolver: nightly-2017-09-18
2+# packages:
3+# - './'
4+# - location: '../hjsonpointer'
5+# extra-dep: true
6+extra-deps:
7+ - hjsonpointer-1.3.0
test/Local.hs+10−7
@@ -1,18 +1,19 @@
1+
12module Main where
23
34import Protolude
45
56import Data.Aeson
67import qualified Data.List.NonEmpty as NE
7−import qualified System.Timeout as TO
8+import qualified System.Timeout as TO
89import Test.Hspec
9−import Test.QuickCheck (property)
10+import Test.QuickCheck (property)
1011
11−import qualified JSONSchema.Draft4 as D4
12−import qualified JSONSchema.Types as JT
12+import qualified JSONSchema.Draft4 as D4
13+import qualified JSONSchema.Types as JT
1314import qualified Local.Failure
14−import qualified Local.Reference
1515import qualified Local.Validation
16+import qualified Local.Reference
1617import Shared
1718
1819-- Examples
@@ -32,10 +33,12 @@ main = do
3233 -- Language agnostic tests
3334 ts <- readSchemaTests
3435 dir
35− (\a -> not (isHTTPTest a || skipTest a))
36+ (\a -> not (isHTTPTest a || skipOptional a))
3637
3738 -- Custom supplements to the language agnostic tests
38− supplementTs <- readSchemaTests supplementDir (not . isHTTPTest)
39+ supplementTs <- readSchemaTests
40+ supplementDir
41+ (\a -> not (isHTTPTest a || skipOptional a))
3942
4043 hspec $ do
4144 describe "Examples" exampleTests
test/Local/Failure.hs+4−3
@@ -1,14 +1,15 @@
1+
12module Local.Failure where
23
34import Protolude
45
56import Data.Aeson
6−import qualified Data.Vector as V
7−import qualified JSONPointer as JP
87import Test.Hspec
8+import qualified Data.Vector as V
9+import qualified JSONPointer as JP
910
1011import JSONSchema.Draft4
11−import qualified JSONSchema.Draft4.Spec as Spec
12+import qualified JSONSchema.Draft4.Spec as Spec
1213import qualified JSONSchema.Validator.Draft4 as VAL
1314
1415spec :: Spec
test/Local/Reference.hs+1
@@ -1,3 +1,4 @@
1+
12module Local.Reference where
23
34import Protolude
test/Local/Validation.hs+5−4
@@ -1,16 +1,17 @@
1+
12module Local.Validation where
23
34import Protolude
45
56import Data.Aeson
6−import qualified Data.Aeson as AE
7+import qualified Data.Aeson as AE
78import qualified Data.HashMap.Strict as HM
8−import Data.String (String)
9+import Data.String (String)
910import Test.Hspec
1011
11−import qualified AlternateSchema as AS
1212import JSONSchema.Draft4
13−import qualified JSONSchema.Types as JT
13+import qualified JSONSchema.Types as JT
14+import qualified AlternateSchema as AS
1415
1516spec :: Spec
1617spec = do
test/Remote.hs+7−6
@@ -1,16 +1,17 @@
1+
12module Main where
23
34import Protolude
45
5−import Control.Concurrent.Async (withAsync)
6−import qualified Data.List.NonEmpty as NE
6+import Control.Concurrent.Async (withAsync)
7+import qualified Data.List.NonEmpty as NE
78import Network.Wai.Application.Static (defaultFileServerSettings,
89 staticApp)
9−import qualified Network.Wai.Handler.Warp as Warp
10+import Network.Wai.Handler.Warp (run)
1011import Test.Hspec
1112
12−import qualified JSONSchema.Draft4 as D4
13−import qualified JSONSchema.Types as JT
13+import qualified JSONSchema.Draft4 as D4
14+import qualified JSONSchema.Types as JT
1415import Shared
1516
1617-- Examples
@@ -51,7 +52,7 @@ main = withAsync serve $ \_ -> do
5152 assertResult sc failures
5253
5354serve :: IO ()
54−serve = Warp.run 1234
55+serve = run 1234
5556 . staticApp
5657 . defaultFileServerSettings
5758 $ "JSON-Schema-Test-Suite/remotes"
test/Shared.hs+20−18
@@ -1,29 +1,19 @@
1+
12module Shared where
23
34import Protolude
45
5−import Control.Monad (fail)
6+import Control.Monad (fail)
67import Data.Aeson
7−import Data.Aeson.TH (fieldLabelModifier)
8−import qualified Data.ByteString as BS
9−import Data.Char (toLower)
10−import Data.List (stripPrefix, unlines)
11−import qualified Data.Text as T
8+import Data.Aeson.TH (fieldLabelModifier)
9+import qualified Data.ByteString as BS
10+import Data.Char (toLower)
11+import Data.List (stripPrefix, unlines)
12+import qualified Data.Text as T
1213import qualified System.Directory as SD
13−import System.FilePath ((</>))
14+import System.FilePath ((</>))
1415import Test.Hspec
1516
16−skipTest :: FilePath -> Bool
17−skipTest file = (file == "optional/format.json") -- Optional
18− || (file == "optional/zeroTerminatedFloats.json")
19− || (file == "optional/ecmascript-regex.json")
20−
21−
22−isHTTPTest :: FilePath -> Bool
23−isHTTPTest file = (file == "definitions.json")
24− || (file == "ref.json")
25− || (file == "refRemote.json")
26−
2717-- Recursively return the contents of a directory
2818-- (or return itself if given a file as an argument).
2919--
@@ -47,6 +37,18 @@ listFilesFullPath path = do
4737 fs <- fmap (path </>) <$> SD.listDirectory path
4838 concat <$> traverse listFilesFullPath fs
4939
40+isHTTPTest :: FilePath -> Bool
41+isHTTPTest file = (file == "definitions.json")
42+ || (file == "ref.json")
43+ || (file == "refRemote.json")
44+
45+-- | We may never support the @"format"@ keywords
46+-- and are currently failing the others listed here.
47+skipOptional :: FilePath -> Bool
48+skipOptional file = (file == "optional/format.json")
49+ || (file == "optional/zeroTerminatedFloats.json")
50+ || (file == "optional/ecmascript-regex.json")
51+
5052data SchemaTest = SchemaTest
5153 { _stDescription :: Text
5254 , _stSchema :: Value