Giter Site home page Giter Site logo

strickinato / elm-validate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rtfeldman/elm-validate

0.0 2.0 0.0 129 KB

Convenience functions for validating Elm data.

Home Page: http://package.elm-lang.org/packages/rtfeldman/elm-validate/latest

License: BSD 3-Clause "New" or "Revised" License

Elm 100.00%

elm-validate's Introduction

elm-validate

elm-validate provides convenience functions for validating data.

It is based around the idea of a Validator - a function which accepts a subject and returns a list of errors representing anything invalid about that subject. If the list is empty, the subject is valid.

For example:

validateModel : Model -> List String
validateModel =
    Validate.all
        [ .name  >> ifBlank "Please enter a name."
        , .email >> ifBlank "Please enter an email address."
        , .age   >> ifNotInt "Age must be a whole number."
        ]

type alias Model =
    { name : String, email : String, age : String }

-- Evaluates to True
validateModel { name = "Sam", email = "", age = "abc" }
    == ["Please enter an email address.", "Age must be a whole number."]

elm-validate is not opinionated about how you represent your errors.

For example, you might want to represent them as a tuple of the error message as well as the field responsible for the error:

validateModel : Model -> List (Field, String)
validateModel =
    Validate.all
        [ .name  >> ifBlank (Name, "Please enter a name.")
        , .email >> ifBlank (Email, "Please enter an email address.")
        , .age   >> ifNotInt (Age, "Age must be a whole number.")
        ]

type Field =
    Name | Email | Age

type alias Model =
    { name : String, email : String, age : String }

-- Evaluates to True
validateModel { name = "Sam", email = "", age = "abc" }
    == [(Email, "Please enter an email address."), (Age, "Age must be a whole number.")]

elm-validate's People

Contributors

rtfeldman avatar strickinato avatar tesk9 avatar

Watchers

 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.