This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0 1.0.1
README Field

Field.ValidationResult

Definition

type ValidationResult e v = Passed v | Failed e

A ValidationResult describes the state of a Field once it has been validated. It can contain the value of the field if the validation Passed or an error if the validation Failed

Mapping

map : (a -> b) -> ValidationResult x a -> ValidationResult x b

Apply a function to a ValidationResult. If the ValidationResult is Passed, it will be converted. if the ValidationResult has Failed then the failure will propogate

contraMap : (a -> b) -> ValidationResult a x -> ValidationResult b x

Apply a function to a ValidationResult. If the ValidationResult is Failed, it will be converted. if the ValidationResult has Passed then the passed state will propogate

Chaining

andThen : (v -> ValidationResult e v) -> ValidationResult e v -> ValidationResult e v

Chain multiple validation functions

Helpers

filterFailures : List (ValidationResult e v) -> List (ValidationResult e v)

Get all ValidationResults that Failed

filterPassed : List (ValidationResult e v) -> List (ValidationResult e v)

Get all ValidationResults that Passed

module Field.ValidationResult exposing (ValidationResult(..), map, contraMap, andThen, filterFailures, filterPassed)

{-|


# Definition

@docs ValidationResult


# Mapping

@docs map, contraMap


# Chaining

@docs andThen


# Helpers

@docs filterFailures, filterPassed

-}


{-| A `ValidationResult` describes the state of a `Field` once it has been validated.
It can contain the value of the field if the validation `Passed` or
an error if the validation `Failed`
-}
type ValidationResult e v
    = Passed v
    | Failed e


{-| Apply a function to a `ValidationResult`. If the `ValidationResult` is `Passed`, it will be converted.
if the `ValidationResult` has `Failed` then the failure will propogate
-}
map : (a -> b) -> ValidationResult x a -> ValidationResult x b
map fn v =
    case v of
        Failed err ->
            Failed err

        Passed val ->
            Passed <| fn val


{-| Apply a function to a `ValidationResult`. If the `ValidationResult` is `Failed`, it will be converted.
if the `ValidationResult` has `Passed` then the passed state will propogate
-}
contraMap : (a -> b) -> ValidationResult a x -> ValidationResult b x
contraMap fn e =
    case e of
        Failed err ->
            Failed <| fn err

        Passed val ->
            Passed val


{-| Chain multiple validation functions
-}
andThen : (v -> ValidationResult e v) -> ValidationResult e v -> ValidationResult e v
andThen fn t =
    case t of
        Failed err ->
            Failed err

        Passed v ->
            fn v


{-| Get all ValidationResults that Failed
-}
filterFailures : List (ValidationResult e v) -> List (ValidationResult e v)
filterFailures =
    List.filter
        (\v ->
            case v of
                Failed _ ->
                    True

                Passed _ ->
                    False
        )


{-| Get all ValidationResults that Passed
-}
filterPassed : List (ValidationResult e v) -> List (ValidationResult e v)
filterPassed =
    List.filter
        (\v ->
            case v of
                Failed _ ->
                    False

                Passed _ ->
                    True
        )