This module provides a few functions for validating data.
isFloat : Validation.ErrorMessage -> Validation.Validator String Basics.Float
Returns an Err errorMessage
if the given value isn't float number, otherwise
return Ok value
.
import Validation exposing (Validator, ErrorMessage)
floatValidation : Validator String Float
floatValidation =
isFloat "The value is not float number!"
floatValidation "5.39161" -- Ok 5.39161
floatValidation "t.39161" -- Err "It is not float number!"
isInt : Validation.ErrorMessage -> Validation.Validator String Basics.Int
Returns an Err errorMessage
if the given value isn't int number, otherwise
return Ok value
.
import Validation exposing (Validator, ErrorMessage)
intValidation : Validator String Int
intValidation =
isInt "The value is not int number!"
intValidation "108" -- Ok 108
intValidation "3.14" -- Err "It is not int number!"
isAtLeast : Validation.Validator String comparable -> comparable -> Validation.ErrorMessage -> Validation.Validator String comparable
Returns an Err errorMessage2
or Err errorMessage1
if the given value isn't
greater or equal to defined value (defined by user) or if first validation failed.
Otherwise return Ok value
.
isAtMost : Validation.Validator String comparable -> comparable -> Validation.ErrorMessage -> Validation.Validator String comparable
Returns an Err errorMessage2
or Err errorMessage1
if the given value isn't
less or equal to defined value (defined by user) or if first validation failed.
Otherwise return Ok value
.
isGreaterThan : Validation.Validator String comparable -> comparable -> Validation.ErrorMessage -> Validation.Validator String comparable
Returns an Err errorMessage2
or Err errorMessage1
if the given value isn't
greater than defined value (defined by user) or if first validation failed.
Otherwise return Ok value
.
isLessThan : Validation.Validator String comparable -> comparable -> Validation.ErrorMessage -> Validation.Validator String comparable
Returns an Err errorMessage2
or Err errorMessage1
if the given value isn't
less than defined value (defined by user) or if first validation failed.
Otherwise return Ok value
.
import Validation exposing (ErrorMessage, Validator)
import Validators exposing (isLessThan)
isLessThanInt : Validator String Int
isLessThanInt =
isLessThan (isInt "The value is not int number!") 5 "The value is not less than 5!"
isLessThanInt "3" -- Ok 3
isLessThanInt "6" -- Err "The value is not less than 5!"
isLessThanInt "6.3" -- Err "The value is not int number!"
isRange : Validation.Validator String comparable -> comparable -> comparable -> Validation.ErrorMessage -> Validation.Validator String comparable
Returns an Err errorMessage2
or Err errorMessage1
if the given value isn't
in range (mathematically speaking it's closed interval) or if first validation failed.
Otherwise return Ok value
. First number has to be less than second.
import Validation exposing (ErrorMessage, Validator)
import Validators exposing (isFloat, isRange)
floatRange : Validator String Float
floatRange =
isRange (isFloat "The value is not float number!") 0 5.5 "The value is not in range!"
floatRange "3.8" -- Ok 3.8
floatRange "6.1" -- Err "The value is not in range!"
floatRange "6.x" -- Err "The value is not float number!"
isNotEmpty : Validation.ErrorMessage -> String -> Result String String
Returns an Err errorMessage
if the given string is empty, otherwise
return Ok value
.
isEmail : Validation.ErrorMessage -> String -> Result String String
Returns an Err errorMessage
if the given string isn't correct an email address,
otherwise return Ok value
.
isUrl : Validation.ErrorMessage -> String -> Result String String
Returns an Err errorMessage
if the given string isn't correct an url path,
otherwise return Ok value
.
isInList : Validation.ErrorMessage -> ( a, List a ) -> Result String a
Returns an Err errorMessage
if the given value isn't in list, otherwise
return Ok value
.
import Validation exposing (Validator, ErrorMessage)
elInListValidation : Validator (Int, List Int) Int
elInListValidation =
isInList "Given value is not in list!"
elInListValidation (3, [1, 2, 3]) -- Ok 3
elInListValidation (5, [1, 2, 3]) -- Err "Given value is not in list!"
isTrue : Validation.ErrorMessage -> Basics.Bool -> Result String Basics.Bool
Returns an Err errorMessage
if the given boolean value is false, otherwise
return Ok True
.
isEqualTo : Validation.Field raw a -> Validation.ErrorMessage -> a -> Result String a
Validate Field
Returns an Err errorMessage
if the given value of Field Valid a
isn't same as
validation argument, otherwise return Ok validation argument
for others Validity
or for Valid a
is Ok value
.
import Validation exposing (Validator, ErrorMessage, Field)
pass : Field String String
pass =
Field "" (Valid "password*")
confirmPasswordValidation : Validator String String
confirmPasswordValidation =
isEqualTo pass "The passwords don't match."
confirmPasswordValidation "password*" -- Ok "password*"
confirmPasswordValidation "pasword*" -- Err "The passwords don't match."
isValidField : Validation.Field raw a -> Basics.Bool
Returns false if Field
hasn't validity Valid a
, otherwise
return true.
import Validation exposing (Field, field, preValidatedField)
intNotValidValue = field "50" -- Field "50" NotValidated
intValidValue = preValidatedField 50 -- Field "50" (Valid 50)
isValidField intNotValidValue -- False
isValidField intValidValue -- True