Skip to content

Commit

Permalink
provide "field" and "toNonEmptyString" out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
choonkeat committed May 7, 2022
1 parent 8104987 commit 7eedba7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 45 deletions.
54 changes: 9 additions & 45 deletions example/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -533,51 +533,15 @@ parseDontValidate tz list =
NativeForm.valuesDict list
in
Ok ParsedInfo
|> field "myselect" (toRating (Dict.get "myselect" dict))
|> field "myselectmulti" (toCharacteristics (Dict.get "myselectmulti" dict))
|> field "mycheckbox" (toHobbies (Dict.get "mycheckbox" dict))
|> field "mytext" (toNonEmptyString (Dict.get "mytext" dict))
|> field "mynumber" (toInt (Dict.get "mynumber" dict))
|> field "myurl" (toUrl (Dict.get "myurl" dict))
|> field "mycolor" (toColor (Dict.get "mycolor" dict))
|> field "mydate" (toTimePosix TypeDate tz (Dict.get "mydate" dict))
|> field "mydatetime-local" (toTimePosix TypeDateTimeLocal tz (Dict.get "mydatetime-local" dict))


{-| Pipe friendly builder of values that accumulates errors
-}
field :
comparable
-> Result err a
-> Result (Dict comparable err) (a -> b)
-> Result (Dict comparable err) b
field k newresult result =
case ( result, newresult ) of
( Err errs, Err newerrs ) ->
Err (Dict.insert k newerrs errs)

( Ok _, Err newerrs ) ->
Err (Dict.fromList [ ( k, newerrs ) ])

( Err errs, Ok _ ) ->
Err errs

( Ok res, Ok a ) ->
Ok (res a)


toNonEmptyString : Maybe (NativeForm.Value String) -> Result String String
toNonEmptyString maybeV =
maybeV
|> Maybe.map (NativeForm.oneWithDefault "")
|> Maybe.withDefault ""
|> (\str ->
if String.isEmpty str then
Err "cannot be empty"

else
Ok str
)
|> NativeForm.field "myselect" (toRating (Dict.get "myselect" dict))
|> NativeForm.field "myselectmulti" (toCharacteristics (Dict.get "myselectmulti" dict))
|> NativeForm.field "mycheckbox" (toHobbies (Dict.get "mycheckbox" dict))
|> NativeForm.field "mytext" (NativeForm.toNonEmptyString (Dict.get "mytext" dict))
|> NativeForm.field "mynumber" (toInt (Dict.get "mynumber" dict))
|> NativeForm.field "myurl" (toUrl (Dict.get "myurl" dict))
|> NativeForm.field "mycolor" (toColor (Dict.get "mycolor" dict))
|> NativeForm.field "mydate" (toTimePosix TypeDate tz (Dict.get "mydate" dict))
|> NativeForm.field "mydatetime-local" (toTimePosix TypeDateTimeLocal tz (Dict.get "mydatetime-local" dict))


toInt : Maybe (NativeForm.Value String) -> Result String (Maybe Int)
Expand Down
69 changes: 69 additions & 0 deletions src/NativeForm.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module NativeForm exposing
, valuesDict, valuesAppend
, oneMap, oneWithDefault
, manyMap, manyWithDefault
, field, toNonEmptyString
)

{-|
Expand Down Expand Up @@ -42,6 +43,13 @@ Example usage
@docs manyMap, manyWithDefault
## Parsing form values into desired types
@docs field, toNonEmptyString
See [example/src/Main.elm](https://github.com/choonkeat/nativeform/blob/main/example/src/Main.elm) for more functions
-}

import Dict exposing (Dict)
Expand Down Expand Up @@ -402,3 +410,64 @@ decodeInput =
Json.Decode.map2 Tuple.pair
(Json.Decode.field "name" Json.Decode.string)
(Json.Decode.field "value" Json.Decode.string)



--


{-| Pipe friendly builder of values that accumulates errors. Useful for writing
your `parseDontValidate` functions
parseDontValidate : Time.Zone -> List ( String, NativeForm.Value String ) -> Result Errors ParsedInfo
parseDontValidate tz list =
let
dict =
NativeForm.valuesDict list
in
Ok ParsedInfo
|> field "myselect" (toRating (Dict.get "myselect" dict))
|> field "myselectmulti" (toCharacteristics (Dict.get "myselectmulti" dict))
|> field "mycheckbox" (toHobbies (Dict.get "mycheckbox" dict))
|> field "mytext" (toNonEmptyString (Dict.get "mytext" dict))
|> field "mynumber" (toInt (Dict.get "mynumber" dict))
|> field "myurl" (toUrl (Dict.get "myurl" dict))
|> field "mycolor" (toColor (Dict.get "mycolor" dict))
|> field "mydate" (toTimePosix TypeDate tz (Dict.get "mydate" dict))
|> field "mydatetime-local" (toTimePosix TypeDateTimeLocal tz (Dict.get "mydatetime-local" dict))
-}
field :
comparable
-> Result err a
-> Result (Dict comparable err) (a -> b)
-> Result (Dict comparable err) b
field k newresult result =
case ( result, newresult ) of
( Err errs, Err newerrs ) ->
Err (Dict.insert k newerrs errs)

( Ok _, Err newerrs ) ->
Err (Dict.fromList [ ( k, newerrs ) ])

( Err errs, Ok _ ) ->
Err errs

( Ok res, Ok a ) ->
Ok (res a)


{-| parse a form field value into a String
-}
toNonEmptyString : Maybe (Value String) -> Result String String
toNonEmptyString maybeV =
maybeV
|> Maybe.map (oneWithDefault "")
|> Maybe.withDefault ""
|> (\str ->
if String.isEmpty str then
Err "cannot be empty"

else
Ok (String.trim str)
)

0 comments on commit 7eedba7

Please sign in to comment.