Add withFallback helper.

This commit is contained in:
Dillon Kearns 2022-06-27 13:25:06 -07:00
parent d93bb49e89
commit dfe46878af

View File

@ -1,4 +1,4 @@
module Validation exposing (Validation(..), andMap, andThen, fail, fromMaybe, fromResult, map, map2, parseWithError, succeed, withError, withErrorIf, withField)
module Validation exposing (Validation(..), andMap, andThen, fail, fromMaybe, fromResult, map, map2, parseWithError, succeed, withError, withErrorIf, withFallback, withField)
import Dict exposing (Dict)
@ -12,6 +12,16 @@ succeed parsed =
Validation ( Just parsed, Dict.empty )
withFallback : parsed -> Validation error parsed -> Validation error parsed
withFallback parsed (Validation ( maybeParsed, errors )) =
Validation
( maybeParsed
|> Maybe.withDefault parsed
|> Just
, errors
)
parseWithError : parsed -> ( String, error ) -> Validation error parsed
parseWithError parsed ( key, error ) =
Validation ( Just parsed, Dict.singleton key [ error ] )