@@ -1,24 +1,25 @@
1module JSONSchema.Validator.Draft4.Any where
2module JSONSchema.Validator.Draft4.Any where
3−import Import hiding ((<>))
4+import Import hiding ((<>))
5−import Data.Aeson.TH (constructorTagModifier)
6+import Data.Aeson.TH (constructorTagModifier)
6−import Data.Char (toLower)
7+import Data.Char (toLower)
7−import qualified Data.HashMap.Strict as HM
8−import Data.List.NonEmpty (NonEmpty((:|)))
8+import Data.List.NonEmpty (NonEmpty((:|)))
9−import qualified Data.List.NonEmpty as NE
9+import qualified Data.List.NonEmpty as NE
10−import qualified Data.Scientific as SCI
10+import qualified Data.Scientific as SCI
11import Data.Semigroup
11import Data.Semigroup
12−import Data.Set (Set)
12+import Data.Set (Set)
13−import qualified Data.Set as Set
13+import qualified Data.Set as Set
14−import Data.Text.Encoding.Error (UnicodeException)
14+import Data.Text.Encoding.Error (UnicodeException)
15−import qualified JSONPointer as JP
15+import qualified JSONPointer as JP
16−import Network.HTTP.Types.URI (urlDecode)
16+import Network.HTTP.Types.URI (urlDecode)
18+import qualified JSONSchema.Validator.Utils as UT
18−import JSONSchema.Validator.Reference (BaseURI(..), Scope(..),
19+import JSONSchema.Validator.Reference (BaseURI(..),
19 URIAndFragment,
21 URIAndFragment,
20 resolveReference)
22 resolveReference)
21−import qualified JSONSchema.Validator.Utils as UT
23--------------------------------------------------
24--------------------------------------------------
@@ -134,7 +135,7 @@ data JSONPointerError
134 deriving (Eq, Show)
135 deriving (Eq, Show)
136resolveFragment
137resolveFragment
137− :: (FromJSON schema, ToJSON schema)
138+ :: forall schema. (FromJSON schema, ToJSON schema)
138 => (BaseURI -> schema -> BaseURI)
139 => (BaseURI -> schema -> BaseURI)
139 -> Scope schema
140 -> Scope schema
@@ -159,40 +160,17 @@ resolveFragment updateScope scope fragment = do
159 -- as property keys that aren't meant to change scope?
160 -- as property keys that aren't meant to change scope?
160 -- Perhaps this should be added to the language agnostic
161 -- Perhaps this should be added to the language agnostic
161 -- test suite as well.
162 -- test suite as well.
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.
167 go :: (Value, BaseURI)
163 go :: (Value, BaseURI)
168 -> JP.Token
164 -> JP.Token
169 -> Either JSONPointerError (Value, BaseURI)
165 -> Either JSONPointerError (Value, BaseURI)
170− go (lastVal, baseURI) tok = do
166+ go (lastVal, uri) tok = do
171 v <- first ResolutionError (JP.resolveToken tok lastVal)
167 v <- first ResolutionError (JP.resolveToken tok lastVal)
172− newBase <- newBaseURIFromFragment updateScope baseURI v
168+ case v of
173− Right (v, newBase)
169+ Array _ -> pure (v, uri)
175−-- | Update the 'BaseURI' (the store of the current "id" value)
171+ -- PERFORMANCE: Avoid deserializing subschemas.
176−-- after resolving one token of a JSON Pointer and stepping into
178−newBaseURIFromFragment
180− => (BaseURI -> schema -> BaseURI)
183− -> Either JSONPointerError BaseURI
184−newBaseURIFromFragment updateScope baseURI v =
187− let hmWithOnlyId = case HM.lookup idKey hm of
189− Just idVal -> HM.singleton idKey idVal
190− schema <- first SubschemaDecodingError (fromJSONEither (Object hmWithOnlyId))
172+ schema <- first SubschemaDecodingError (fromJSONEither v)
191− Right (updateScope baseURI schema)
173+ pure (v, updateScope uri schema)
197--------------------------------------------------
175--------------------------------------------------