@@ -3,7 +3,6 @@ module Data.Schematic.Validation where
3import Control.Monad
3import Control.Monad
4import Control.Monad.Validation
4import Control.Monad.Validation
5import Data.Aeson
5import Data.Aeson
6−import Data.Aeson.Internal
7import Data.Aeson.Types
6import Data.Aeson.Types
8import Data.Foldable
7import Data.Foldable
9import Data.Functor.Identity
8import Data.Functor.Identity
@@ -18,7 +17,6 @@ import Data.Traversable
18import Data.Union
17import Data.Union
19import Data.Vector as V
18import Data.Vector as V
20import Data.Vinyl
19import Data.Vinyl
21−import Data.Vinyl.TypeLevel
22import Prelude as P
20import Prelude as P
23import Text.Regex.TDFA
21import Text.Regex.TDFA
@@ -39,7 +37,7 @@ instance (TopLevel a, SingI a, FromJSON (JsonRepr a))
40isValid :: ParseResult a -> Bool
38isValid :: ParseResult a -> Bool
41isValid (Valid _) = True
39isValid (Valid _) = True
42−isValid _ = False
40+isValid _ = False
44isDecodingError :: ParseResult a -> Bool
42isDecodingError :: ParseResult a -> Bool
45isDecodingError (DecodingError _) = True
43isDecodingError (DecodingError _) = True
@@ -50,11 +48,11 @@ isValidationError (ValidationError _) = True
50isValidationError _ = False
48isValidationError _ = False
52validateTextConstraint
50validateTextConstraint
53− :: JSONPathText
51+ :: JSONPath
55 -> Sing (tcs :: TextConstraint)
53 -> Sing (tcs :: TextConstraint)
56 -> Validation ()
54 -> Validation ()
57−validateTextConstraint (JSONPathText path) t = \case
55+validateTextConstraint (JSONPath path) t = \case
58 STEq n -> do
56 STEq n -> do
60 nlen = withKnownNat n $ natVal n
58 nlen = withKnownNat n $ natVal n
@@ -107,11 +105,11 @@ validateTextConstraint (JSONPathText path) t = \case
107 unless (matching ss) warn
105 unless (matching ss) warn
109validateNumberConstraint
107validateNumberConstraint
110− :: JSONPathText
108+ :: JSONPath
111 -> Scientific
109 -> Scientific
112 -> Sing (tcs :: NumberConstraint)
110 -> Sing (tcs :: NumberConstraint)
113 -> Validation ()
111 -> Validation ()
114−validateNumberConstraint (JSONPathText path) num = \case
112+validateNumberConstraint (JSONPath path) num = \case
115 SNEq n -> do
113 SNEq n -> do
117 nlen = withKnownNat n $ natVal n
115 nlen = withKnownNat n $ natVal n
@@ -149,11 +147,11 @@ validateNumberConstraint (JSONPathText path) num = \case
149 unless predicate warn
147 unless predicate warn
151validateArrayConstraint
149validateArrayConstraint
152− :: JSONPathText
150+ :: JSONPath
153 -> V.Vector a
151 -> V.Vector a
154 -> Sing (tcs :: ArrayConstraint)
152 -> Sing (tcs :: ArrayConstraint)
155 -> Validation ()
153 -> Validation ()
156−validateArrayConstraint (JSONPathText path) v = \case
154+validateArrayConstraint (JSONPath path) v = \case
157 SAEq n -> do
155 SAEq n -> do
159 nlen = withKnownNat n $ natVal n
157 nlen = withKnownNat n $ natVal n
@@ -161,38 +159,10 @@ validateArrayConstraint (JSONPathText path) v = \case
161 errMsg = "length should be == " <> T.pack (show nlen)
159 errMsg = "length should be == " <> T.pack (show nlen)
162 warn = vWarning $ mmSingleton path (pure errMsg)
160 warn = vWarning $ mmSingleton path (pure errMsg)
163 unless predicate warn
161 unless predicate warn
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
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
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
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
193validateJsonRepr
163validateJsonRepr
194 :: Sing schema
164 :: Sing schema
195− -> JSONPath
165+ -> [DemotedPathSegment]
196 -> JsonRepr schema
166 -> JsonRepr schema
197 -> Validation ()
167 -> Validation ()
198validateJsonRepr sschema dpath jr = case jr of
168validateJsonRepr sschema dpath jr = case jr of
@@ -226,7 +196,7 @@ validateJsonRepr sschema dpath jr = case jr of
226 process cs
196 process cs
227 process acs
197 process acs
228 for_ (V.indexed v) $ \(ix, jr') -> do
198 for_ (V.indexed v) $ \(ix, jr') -> do
229− let newPath = dpath <> pure (Index $ fromIntegral ix)
199+ let newPath = dpath <> pure (DIx $ fromIntegral ix)
230 validateJsonRepr s newPath jr'
200 validateJsonRepr s newPath jr'
231 ReprOptional d -> case sschema of
201 ReprOptional d -> case sschema of
232 SSchemaOptional ss -> case d of
202 SSchemaOptional ss -> case d of
@@ -238,49 +208,17 @@ validateJsonRepr sschema dpath jr = case jr of
238 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
208 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
239 go RNil = pure ()
209 go RNil = pure ()
240 go (f@(FieldRepr d) :& ftl) = do
210 go (f@(FieldRepr d) :& ftl) = do
241− let newPath = dpath <> [Key (knownFieldName f)]
211+ let newPath = dpath <> [DKey (knownFieldName f)]
242 validateJsonRepr (knownFieldSchema f) newPath d
212 validateJsonRepr (knownFieldSchema f) newPath d
244− ReprUnion _ -> pure () -- FIXME
245− -- case sschema of
214+ ReprUnion ru -> case sschema of
246− -- SSchemaUnion ss -> case ss of
215+ SSchemaUnion su -> validateUnion su ru
247− -- SCons s stl -> case umatch' s u of
216+ where
248− -- Nothing -> case urestrict u of
217+ validateUnion :: Sing us -> Union JsonRepr us -> Validation ()
249− -- Nothing ->
218+ validateUnion ss r = case (ss,r) of
250− -- fail "impossible to produce subUnion, please report this as a bug"
219+ (SCons s _, This v) -> validateJsonRepr s dpath v
251− -- Just x -> do
220+ (SCons _ stl, That r') -> validateUnion stl r'
252− -- let
221+ (SNil,_) -> fail "Invalid union. Please report this as a bug"
253− -- JSONPathText path = demotedPathToText dpath
255− -- SNil -> void $ vWarning $ mmSingleton path
256− -- $ pure "union handling error, please report this as bug"
258− -- validateJsonRepr (SSchemaUnion (SCons s' stl')) dpath
259− -- $ toUnion (SCons s' stl') x
260− -- Just x -> validateJsonRepr s dpath x
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
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
276− :: USubset s' (s ': ss) (RImage s' (s ': ss))
278− -> Union JsonRepr s'
279− -> JsonRepr ('SchemaUnion (s ': ss))
280−toUnion _ = ReprUnion . urelax
282−umatch' :: UElem a as i => Sing a -> Union f as -> Maybe (f a)
283−umatch' _ u = umatch u
285parseAndValidateJson
223parseAndValidateJson
286 :: forall schema
224 :: forall schema