Code Monkey home page Code Monkey logo

elm-verify's Introduction

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 ]

elm-verify's People

Contributors

gyzerok avatar stoeffel avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.