stoeffel/elm-verify/3.0.0

NOTE: this package is not compatible with Elm 0.19.1

Verify

Verify allows you to validate a model into a structure that makes forbidden states impossible.

type alias Validator error input result =
input -> Result (List error) result

This is just an alias for a function from an input to a result. The result is either:

  • contains a list of all errors that didn't satisfy the given spec.
  • contains an resulting structure.
ok : finally -> Validator error input finally

This allows you to lift any value into a validator. This is particularly useful to initialize a pipeline.

ok "always ok" Nothing
--> Ok "always ok"
fail : error -> Validator error input result

Allows you to create a failing Validator.

fail "always fail" Nothing
--> Err [ "always fail" ]
(bigger -> smaller)
-> Validator error smaller result
-> Validator error bigger (result -> finally)
-> Validator error bigger finally

Allows you to verify a part of a structure.

import Maybe.Verify exposing (isJust)




validator : Validator String { a | firstName : Maybe String } String
validator =
    Verify.ok identity
        |> verify .firstName (isJust "You need to provide a first name.")


validator { firstName = Nothing }
--> Err [ "You need to provide a first name." ]


validator { firstName = Just "Stöffel" }
--> Ok "Stöffel"
(bigger -> smaller)
-> Validator error bigger (smaller -> finally)
-> Validator error bigger finally

You can use keep if you want a value to be in the verified structure without any verification.

import Maybe.Verify exposing (isJust)




validator : Validator String { a | id : Int, firstName : Maybe String } (Int, String)
validator =
    Verify.ok (,)
        |> keep .id
        |> verify .firstName (isJust "You need to provide a first name.")




validator { id = 1, firstName = Nothing }
--> Err [ "You need to provide a first name." ]


validator { id = 1, firstName = Just "Stöffel" }
--> Ok (1, "Stöffel")
Validator error input result
-> Validator error input (result -> finally)
-> Validator error input finally

Sometimes the verification of a part only makes sense in a bigger context. This means your Validator needs access to the whole structure.

import Maybe.Verify exposing (isJust)




validator : Validator String { a | username : Maybe String, level: Int, strength: Int } (String, Int)
validator =
    Verify.ok (,)
        |> verify .username (isJust "You need to provide a username.")
        |> custom (\{level, strength} ->
            if strength > level then
                Err [ "Your strength can exceed your level." ]
            else
                Ok strength
            )




validator { username = Just "Ork1", level = 3, strength = 5 }
--> Err [ "Your strength can exceed your level." ]


validator { username = Just "Ork1", level = 6, strength = 5 }
--> Ok ("Ork1", 5)


validator { username = Nothing, level = 3, strength = 5 }
--> Err [ "You need to provide a username."
-->     , "Your strength can exceed your level."
-->     ]
Validator error result finally
-> Validator error input result
-> Validator error input finally

This allows you to combine multible Validators.

import Maybe.Verify exposing (isJust)
import String.Verify exposing (notBlank)




validator { firstName = Nothing }
--> Err [ "You need to provide a first name." ]


validator { firstName = Just "   " }
--> Err [  "You need to provide a none empty first name." ]


validator { firstName = Just "Stöffel" }
--> Ok "Stöffel"


validator : Validator String { a | firstName : Maybe String } String
validator =
    Verify.ok identity
        |> verify .firstName verifyName


verifyName : Validator String (Maybe String) String
verifyName =
    isJust "You need to provide a first name."
        |> andThen (notBlank "You need to provide a none empty first name.")
fromMaybe : (input -> Maybe result) -> error -> Validator error input result

This is a convenient function to create a Validator from a function that returns a maybe instead of a Result. It fails if the function returns a Nothing. This allows you to verify a input and return a result of a different type.

fromMaybe hasInitial "error" ""
--> Err [ "error" ]


fromMaybe hasInitial "error" "Christoph"
--> Ok 'C'


hasInitial : String -> Maybe Char
hasInitial str =
    case String.uncons str of
        Just (initial, _) -> Just initial
        Nothing -> Nothing