mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-25 09:21:57 +03:00
Parse form data into more structure.
This commit is contained in:
parent
33183024ad
commit
4f5bbce442
@ -1,6 +1,8 @@
|
||||
module Pages.FormParser exposing (..)
|
||||
|
||||
import Dict exposing (Dict)
|
||||
import Html
|
||||
import Html.Attributes as Attr
|
||||
import Pages.Form as Form
|
||||
|
||||
|
||||
@ -30,8 +32,42 @@ init =
|
||||
|
||||
string : error -> FieldThing error String
|
||||
string error =
|
||||
--Debug.todo ""
|
||||
FieldThing (\formState -> ( Just "TODO real value", [] ))
|
||||
FieldThing
|
||||
(\fieldName formState ->
|
||||
let
|
||||
rawValue : Maybe String
|
||||
rawValue =
|
||||
formState
|
||||
|> Dict.get fieldName
|
||||
|> Maybe.map .value
|
||||
in
|
||||
( rawValue
|
||||
, []
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
requiredString : error -> FieldThing error String
|
||||
requiredString error =
|
||||
FieldThing
|
||||
(\fieldName formState ->
|
||||
let
|
||||
rawValue : Maybe String
|
||||
rawValue =
|
||||
formState
|
||||
|> Dict.get fieldName
|
||||
|> Maybe.map .value
|
||||
in
|
||||
if rawValue == Just "" || rawValue == Nothing then
|
||||
( Nothing
|
||||
, [ error ]
|
||||
)
|
||||
|
||||
else
|
||||
( rawValue
|
||||
, []
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
andThenNew : a -> CombinedParser String a
|
||||
@ -69,7 +105,7 @@ field name (FieldThing fieldParser) (CombinedParser definitions parseFn) =
|
||||
let
|
||||
--something : ( Maybe parsed, List error )
|
||||
( maybeParsed, errors ) =
|
||||
fieldParser formState
|
||||
fieldParser name formState
|
||||
|
||||
parsedField : Maybe (ParsedField error parsed)
|
||||
parsedField =
|
||||
@ -140,6 +176,18 @@ type CompleteParser error parsed
|
||||
= CompleteParser
|
||||
|
||||
|
||||
input attrs fieldThing =
|
||||
Html.input
|
||||
(attrs
|
||||
++ [ Attr.value "TODO"
|
||||
|
||||
-- TODO provide a way to get rawValue
|
||||
--fieldThing.rawValue
|
||||
]
|
||||
)
|
||||
[]
|
||||
|
||||
|
||||
runNew : Form.FormState -> CombinedParser error parsed -> ( Maybe parsed, Dict String (List error) )
|
||||
runNew formState (CombinedParser fieldDefinitions parser) =
|
||||
--Debug.todo ""
|
||||
@ -159,7 +207,7 @@ type CombinedParser error parsed
|
||||
|
||||
|
||||
type FieldThing error parsed
|
||||
= FieldThing (Form.FormState -> ( Maybe parsed, List error ))
|
||||
= FieldThing (String -> Form.FormState -> ( Maybe parsed, List error ))
|
||||
|
||||
|
||||
type FieldDefinition
|
||||
@ -217,8 +265,8 @@ value =
|
||||
-- FullFieldThing { name = "TODO" } (\_ -> okValue)
|
||||
|
||||
|
||||
ok =
|
||||
()
|
||||
ok result =
|
||||
result
|
||||
|
||||
|
||||
withError : error -> ParsedField error parsed -> ()
|
||||
|
@ -26,54 +26,101 @@ type Action
|
||||
all : Test
|
||||
all =
|
||||
describe "Form Parser"
|
||||
[ test "new design idea with errors" <|
|
||||
[ --test "new design idea with errors" <|
|
||||
-- \() ->
|
||||
-- FormParser.runNew
|
||||
-- (fields
|
||||
-- [ ( "password", "mypassword" )
|
||||
-- , ( "password-confirmation", "my-password" )
|
||||
-- ]
|
||||
-- )
|
||||
-- (FormParser.andThenNew
|
||||
-- (\password passwordConfirmation ->
|
||||
-- if password.value == passwordConfirmation.value then
|
||||
-- passwordConfirmation |> FormParser.withError "Must match password"
|
||||
--
|
||||
-- else
|
||||
-- FormParser.ok
|
||||
-- )
|
||||
-- |> FormParser.field "password" (FormParser.string "Password is required")
|
||||
-- |> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
||||
-- )
|
||||
-- |> Expect.equal
|
||||
-- ( Nothing
|
||||
-- , Dict.fromList
|
||||
-- [ ( "password-confirmation", [ "Must match password" ] )
|
||||
-- ]
|
||||
-- )
|
||||
test "non-dependent field error" <|
|
||||
\() ->
|
||||
FormParser.runNew
|
||||
(fields
|
||||
[ ( "password", "mypassword" )
|
||||
, ( "password-confirmation", "my-password" )
|
||||
, ( "password-confirmation", "" )
|
||||
]
|
||||
)
|
||||
(FormParser.andThenNew
|
||||
(\password passwordConfirmation ->
|
||||
if password.value == passwordConfirmation.value then
|
||||
passwordConfirmation |> FormParser.withError "Must match password"
|
||||
--passwordConfirmation |> FormParser.withError "Must match password"
|
||||
Debug.todo ""
|
||||
|
||||
else
|
||||
FormParser.ok
|
||||
FormParser.ok { password = password }
|
||||
)
|
||||
|> FormParser.field "password" (FormParser.string "Password is required")
|
||||
|> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
||||
|> FormParser.field "password" (FormParser.requiredString "Password is required")
|
||||
|> FormParser.field "password-confirmation" (FormParser.requiredString "Password confirmation is required")
|
||||
)
|
||||
|> Expect.equal
|
||||
( Nothing
|
||||
, Dict.fromList
|
||||
[ ( "password-confirmation", [ "Must match password" ] )
|
||||
[ ( "password", [] )
|
||||
, ( "password-confirmation", [ "Password confirmation is required" ] )
|
||||
]
|
||||
)
|
||||
, test "new design idea no errors" <|
|
||||
, test "new design idea 3" <|
|
||||
\() ->
|
||||
FormParser.runNew
|
||||
(fields
|
||||
[ ( "password", "mypassword" )
|
||||
, ( "password-confirmation", "my-password" )
|
||||
, ( "password-confirmation", "mypassword" )
|
||||
]
|
||||
)
|
||||
(FormParser.andThenNew
|
||||
(\password passwordConfirmation ->
|
||||
if password.value == passwordConfirmation.value then
|
||||
passwordConfirmation |> FormParser.withError "Must match password"
|
||||
--{
|
||||
--dependentErrors =
|
||||
if password.value /= passwordConfirmation.value then
|
||||
Debug.todo ""
|
||||
--passwordConfirmation |> FormParser.withError "Must match password"
|
||||
|
||||
else
|
||||
FormParser.ok
|
||||
{ password = password.value }
|
||||
--FormParser.ok
|
||||
--, view =
|
||||
-- Html.form []
|
||||
-- [ password |> FormParser.input []
|
||||
-- , passwordConfirmation |> FormParser.input []
|
||||
-- ]
|
||||
--}
|
||||
)
|
||||
|> FormParser.field "password" (FormParser.string "Password is required")
|
||||
|> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
||||
)
|
||||
|> Expect.equal
|
||||
( Just ()
|
||||
, Dict.fromList []
|
||||
)
|
||||
|> expectNoErrors { password = "mypassword" }
|
||||
]
|
||||
|
||||
|
||||
expectNoErrors : parsed -> ( Maybe parsed, Dict String (List error) ) -> Expect.Expectation
|
||||
expectNoErrors parsed =
|
||||
Expect.all
|
||||
[ Tuple.first
|
||||
>> Expect.equal
|
||||
(Just parsed)
|
||||
, Tuple.second
|
||||
>> Dict.values
|
||||
>> List.all List.isEmpty
|
||||
>> Expect.true "Expected no errors"
|
||||
]
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user