@@ -3,6 +3,7 @@ module Data.Schematic.Validation where
33import Control.Monad
44import Control.Monad.Validation
55import Data.Aeson
6+import Data.Aeson.Internal
67import Data.Aeson.Types
78import Data.Foldable
89import Data.Functor.Identity
@@ -49,11 +50,11 @@ isValidationError (ValidationError _) = True
4950isValidationError _ = False
5051
5152validateTextConstraint
52− :: JSONPath
53+ :: JSONPathText
5354 -> Text
5455 -> Sing (tcs :: TextConstraint)
5556 -> Validation ()
56−validateTextConstraint (JSONPath path) t = \case
57+validateTextConstraint (JSONPathText path) t = \case
5758 STEq n -> do
5859 let
5960 nlen = withKnownNat n $ natVal n
@@ -106,11 +107,11 @@ validateTextConstraint (JSONPath path) t = \case
106107 unless (matching ss) warn
107108
108109validateNumberConstraint
109− :: JSONPath
110+ :: JSONPathText
110111 -> Scientific
111112 -> Sing (tcs :: NumberConstraint)
112113 -> Validation ()
113−validateNumberConstraint (JSONPath path) num = \case
114+validateNumberConstraint (JSONPathText path) num = \case
114115 SNEq n -> do
115116 let
116117 nlen = withKnownNat n $ natVal n
@@ -148,11 +149,11 @@ validateNumberConstraint (JSONPath path) num = \case
148149 unless predicate warn
149150
150151validateArrayConstraint
151− :: JSONPath
152+ :: JSONPathText
152153 -> V.Vector a
153154 -> Sing (tcs :: ArrayConstraint)
154155 -> Validation ()
155−validateArrayConstraint (JSONPath path) v = \case
156+validateArrayConstraint (JSONPathText path) v = \case
156157 SAEq n -> do
157158 let
158159 nlen = withKnownNat n $ natVal n
@@ -160,10 +161,38 @@ validateArrayConstraint (JSONPath path) v = \case
160161 errMsg = "length should be == " <> T.pack (show nlen)
161162 warn = vWarning $ mmSingleton path (pure errMsg)
162163 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
163192
164193validateJsonRepr
165194 :: Sing schema
166− -> [DemotedPathSegment]
195+ -> JSONPath
167196 -> JsonRepr schema
168197 -> Validation ()
169198validateJsonRepr sschema dpath jr = case jr of
@@ -197,7 +226,7 @@ validateJsonRepr sschema dpath jr = case jr of
197226 process cs
198227 process acs
199228 for_ (V.indexed v) $ \(ix, jr') -> do
200− let newPath = dpath <> pure (DIx $ fromIntegral ix)
229+ let newPath = dpath <> pure (Index $ fromIntegral ix)
201230 validateJsonRepr s newPath jr'
202231 ReprOptional d -> case sschema of
203232 SSchemaOptional ss -> case d of
@@ -209,7 +238,7 @@ validateJsonRepr sschema dpath jr = case jr of
209238 go :: Rec FieldRepr (ts :: [(Symbol, Schema)] ) -> Validation ()
210239 go RNil = pure ()
211240 go (f@(FieldRepr d) :& ftl) = do
212− let newPath = dpath <> [DKey (knownFieldName f)]
241+ let newPath = dpath <> [Key (knownFieldName f)]
213242 validateJsonRepr (knownFieldSchema f) newPath d
214243 go ftl
215244 ReprUnion _ -> pure () -- FIXME
@@ -221,7 +250,7 @@ validateJsonRepr sschema dpath jr = case jr of
221250 -- fail "impossible to produce subUnion, please report this as a bug"
222251 -- Just x -> do
223252 -- let
224− -- JSONPath path = demotedPathToText dpath
253+ -- JSONPathText path = demotedPathToText dpath
225254 -- case stl of
226255 -- SNil -> void $ vWarning $ mmSingleton path
227256 -- $ pure "union handling error, please report this as bug"