@@ -10,7 +10,7 @@ module Lumi.Components.Form.Validation
10
10
, _Validated , _Fresh , _Modified
11
11
, setFresh , setModified
12
12
, ModifyValidated (..)
13
- , ValidatedNewtype (..), _ValidatedNewtype
13
+ , ModifyValidatedProxy (..), class CustomModifyValidated , customModifyValidated
14
14
, class CanValidate , fresh , modified , fromValidated
15
15
, validated
16
16
, warn
@@ -26,8 +26,10 @@ import Data.Either (Either(..), either, hush, note)
26
26
import Data.Enum (toEnum )
27
27
import Data.Eq (class Eq1 )
28
28
import Data.Foldable (foldMap )
29
+ import Data.Function (on )
30
+ import Data.Generic.Rep (class Generic , from , to )
29
31
import Data.Int as Int
30
- import Data.Lens (Lens , Prism' , Iso' , lens , over , prism' , review , view )
32
+ import Data.Lens (Lens , Prism' , lens , over , prism' , review , view )
31
33
import Data.Lens.Iso.Newtype (_Newtype )
32
34
import Data.Maybe (Maybe (..))
33
35
import Data.Monoid (guard )
@@ -40,6 +42,7 @@ import Data.String.NonEmpty (NonEmptyString)
40
42
import Data.String.NonEmpty (fromString ) as NES
41
43
import Data.String.Pattern (Pattern (..))
42
44
import Data.Traversable (traverse )
45
+ import Foreign.Generic (class Decode , class Encode , decode , encode )
43
46
import Heterogeneous.Mapping (class MapRecordWithIndex , class Mapping , ConstMapping , hmap , mapping )
44
47
import Lumi.Components.Column (column )
45
48
import Lumi.Components.Form.Internal (Forest , FormBuilder , FormBuilder' (..), Tree (..))
@@ -160,6 +163,18 @@ instance applyValidated :: Apply Validated where
160
163
instance applicativeValidated :: Applicative Validated where
161
164
pure = Fresh
162
165
166
+ instance genericValidated :: Generic value rep => Generic (Validated value ) rep where
167
+ to = Fresh <<< to
168
+ from (Fresh value) = from value
169
+ from (Modified value) = from value
170
+
171
+ instance decodeValidated :: Decode value => Decode (Validated value ) where
172
+ decode value = Fresh <$> decode value
173
+
174
+ instance encodeValidated :: Encode value => Encode (Validated value ) where
175
+ encode (Fresh value) = encode value
176
+ encode (Modified value) = encode value
177
+
163
178
-- | Lens for viewing and modifying `Validated` values.
164
179
_Validated :: forall a b . Lens (Validated a ) (Validated b ) a b
165
180
_Validated = flip lens ($>) $
@@ -203,12 +218,31 @@ setModified = mapping (ModifyValidated (Modified <<< view _Validated))
203
218
-- | records containing `Validated` values.
204
219
newtype ModifyValidated = ModifyValidated (Validated ~> Validated )
205
220
206
- newtype ValidatedNewtype a = ValidatedNewtype a
221
+ newtype ModifyValidatedProxy a = ModifyValidatedProxy a
222
+
223
+ unModifyValidatedProxy :: forall value . ModifyValidatedProxy value -> value
224
+ unModifyValidatedProxy (ModifyValidatedProxy value) = value
225
+
226
+ derive instance ntMVP :: Newtype (ModifyValidatedProxy a ) _
207
227
208
- derive instance ntMVP :: Newtype (ValidatedNewtype a ) _
228
+ instance eqValidatedNewtype :: Eq value => Eq (ModifyValidatedProxy value ) where
229
+ eq = eq `on` unModifyValidatedProxy
209
230
210
- _ValidatedNewtype :: forall s a . Newtype s a => Iso' (ValidatedNewtype s ) a
211
- _ValidatedNewtype = _Newtype <<< _Newtype
231
+ instance ordValidatedNewtype :: Ord value => Ord (ModifyValidatedProxy value ) where
232
+ compare = compare `on` unModifyValidatedProxy
233
+
234
+ instance genericValidatedNewtype :: Generic value rep => Generic (ModifyValidatedProxy value ) rep where
235
+ to = ModifyValidatedProxy <<< to
236
+ from = from <<< unModifyValidatedProxy
237
+
238
+ instance decodeValidatedNewtype :: Decode value => Decode (ModifyValidatedProxy value ) where
239
+ decode value = ModifyValidatedProxy <$> decode value
240
+
241
+ instance encodeValidatedNewtype :: Encode value => Encode (ModifyValidatedProxy value ) where
242
+ encode (ModifyValidatedProxy value) = encode value
243
+
244
+ class CustomModifyValidated a where
245
+ customModifyValidated :: ModifyValidated -> a -> a
212
246
213
247
instance modifyValidated :: Mapping ModifyValidated a a => Mapping ModifyValidated (Validated a ) (Validated a ) where
214
248
mapping m@(ModifyValidated f) = over _Validated (mapping m) <<< f
@@ -219,8 +253,10 @@ else instance modifyValidatedRecord ::
219
253
mapping d = hmap d
220
254
else instance modifyValidatedArray :: Mapping ModifyValidated a a => Mapping ModifyValidated (Array a ) (Array a ) where
221
255
mapping d = map (mapping d)
222
- else instance modifyValidatedNewtype :: (Newtype a b , Mapping ModifyValidated b b ) => Mapping ModifyValidated (ValidatedNewtype a ) (ValidatedNewtype a ) where
223
- mapping d = over (_Newtype <<< _Newtype) (mapping d)
256
+ else instance modifyValidatedMaybe :: Mapping ModifyValidated a a => Mapping ModifyValidated (Maybe a ) (Maybe a ) where
257
+ mapping d = map (mapping d)
258
+ else instance modifyValidatedProxy :: (CustomModifyValidated a , Mapping ModifyValidated a a ) => Mapping ModifyValidated (ModifyValidatedProxy a ) (ModifyValidatedProxy a ) where
259
+ mapping f = over _Newtype (customModifyValidated f)
224
260
else instance modifyValidatedIdentity :: Mapping ModifyValidated a a where
225
261
mapping _ = identity
226
262
0 commit comments