@@ -1,19 +1,24 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
2+
13module Data.Schematic.Validation where
24
5+import Control.Applicative
36import Control.Monad
47import Control.Monad.Validation
58import Data.Aeson
69import Data.Aeson.Types
10+import Data.Coerce
711import Data.Foldable
812import Data.Functor.Identity
913import Data.Monoid
1014import Data.Schematic.Path
1115import Data.Schematic.Schema
1216import Data.Scientific
13−import Data.Singletons.Prelude
17+import Data.Singletons.Prelude hiding (Const)
1418import Data.Singletons.TypeLits
1519import Data.Text as T
1620import Data.Traversable
21+import Data.Type.Equality
1722import Data.Union
1823import Data.Vector as V
1924import Data.Vinyl
@@ -161,13 +166,25 @@ validateArrayConstraint (JSONPath path) v = \case
161166 warn = vWarning $ mmSingleton path (pure errMsg)
162167 unless predicate warn
163168
164−validateJsonRepr
165− :: Sing schema
166− -> [DemotedPathSegment]
169+lookaheadJsonRepr
170+ :: forall schema. (SingI schema)
171+ => [DemotedPathSegment]
167172 -> JsonRepr schema
168173 -> Validation ()
169−validateJsonRepr sschema dpath jr = case jr of
170− ReprText t -> case sschema of
174+lookaheadJsonRepr dpath jr = case sing :: Sing schema of
175+ SSchemaText s -> validateJsonRepr @schema dpath jr
176+ SSchemaNumber s -> validateJsonRepr @schema dpath jr
177+ SSchemaNull -> validateJsonRepr @schema dpath jr
178+ SSchemaBoolean -> validateJsonRepr @schema dpath jr
179+ SSchemaArray _ _ -> validateJsonRepr @schema dpath jr
180+ SSchemaObject _ -> validateJsonRepr @schema dpath jr
181+ SSchemaUnion _ -> validateJsonRepr @schema dpath jr
182+
183+class Validatable schema where
184+ validateJsonRepr :: [DemotedPathSegment] -> JsonRepr schema -> Validation ()
185+
186+instance SingI (SchemaText s) => Validatable (SchemaText s) where
187+ validateJsonRepr dpath (ReprText t) = case sing :: Sing (SchemaText s) of
171188 SSchemaText scs -> do
172189 let
173190 process :: Sing (cs :: [TextConstraint]) -> Validation ()
@@ -176,7 +193,9 @@ validateJsonRepr sschema dpath jr = case jr of
176193 validateTextConstraint (demotedPathToText dpath) t c
177194 process cs
178195 process scs
179− ReprNumber n -> case sschema of
196+
197+instance SingI (SchemaNumber s) => Validatable (SchemaNumber s) where
198+ validateJsonRepr dpath (ReprNumber n) = case sing :: Sing (SchemaNumber s) of
180199 SSchemaNumber scs -> do
181200 let
182201 process :: Sing (cs :: [NumberConstraint]) -> Validation ()
@@ -185,9 +204,13 @@ validateJsonRepr sschema dpath jr = case jr of
185204 validateNumberConstraint (demotedPathToText dpath) n c
186205 process cs
187206 process scs
188− ReprNull -> pure ()
189− ReprBoolean _ -> pure ()
190− ReprArray v -> case sschema of
207+
208+instance Validatable SchemaNull where validateJsonRepr _ _ = pure ()
209+
210+instance Validatable SchemaBoolean where validateJsonRepr _ _ = pure ()
211+
212+instance SingI (SchemaArray css s) => Validatable (SchemaArray css s) where
213+ validateJsonRepr dpath (ReprArray v) = case sing :: Sing (SchemaArray css s) of
191214 SSchemaArray acs s -> do
192215 let
193216 process :: Sing (cs :: [ArrayConstraint]) -> Validation ()
@@ -198,60 +221,59 @@ validateJsonRepr sschema dpath jr = case jr of
198221 process acs
199222 for_ (V.indexed v) $ \(ix, jr') -> do
200223 let newPath = dpath <> pure (DIx $ fromIntegral ix)
201− validateJsonRepr s newPath jr'
202− ReprOptional d -> case sschema of
203− SSchemaOptional ss -> case d of
204− Just x -> validateJsonRepr ss dpath x
224+ withSingI s $ lookaheadJsonRepr newPath jr'
225+
226+instance SingI (SchemaOptional s) => Validatable (SchemaOptional s) where
227+ validateJsonRepr dpath (ReprOptional s) = case sing :: Sing (SchemaOptional s) of
228+ SSchemaOptional ss -> case s of
229+ Just x -> withSingI ss $ lookaheadJsonRepr dpath x
205230 Nothing -> pure ()
206− ReprObject fs -> case sschema of
231+
232+instance SingI (SchemaObject s) => Validatable (SchemaObject s) where
233+ validateJsonRepr dpath (ReprObject fs) = case sing :: Sing (SchemaObject s) of
207234 SSchemaObject _ -> go fs
208235 where
209236 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
210237 go RNil = pure ()
211238 go (f@(FieldRepr d) :& ftl) = do
212239 let newPath = dpath <> [DKey (knownFieldName f)]
213− validateJsonRepr (knownFieldSchema f) newPath d
240+ withSingI (knownFieldSchema f)
241+ $ lookaheadJsonRepr newPath d
214242 go ftl
215− ReprUnion _ -> pure () -- FIXME
216− -- case sschema of
217− -- SSchemaUnion ss -> case ss of
218− -- SCons s stl -> case umatch' s u of
219− -- Nothing -> case urestrict u of
220− -- Nothing ->
221− -- fail "impossible to produce subUnion, please report this as a bug"
222− -- Just x -> do
223− -- let
224− -- JSONPath path = demotedPathToText dpath
225− -- case stl of
226− -- SNil -> void $ vWarning $ mmSingleton path
227− -- $ pure "union handling error, please report this as bug"
228− -- SCons s' stl' ->
229− -- validateJsonRepr (SSchemaUnion (SCons s' stl')) dpath
230− -- $ toUnion (SCons s' stl') x
231− -- Just x -> validateJsonRepr s dpath x
232243
233−-- subUnion
234−-- :: Sing (s ': stl)
235−-- -> ( USubset stl (s ': stl) (RImage stl (s ': stl))
236−-- => Union f (s ': stl)
237−-- -> Maybe (Union f stl) )
238−-- subUnion (SCons s stl) = urestrict
244+instance SingI (SchemaUnion s) => Validatable (SchemaUnion s) where
245+ validateJsonRepr dpath (ReprUnion u) = pure ()
246+
247+ -- ReprUnion u -> do
248+ -- let
249+ -- v :: forall s. JsonRepr s -> Const (Validation ()) s
250+ -- v j = Const $ validateJsonRepr (singByProxy j) dpath j
251+ -- peel :: Union (Const (Validation ())) ts -> Validation ()
252+ -- peel (That u) = peel u
253+ -- peel (This v) = getConst v
254+ -- peel (umap v u)
255+ -- ReprUnion u -> do
256+ -- let
257+ -- peel :: Union JsonRepr ss -> [DemotedSchema] -> Validation ()
258+ -- peel (That u) ds = peel u ds
259+ -- peel (This j) ds = case ds of
260+ -- [] -> error "peeling error: report this as a schematic bug"
261+ -- (ds' :_) -> let s' = toSing ds'
262+ -- in case s' of
263+ -- SomeSing s -> case s `testEquality` (singByProxy j) of
264+ -- Just Refl -> validateJsonRepr s dpath j
265+ -- case sschema of
266+ -- SSchemaUnion us -> peel u (fromSing us)
239267
240−-- withUSubset
241−-- :: Sing (s ': stl)
242−-- -> (USubset stl (s ': stl) (RImage stl (s ': stl)) => Maybe (Union f stl))
243−-- -> Maybe (Union f stl)
244−-- withUSubset (SCons s stl) r = r
268+class UPeel ss where
269+ upeel :: Union JsonRepr ss -> Sing (SchemaUnion ss :: Schema) -> Validation ()
245270
246−toUnion
247− :: USubset s' (s ': ss) (RImage s' (s ': ss))
248− => Sing (s ': ss)
249− -> Union JsonRepr s'
250− -> JsonRepr ('SchemaUnion (s ': ss))
251−toUnion _ = ReprUnion . urelax
271+instance (UPeel stl) => UPeel (s ': stl) where
272+ upeel (That u) (SSchemaUnion (SCons _ stl)) = upeel @stl u (SSchemaUnion stl)
273+ upeel (This j) (SSchemaUnion (SCons s _)) = withSingI s $ lookaheadJsonRepr mempty j
252274
253−umatch' :: UElem a as i => Sing a -> Union f as -> Maybe (f a)
254−umatch' _ u = umatch u
275+umatch' :: UElem a as i => Sing a -> Union JsonRepr as -> Maybe (JsonRepr a)
276+umatch' _ = umatch
255277
256278parseAndValidateJson
257279 :: forall schema
@@ -263,7 +285,7 @@ parseAndValidateJson v =
263285 Left s -> DecodingError $ T.pack s
264286 Right jsonRepr ->
265287 let
266− validate = validateJsonRepr (sing :: Sing schema) [] jsonRepr
288+ validate = lookaheadJsonRepr [] jsonRepr
267289 res = runIdentity . runValidationTEither $ validate
268290 in case res of
269291 Left em -> ValidationError em