rtfeldman/elm-validate/2.0.0

NOTE: this package is not compatible with Elm 0.19.1

Validate

Convenience functions for validating data.

import Validate exposing (ifBlank, ifNotInt, validate)


type Field = Name | Email | Age


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


modelValidator : Validator String Model
modelValidator =
    Validate.all
        [ ifBlank .name "Please enter a name."
        , Validate.firstError
            [ ifBlank .email "Please enter an email address."
            , ifInvalidEmail .email "This is not a valid email address."
            ]
        , ifNotInt .age "Age must be a whole number."
        ]


validate modelValidator { name = "Sam", email = "blah", age = "abc" }
    --> [ "This is not a valid email address.", "Age must be a whole number." ]

Validating a subject

type Validator error subject

A Validator contains a function which takes a subject and returns a list of errors describing anything invalid about that subject.

Pass it to validate to get the list of errors. An empty error list means the subject was valid.

validate : Validator error subject -> subject -> List error

Return an error if the given predicate returns True for the given subject.

import Validate exposing (ifBlank, ifNotInt, validate)


type Field = Name | Email | Age


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


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


validate modelValidator { name = "Sam", email = "", age = "abc" }
    --> [ ( Email, "Please enter an email address." ), ( Age, "Age must be a whole number." ) ]

Creating validators

ifBlank : (subject -> String) -> error -> Validator error subject

Return an error if the given String is empty, or if it contains only whitespace characters.

import Validate exposing (ifBlank, ifNotInt)


modelValidator : Validator Model String
modelValidator =
    Validate.all
        [ ifBlank .name "Please enter a name."
        , ifBlank .email "Please enter an email address."
        ]
ifNotInt : (subject -> String) -> error -> Validator error subject

Return an error if the given String cannot be parsed as an Int.

ifEmptyList : (subject -> List a) -> error -> Validator error subject

Return an error if a List is empty.

ifEmptyDict : (subject -> Dict comparable v) -> error -> Validator error subject

Return an error if a Dict is empty.

ifEmptySet : (subject -> Set comparable) -> error -> Validator error subject

Return an error if a Set is empty.

ifNothing : (subject -> Maybe a) -> error -> Validator error subject

Return an error if a Maybe is Nothing.

ifInvalidEmail : (subject -> String) -> error -> Validator error subject

Return an error if an email address is malformed.

ifTrue : (subject -> Bool) -> error -> Validator error subject

Return an error if a predicate returns True for the given subject.

import Validate exposing (ifTrue)


modelValidator : Validator Model String
modelValidator =
    ifTrue (\model -> countSelected model < 2)
        "Please select at least two."
ifFalse : (subject -> Bool) -> error -> Validator error subject

Return an error if a predicate returns False for the given subject.

import Validate exposing (ifFalse)


modelValidator : Validator Model String
modelValidator =
    ifFalse (\model -> countSelected model >= 2)
        "Please select at least two."

Combining validators

all : List (Validator error subject) -> Validator error subject

Run each of the given validators, in order, and return their concatenated error lists.

import Validate exposing (ifBlank, ifNotInt)


modelValidator : Validator Model String
modelValidator =
    Validate.all
        [ ifBlank .name "Please enter a name."
        , ifBlank .email "Please enter an email address."
        , ifNotInt .age "Age must be a whole number."
        ]
any : List (Validator error subject) -> subject -> Bool

Return True if none of the given validators returns any errors for the given subject, and False if any validator returns one or more errors.

firstError : List (Validator error subject) -> Validator error subject

Run each of the given validators, in order, stopping after the first error and returning it. If no errors are encountered, return Nothing.

import Validate exposing (ifBlank, ifInvalidEmail, ifNotInt)




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




modelValidator : Validator String Model
modelValidator =
    Validate.all
        [ Validate.firstError
            [ ifBlank .email "Please enter an email address."
            , ifInvalidEmail .email "This is not a valid email address."
            ]
        , ifNotInt .age "Age must be a whole number."
        ]




validate modelValidator { email = " ", age = "5" }
    --> [ "Please enter an email address." ]


validate modelValidator { email = "blah", age = "5" }
    --> [ "This is not a valid email address." ]


validate modelValidator { email = "[email protected]", age = "5" }
    --> []

Checking values directly

isBlank : String -> Bool

Returns True if the given string is nothing but whitespace.

ifBlank uses this under the hood.

isInt : String -> Bool

Returns True if String.toInt on the given string returns an Ok.

ifNotInt uses this under the hood.

isValidEmail : String -> Bool

Returns True if the email is malformed.

ifInvalidEmail uses this under the hood.