This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Validate a model into a structure that makes forbidden states impossible.
version 4.0.0
license BSD-3-Clause
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 4.0.0
Committed At 2018-05-30 08:24:44 UTC
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

README

elm-verify

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

Example Use-Case

Your model is unvalidated, but on submit you want to verify your model and allow your function to submit that model to only take a verified model.

Your model would maybe look like this:

    type alias Model =
        { id : Int
        , firstName : Maybe String
        , lastName : Maybe String

        -- ...
        }

Your submit function would be: submit : VerifiedModel -> Cmd msg -- not Model

So you define VerifiedModel like this:

    type alias VerifiedModel =
        { id : Int
        , firstName : String
        , lastName : String

        -- ...
        }

and you can verify this by using a Json.Decode.Pipeline-like api.

     validator : Validator String Model VerifiedModel
     validator =
        validate VerifiedModel
            |> keep .id
            |> verify .firstName (Maybe.Verify.isJust "no first name")
            |> verify .lastName (Maybe.Verify.isJust"no last name")

You can execute a Validator just by calling it.

    validator { id = 1, firstName = Just "Luke", lastName = Just "Skywalker" }
    --> Ok { id = 1, firstName = "Luke", lastName = "Skywalker" }

    validator { id = 1, firstName = Nothing, lastName = Nothing }
    --> Err [ "no first name", "no last name" ]

So far we've used Validators from Maybe.Verify. There are other Validators in this package, but it's also simple to write your own Validator.

  possitiveNumber : error -> Validator error Int Int
  possitiveNumber error input =
    if input >= 0 then
       Ok input
    else
       Err [ error ]