From 4f5bbce44283784db607d59e9feaf2c424dc213b Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 8 Jun 2022 07:23:21 -0700 Subject: [PATCH] Parse form data into more structure. --- src/Pages/FormParser.elm | 60 ++++++++++++++++++++++++++--- tests/FormParserTests.elm | 79 +++++++++++++++++++++++++++++++-------- 2 files changed, 117 insertions(+), 22 deletions(-) diff --git a/src/Pages/FormParser.elm b/src/Pages/FormParser.elm index 614d59b1..c31fc9ec 100644 --- a/src/Pages/FormParser.elm +++ b/src/Pages/FormParser.elm @@ -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 -> () diff --git a/tests/FormParserTests.elm b/tests/FormParserTests.elm index dd594ed7..1db90836 100644 --- a/tests/FormParserTests.elm +++ b/tests/FormParserTests.elm @@ -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" ]