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." ]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." ) ]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."
]Return an error if the given String cannot be parsed as an Int.
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.
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."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."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."
]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 (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 the email is malformed.
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.