@@ -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
@@ -50,11 +49,11 @@ isValidationError (ValidationError _) = True
50isValidationError _ = False
49isValidationError _ = False
52validateTextConstraint
51validateTextConstraint
53− :: JSONPathText
52+ :: JSONPath
55 -> Sing (tcs :: TextConstraint)
54 -> Sing (tcs :: TextConstraint)
56 -> Validation ()
55 -> Validation ()
57−validateTextConstraint (JSONPathText path) t = \case
56+validateTextConstraint (JSONPath path) t = \case
58 STEq n -> do
57 STEq n -> do
60 nlen = withKnownNat n $ natVal n
59 nlen = withKnownNat n $ natVal n
@@ -107,11 +106,11 @@ validateTextConstraint (JSONPathText path) t = \case
107 unless (matching ss) warn
106 unless (matching ss) warn
109validateNumberConstraint
108validateNumberConstraint
110− :: JSONPathText
109+ :: JSONPath
111 -> Scientific
110 -> Scientific
112 -> Sing (tcs :: NumberConstraint)
111 -> Sing (tcs :: NumberConstraint)
113 -> Validation ()
112 -> Validation ()
114−validateNumberConstraint (JSONPathText path) num = \case
113+validateNumberConstraint (JSONPath path) num = \case
115 SNEq n -> do
114 SNEq n -> do
117 nlen = withKnownNat n $ natVal n
116 nlen = withKnownNat n $ natVal n
@@ -149,11 +148,11 @@ validateNumberConstraint (JSONPathText path) num = \case
149 unless predicate warn
148 unless predicate warn
151validateArrayConstraint
150validateArrayConstraint
152− :: JSONPathText
151+ :: JSONPath
153 -> V.Vector a
152 -> V.Vector a
154 -> Sing (tcs :: ArrayConstraint)
153 -> Sing (tcs :: ArrayConstraint)
155 -> Validation ()
154 -> Validation ()
156−validateArrayConstraint (JSONPathText path) v = \case
155+validateArrayConstraint (JSONPath path) v = \case
157 SAEq n -> do
156 SAEq n -> do
159 nlen = withKnownNat n $ natVal n
158 nlen = withKnownNat n $ natVal n
@@ -161,38 +160,10 @@ validateArrayConstraint (JSONPathText path) v = \case
161 errMsg = "length should be == " <> T.pack (show nlen)
160 errMsg = "length should be == " <> T.pack (show nlen)
162 warn = vWarning $ mmSingleton path (pure errMsg)
161 warn = vWarning $ mmSingleton path (pure errMsg)
163 unless predicate warn
162 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
164validateJsonRepr
194 :: Sing schema
165 :: Sing schema
195− -> JSONPath
166+ -> [DemotedPathSegment]
196 -> JsonRepr schema
167 -> JsonRepr schema
197 -> Validation ()
168 -> Validation ()
198validateJsonRepr sschema dpath jr = case jr of
169validateJsonRepr sschema dpath jr = case jr of
@@ -226,7 +197,7 @@ validateJsonRepr sschema dpath jr = case jr of
226 process cs
197 process cs
227 process acs
198 process acs
228 for_ (V.indexed v) $ \(ix, jr') -> do
199 for_ (V.indexed v) $ \(ix, jr') -> do
229− let newPath = dpath <> pure (Index $ fromIntegral ix)
200+ let newPath = dpath <> pure (DIx $ fromIntegral ix)
230 validateJsonRepr s newPath jr'
201 validateJsonRepr s newPath jr'
231 ReprOptional d -> case sschema of
202 ReprOptional d -> case sschema of
232 SSchemaOptional ss -> case d of
203 SSchemaOptional ss -> case d of
@@ -238,7 +209,7 @@ validateJsonRepr sschema dpath jr = case jr of
238 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
209 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
239 go RNil = pure ()
210 go RNil = pure ()
240 go (f@(FieldRepr d) :& ftl) = do
211 go (f@(FieldRepr d) :& ftl) = do
241− let newPath = dpath <> [Key (knownFieldName f)]
212+ let newPath = dpath <> [DKey (knownFieldName f)]
242 validateJsonRepr (knownFieldSchema f) newPath d
213 validateJsonRepr (knownFieldSchema f) newPath d
244 ReprUnion _ -> pure () -- FIXME
215 ReprUnion _ -> pure () -- FIXME
@@ -250,7 +221,7 @@ validateJsonRepr sschema dpath jr = case jr of
250 -- fail "impossible to produce subUnion, please report this as a bug"
221 -- fail "impossible to produce subUnion, please report this as a bug"
251 -- Just x -> do
222 -- Just x -> do
253− -- JSONPathText path = demotedPathToText dpath
224+ -- JSONPath path = demotedPathToText dpath
254 -- case stl of
225 -- case stl of
255 -- SNil -> void $ vWarning $ mmSingleton path
226 -- SNil -> void $ vWarning $ mmSingleton path
256 -- $ pure "union handling error, please report this as bug"
227 -- $ pure "union handling error, please report this as bug"