@@ -3,7 +3,6 @@ module Data.Schematic.Validation where
33import Control.Monad
44import Control.Monad.Validation
55import Data.Aeson
6−import Data.Aeson.Internal
76import Data.Aeson.Types
87import Data.Foldable
98import Data.Functor.Identity
@@ -18,7 +17,6 @@ import Data.Traversable
1817import Data.Union
1918import Data.Vector as V
2019import Data.Vinyl
21−import Data.Vinyl.TypeLevel
2220import Prelude as P
2321import Text.Regex.TDFA
2422
@@ -39,7 +37,7 @@ instance (TopLevel a, SingI a, FromJSON (JsonRepr a))
3937
4038isValid :: ParseResult a -> Bool
4139isValid (Valid _) = True
42−isValid _ = False
40+isValid _ = False
4341
4442isDecodingError :: ParseResult a -> Bool
4543isDecodingError (DecodingError _) = True
@@ -50,11 +48,11 @@ isValidationError (ValidationError _) = True
5048isValidationError _ = False
5149
5250validateTextConstraint
53− :: JSONPathText
51+ :: JSONPath
5452 -> Text
5553 -> Sing (tcs :: TextConstraint)
5654 -> Validation ()
57−validateTextConstraint (JSONPathText path) t = \case
55+validateTextConstraint (JSONPath path) t = \case
5856 STEq n -> do
5957 let
6058 nlen = withKnownNat n $ natVal n
@@ -107,11 +105,11 @@ validateTextConstraint (JSONPathText path) t = \case
107105 unless (matching ss) warn
108106
109107validateNumberConstraint
110− :: JSONPathText
108+ :: JSONPath
111109 -> Scientific
112110 -> Sing (tcs :: NumberConstraint)
113111 -> Validation ()
114−validateNumberConstraint (JSONPathText path) num = \case
112+validateNumberConstraint (JSONPath path) num = \case
115113 SNEq n -> do
116114 let
117115 nlen = withKnownNat n $ natVal n
@@ -149,11 +147,11 @@ validateNumberConstraint (JSONPathText path) num = \case
149147 unless predicate warn
150148
151149validateArrayConstraint
152− :: JSONPathText
150+ :: JSONPath
153151 -> V.Vector a
154152 -> Sing (tcs :: ArrayConstraint)
155153 -> Validation ()
156−validateArrayConstraint (JSONPathText path) v = \case
154+validateArrayConstraint (JSONPath path) v = \case
157155 SAEq n -> do
158156 let
159157 nlen = withKnownNat n $ natVal n
@@ -161,38 +159,10 @@ validateArrayConstraint (JSONPathText path) v = \case
161159 errMsg = "length should be == " <> T.pack (show nlen)
162160 warn = vWarning $ mmSingleton path (pure errMsg)
163161 unless predicate warn
164− SAGt n -> do
165− let
166− nlen = withKnownNat n $ natVal n
167− predicate = fromIntegral (V.length v) > nlen
168− errMsg = "length should be > " <> T.pack (show nlen)
169− warn = vWarning $ mmSingleton path (pure errMsg)
170− unless predicate warn
171− SAGe n -> do
172− let
173− nlen = withKnownNat n $ natVal n
174− predicate = fromIntegral (V.length v) >= nlen
175− errMsg = "length should be >= " <> T.pack (show nlen)
176− warn = vWarning $ mmSingleton path (pure errMsg)
177− unless predicate warn
178− SALt n -> do
179− let
180− nlen = withKnownNat n $ natVal n
181− predicate = fromIntegral (V.length v) < nlen
182− errMsg = "length should be < " <> T.pack (show nlen)
183− warn = vWarning $ mmSingleton path (pure errMsg)
184− unless predicate warn
185− SALe n -> do
186− let
187− nlen = withKnownNat n $ natVal n
188− predicate = fromIntegral (V.length v) <= nlen
189− errMsg = "length should be <= " <> T.pack (show nlen)
190− warn = vWarning $ mmSingleton path (pure errMsg)
191− unless predicate warn
192162
193163validateJsonRepr
194164 :: Sing schema
195− -> JSONPath
165+ -> [DemotedPathSegment]
196166 -> JsonRepr schema
197167 -> Validation ()
198168validateJsonRepr sschema dpath jr = case jr of
@@ -226,7 +196,7 @@ validateJsonRepr sschema dpath jr = case jr of
226196 process cs
227197 process acs
228198 for_ (V.indexed v) $ \(ix, jr') -> do
229− let newPath = dpath <> pure (Index $ fromIntegral ix)
199+ let newPath = dpath <> pure (DIx $ fromIntegral ix)
230200 validateJsonRepr s newPath jr'
231201 ReprOptional d -> case sschema of
232202 SSchemaOptional ss -> case d of
@@ -238,49 +208,17 @@ validateJsonRepr sschema dpath jr = case jr of
238208 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
239209 go RNil = pure ()
240210 go (f@(FieldRepr d) :& ftl) = do
241− let newPath = dpath <> [Key (knownFieldName f)]
211+ let newPath = dpath <> [DKey (knownFieldName f)]
242212 validateJsonRepr (knownFieldSchema f) newPath d
243213 go ftl
244− ReprUnion _ -> pure () -- FIXME
245− -- case sschema of
246− -- SSchemaUnion ss -> case ss of
247− -- SCons s stl -> case umatch' s u of
248− -- Nothing -> case urestrict u of
249− -- Nothing ->
250− -- fail "impossible to produce subUnion, please report this as a bug"
251− -- Just x -> do
252− -- let
253− -- JSONPathText path = demotedPathToText dpath
254− -- case stl of
255− -- SNil -> void $ vWarning $ mmSingleton path
256− -- $ pure "union handling error, please report this as bug"
257− -- SCons s' stl' ->
258− -- validateJsonRepr (SSchemaUnion (SCons s' stl')) dpath
259− -- $ toUnion (SCons s' stl') x
260− -- Just x -> validateJsonRepr s dpath x
261−
262−-- subUnion
263−-- :: Sing (s ': stl)
264−-- -> ( USubset stl (s ': stl) (RImage stl (s ': stl))
265−-- => Union f (s ': stl)
266−-- -> Maybe (Union f stl) )
267−-- subUnion (SCons s stl) = urestrict
268−
269−-- withUSubset
270−-- :: Sing (s ': stl)
271−-- -> (USubset stl (s ': stl) (RImage stl (s ': stl)) => Maybe (Union f stl))
272−-- -> Maybe (Union f stl)
273−-- withUSubset (SCons s stl) r = r
274−
275−toUnion
276− :: USubset s' (s ': ss) (RImage s' (s ': ss))
277− => Sing (s ': ss)
278− -> Union JsonRepr s'
279− -> JsonRepr ('SchemaUnion (s ': ss))
280−toUnion _ = ReprUnion . urelax
281−
282−umatch' :: UElem a as i => Sing a -> Union f as -> Maybe (f a)
283−umatch' _ u = umatch u
214+ ReprUnion ru -> case sschema of
215+ SSchemaUnion su -> validateUnion su ru
216+ where
217+ validateUnion :: Sing us -> Union JsonRepr us -> Validation ()
218+ validateUnion ss r = case (ss,r) of
219+ (SCons s _, This v) -> validateJsonRepr s dpath v
220+ (SCons _ stl, That r') -> validateUnion stl r'
221+ (SNil,_) -> fail "Invalid union. Please report this as a bug"
284222
285223parseAndValidateJson
286224 :: forall schema