Fix test failure.

This commit is contained in:
Dillon Kearns 2022-06-25 15:38:15 -07:00
parent 669bc2e53f
commit b6d03955bc
2 changed files with 34 additions and 17 deletions

View File

@ -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) import Dict exposing (Dict)
@ -12,11 +12,21 @@ succeed parsed =
( Just parsed, Dict.empty ) ( 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 : String -> error -> Validation error parsed
fail key parsed = fail key parsed =
( Nothing, Dict.singleton 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 : (parsed -> mapped) -> Validation error parsed -> Validation error mapped
map mapFn ( maybeParsedA, errorsA ) = map mapFn ( maybeParsedA, errorsA ) =
( Maybe.map mapFn maybeParsedA, errorsA ) ( Maybe.map mapFn maybeParsedA, errorsA )

View File

@ -156,18 +156,19 @@ all =
] ]
, describe "dependent validations" <| , describe "dependent validations" <|
let let
--checkinFormParser : Form.HtmlForm String ( Date, Date ) data msg checkinFormParser : Form String (Validation String ( Date, Date )) data (Form.Context String data -> MyView)
checkinFormParser : Form.Form String ( Maybe ( Date, Date ), Dict String (List String) ) data (Form.Context String data -> MyView)
checkinFormParser = checkinFormParser =
Form.init Form.init
(\checkin checkout -> (\checkin checkout ->
Validation.succeed Validation.succeed
(\checkinValue checkoutValue -> (\checkinValue checkoutValue ->
if Date.toRataDie checkinValue >= Date.toRataDie checkoutValue then Validation.succeed ( checkinValue, checkoutValue )
Validation.fail checkin.name "Must be before checkout" |> (if Date.toRataDie checkinValue >= Date.toRataDie checkoutValue then
Validation.withError checkin.name "Must be before checkout"
else else
Validation.succeed ( checkinValue, checkoutValue ) identity
)
) )
|> Validation.withField checkin |> Validation.withField checkin
|> Validation.withField checkout |> Validation.withField checkout
@ -203,7 +204,6 @@ all =
checkinFormParser checkinFormParser
|> Expect.equal |> Expect.equal
( Just ( Date.fromRataDie 738158, Date.fromRataDie 738156 ) ( Just ( Date.fromRataDie 738158, Date.fromRataDie 738156 )
--( Just (Date.fromRataDie 738158)
, Dict.fromList , Dict.fromList
[ ( "checkin", [ "Must be before checkout" ] ) [ ( "checkin", [ "Must be before checkout" ] )
] ]
@ -219,20 +219,26 @@ all =
(Form.init (Form.init
(\postForm_ -> (\postForm_ ->
postForm_ () postForm_ ()
-- TODO @@@@ remove Tuple.first -- TODO simplify
|> Tuple.first |> Tuple.mapFirst Just
|> Validation.andThen identity
) )
(\formState postForm_ -> ( [], [ Div ] )) (\formState postForm_ -> ( [], [ Div ] ))
|> Form.dynamic |> Form.dynamic
(\() -> (\() ->
Form.init Form.init
(\password passwordConfirmation -> (\password passwordConfirmation ->
if password.value == passwordConfirmation.value then Validation.succeed
Form.ok password.value (\passwordValue passwordConfirmationValue ->
if passwordValue == passwordConfirmationValue then
Validation.succeed { password = passwordValue }
else else
--Form.ok password.value|> Validation.fail passwordConfirmation.name "Must match password"
Form.fail passwordConfirmation "Must match password" )
|> Validation.withField password
|> Validation.withField passwordConfirmation
|> Validation.andThen identity
) )
(\formState password passwordConfirmation -> [ Div ]) (\formState password passwordConfirmation -> [ Div ])
|> Form.field "password" (Field.text |> Field.password |> Field.required "Required") |> Form.field "password" (Field.text |> Field.password |> Field.required "Required")
@ -289,8 +295,9 @@ all =
|> Validation.andThen |> Validation.andThen
(\kindValue -> (\kindValue ->
postForm_ kindValue postForm_ kindValue
-- TODO @@@@@ remove Tuple.first ---- TODO simplify
|> Tuple.first |> Tuple.mapFirst Just
|> Validation.andThen identity
) )
) )
(\fieldErrors kind postForm_ -> (\fieldErrors kind postForm_ ->