@@ -10,7 +10,7 @@ module Lumi.Components.Form.Validation
1010 , _Validated , _Fresh , _Modified
1111 , setFresh , setModified
1212 , ModifyValidated (..)
13- , ValidatedNewtype (..), _ValidatedNewtype
13+ , ModifyValidatedProxy (..), class CustomModifyValidated , customModifyValidated
1414 , class CanValidate , fresh , modified , fromValidated
1515 , validated
1616 , warn
@@ -26,8 +26,10 @@ import Data.Either (Either(..), either, hush, note)
2626import Data.Enum (toEnum )
2727import Data.Eq (class Eq1 )
2828import Data.Foldable (foldMap )
29+ import Data.Function (on )
30+ import Data.Generic.Rep (class Generic , from , to )
2931import 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 )
3133import Data.Lens.Iso.Newtype (_Newtype )
3234import Data.Maybe (Maybe (..))
3335import Data.Monoid (guard )
@@ -40,6 +42,7 @@ import Data.String.NonEmpty (NonEmptyString)
4042import Data.String.NonEmpty (fromString ) as NES
4143import Data.String.Pattern (Pattern (..))
4244import Data.Traversable (traverse )
45+ import Foreign.Generic (class Decode , class Encode , decode , encode )
4346import Heterogeneous.Mapping (class MapRecordWithIndex , class Mapping , ConstMapping , hmap , mapping )
4447import Lumi.Components.Column (column )
4548import Lumi.Components.Form.Internal (Forest , FormBuilder , FormBuilder' (..), Tree (..))
@@ -160,6 +163,18 @@ instance applyValidated :: Apply Validated where
160163instance applicativeValidated :: Applicative Validated where
161164 pure = Fresh
162165
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+
163178-- | Lens for viewing and modifying `Validated` values.
164179_Validated :: forall a b . Lens (Validated a ) (Validated b ) a b
165180_Validated = flip lens ($>) $
@@ -203,12 +218,31 @@ setModified = mapping (ModifyValidated (Modified <<< view _Validated))
203218-- | records containing `Validated` values.
204219newtype ModifyValidated = ModifyValidated (Validated ~> Validated )
205220
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 ) _
207227
208- derive instance ntMVP :: Newtype (ValidatedNewtype a ) _
228+ instance eqValidatedNewtype :: Eq value => Eq (ModifyValidatedProxy value ) where
229+ eq = eq `on` unModifyValidatedProxy
209230
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
212246
213247instance modifyValidated :: Mapping ModifyValidated a a => Mapping ModifyValidated (Validated a ) (Validated a ) where
214248 mapping m@(ModifyValidated f) = over _Validated (mapping m) <<< f
@@ -219,8 +253,10 @@ else instance modifyValidatedRecord ::
219253 mapping d = hmap d
220254else instance modifyValidatedArray :: Mapping ModifyValidated a a => Mapping ModifyValidated (Array a ) (Array a ) where
221255 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)
224260else instance modifyValidatedIdentity :: Mapping ModifyValidated a a where
225261 mapping _ = identity
226262
0 commit comments