Convenience functions for validating data.
import Validate exposing (Validator, 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." ]Return an error if the given predicate returns True for the given
subject.
import Validate exposing (Validator, 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." ) ]Return an error if the given String is empty, or if it contains only
spaces, tabs, newlines, or carriage returns.
import Validate exposing (Validator, ifBlank)
modelValidator : Validator Model String
modelValidator =
Validate.all
[ ifBlank .name "Please enter a name."
, ifBlank .email "Please enter an email address."
]Return an error if the given String cannot be parsed as an Int.
import Validate exposing (Validator, ifNotInt)
modelValidator : Validator Model String
modelValidator =
Validate.all
[ ifNotInt .followers (\_ -> "Please enter a whole number for followers.")
, ifNotInt .stars (\stars -> "Stars was \"" ++ stars ++ "\", but it needs to be a whole number.")"
]Return an error if a String cannot be parsed as an Float.
Return an error if a List is empty.
Return an error if a Dict is empty.
Return an error if a Set is empty.
Return an error if a Maybe is Nothing.
Return an error if an email address is malformed.
import Validate exposing (Validator, ifBlank, ifNotInt)
modelValidator : Validator Model String
modelValidator =
Validate.all
[ ifInvalidEmail .primaryEmail (\_ -> "Please enter a valid primary email address.")
, ifInvalidEmail .superSecretEmail (\email -> "Unfortunately, \"" ++ email ++ "\" is not a valid Super Secret Email Address.")
]Return an error if a predicate returns True for the given
subject.
import Validate exposing (Validator, ifTrue)
modelValidator : Validator Model String
modelValidator =
ifTrue (\model -> countSelected model < 2)
"Please select at least two."Return an error if a predicate returns False for the given
subject.
import Validate exposing (Validator, ifFalse)
modelValidator : Validator Model String
modelValidator =
ifFalse (\model -> countSelected model >= 2)
"Please select at least two."Create a custom validator, by providing a function that returns a list of errors given a subject.
import Validate exposing (Validator, fromErrors)
modelValidator : Validator Model String
modelValidator =
fromErrors modelToErrors
modelToErrors : Model -> List String
modelToErrors model =
let
usernameLength =
String.length model.username
in
if usernameLength < minUsernameChars then
[ "Username not long enough" ]
else if usernameLength > maxUsernameChars then
[ "Username too long" ]
else
[]Run each of the given validators, in order, and return their concatenated error lists.
import Validate exposing (Validator, 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."
]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.
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 (Validator, 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" }
--> []Returns True if the given string is nothing but whitespace.
ifBlank uses this under the hood.
Returns True if String.toInt on the given string returns an Ok.
ifNotInt uses this under the hood.
Returns True if String.toFloat on the given string returns an Ok.
ifNotFloat uses this under the hood.
Returns True if the email is valid.
ifInvalidEmail uses this under the hood.
A
Validatorcontains a function which takes a subject and returns a list of errors describing anything invalid about that subject.Pass it to
validateto get the list of errors. An empty error list means the subject was valid.