From b6d03955bc72c1b44c1ac96a23565011f9ba985d Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Sat, 25 Jun 2022 15:38:15 -0700 Subject: [PATCH] Fix test failure. --- src/Validation.elm | 12 +++++++++++- tests/FormTests.elm | 39 +++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/Validation.elm b/src/Validation.elm index ccdc850a..495e3b40 100644 --- a/src/Validation.elm +++ b/src/Validation.elm @@ -1,4 +1,4 @@ -module Validation exposing (Validation, andMap, andThen, fail, fromMaybe, fromResult, map, map2, succeed, withField) +module Validation exposing (Validation, andMap, andThen, fail, fromMaybe, fromResult, map, map2, parseWithError, succeed, withError, withField) import Dict exposing (Dict) @@ -12,11 +12,21 @@ succeed parsed = ( Just parsed, Dict.empty ) +parseWithError : parsed -> ( String, error ) -> Validation error parsed +parseWithError parsed ( key, error ) = + ( Just parsed, Dict.singleton key [ error ] ) + + fail : String -> error -> Validation error parsed fail key parsed = ( Nothing, Dict.singleton key [ parsed ] ) +withError : String -> error -> Validation error parsed -> Validation error parsed +withError key error ( maybeParsedA, errorsA ) = + ( maybeParsedA, errorsA |> insertIfNonempty key [ error ] ) + + map : (parsed -> mapped) -> Validation error parsed -> Validation error mapped map mapFn ( maybeParsedA, errorsA ) = ( Maybe.map mapFn maybeParsedA, errorsA ) diff --git a/tests/FormTests.elm b/tests/FormTests.elm index 0309d77e..4c312317 100644 --- a/tests/FormTests.elm +++ b/tests/FormTests.elm @@ -156,18 +156,19 @@ all = ] , describe "dependent validations" <| let - --checkinFormParser : Form.HtmlForm String ( Date, Date ) data msg - checkinFormParser : Form.Form String ( Maybe ( Date, Date ), Dict String (List String) ) data (Form.Context String data -> MyView) + checkinFormParser : Form String (Validation String ( Date, Date )) data (Form.Context String data -> MyView) checkinFormParser = Form.init (\checkin checkout -> Validation.succeed (\checkinValue checkoutValue -> - if Date.toRataDie checkinValue >= Date.toRataDie checkoutValue then - Validation.fail checkin.name "Must be before checkout" + Validation.succeed ( checkinValue, checkoutValue ) + |> (if Date.toRataDie checkinValue >= Date.toRataDie checkoutValue then + Validation.withError checkin.name "Must be before checkout" - else - Validation.succeed ( checkinValue, checkoutValue ) + else + identity + ) ) |> Validation.withField checkin |> Validation.withField checkout @@ -203,7 +204,6 @@ all = checkinFormParser |> Expect.equal ( Just ( Date.fromRataDie 738158, Date.fromRataDie 738156 ) - --( Just (Date.fromRataDie 738158) , Dict.fromList [ ( "checkin", [ "Must be before checkout" ] ) ] @@ -219,20 +219,26 @@ all = (Form.init (\postForm_ -> postForm_ () - -- TODO @@@@ remove Tuple.first - |> Tuple.first + -- TODO simplify + |> Tuple.mapFirst Just + |> Validation.andThen identity ) (\formState postForm_ -> ( [], [ Div ] )) |> Form.dynamic (\() -> Form.init (\password passwordConfirmation -> - if password.value == passwordConfirmation.value then - Form.ok password.value + Validation.succeed + (\passwordValue passwordConfirmationValue -> + if passwordValue == passwordConfirmationValue then + Validation.succeed { password = passwordValue } - else - --Form.ok password.value|> - Form.fail passwordConfirmation "Must match password" + else + Validation.fail passwordConfirmation.name "Must match password" + ) + |> Validation.withField password + |> Validation.withField passwordConfirmation + |> Validation.andThen identity ) (\formState password passwordConfirmation -> [ Div ]) |> Form.field "password" (Field.text |> Field.password |> Field.required "Required") @@ -289,8 +295,9 @@ all = |> Validation.andThen (\kindValue -> postForm_ kindValue - -- TODO @@@@@ remove Tuple.first - |> Tuple.first + ---- TODO simplify + |> Tuple.mapFirst Just + |> Validation.andThen identity ) ) (\fieldErrors kind postForm_ ->