Migrate some test cases.

This commit is contained in:
Dillon Kearns 2022-07-23 08:48:36 +02:00
parent 8a6bd84c11
commit 0e2ab10e50
2 changed files with 34 additions and 17 deletions

View File

@ -1,7 +1,7 @@
module Form.Validation exposing
( Validation, andMap, andThen, fail, fromMaybe, fromResult, map, map2, parseWithError, succeed, withError, withErrorIf, withFallback
, value
, fail2, fieldName, withError2
, fail2, fieldName, withError2, withErrorIf2
)
{-|
@ -97,6 +97,21 @@ withErrorIf includeError (Validation _ key _) error (Validation viewField name (
)
{-| -}
withErrorIf2 : Bool -> Validation error ignored field -> error -> Validation error parsed named -> Validation error parsed named
withErrorIf2 includeError (Validation _ key _) error (Validation viewField name ( maybeParsedA, errorsA )) =
-- TODO use something like { field : kind } for type variable to check that it represents a field
Validation viewField
name
( maybeParsedA
, if includeError then
errorsA |> insertIfNonempty (key |> Maybe.withDefault "") [ error ]
else
errorsA
)
--map : (parsed -> mapped) -> Validation error parsed named -> Validation error mapped named

View File

@ -23,26 +23,28 @@ all =
describe "Form Parser" <|
let
passwordConfirmationParser =
Form.init
Form.init2
(\password passwordConfirmation ->
Validation.succeed
(\passwordValue passwordConfirmationValue ->
Validation.succeed { password = passwordValue }
|> Validation.withErrorIf (passwordValue /= passwordConfirmationValue)
passwordConfirmation
"Must match password"
)
|> Validation.andMap password
|> Validation.andMap passwordConfirmation
|> Validation.andThen identity
{ combine =
Validation.succeed
(\passwordValue passwordConfirmationValue ->
Validation.succeed { password = passwordValue }
|> Validation.withErrorIf2 (passwordValue /= passwordConfirmationValue)
passwordConfirmation
"Must match password"
)
|> Validation.andMap password
|> Validation.andMap passwordConfirmation
|> Validation.andThen identity
, view = \info -> Div
}
)
(\_ _ _ -> Div)
|> Form.field "password" (Field.text |> Field.required "Password is required")
|> Form.field "password-confirmation" (Field.text |> Field.required "Password confirmation is required")
|> Form.field2 "password" (Field.text |> Field.required "Password is required")
|> Form.field2 "password-confirmation" (Field.text |> Field.required "Password confirmation is required")
in
[ test "matching password" <|
\() ->
Form.runServerSide
Form.runServerSide4
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "mypassword" )
@ -55,7 +57,7 @@ all =
)
, test "non-matching password" <|
\() ->
Form.runServerSide
Form.runServerSide4
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "doesnt-match-password" )