ozmat/elm-forms/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Forms.Validation

This module provides the validation logic for the library. Please refer to the examples for a better understanding

Field Validation

type FieldError err
= CustomError err
| MissingField
| WrongType
| NotInt
| NotFloat
| EmptyString
| WrongLength
| NotEmail
| PasswordNotEqual

A FieldError represents an error that happened during a FieldValidation. Here are the main ones :

MissingField          -- When the `Field` cannot be found
WrongType             -- When the `Value` has a different type
CustomError yourError -- Your type of error
type alias FieldValidation err a =
Validation (FieldError err) a

A FieldValidation represents the Validation of a Field

Common Helpers

failure : FieldError err -> FieldValidation err a

Returns a failed FieldValidation

customFailure : err -> FieldValidation err a

Returns a failed FieldValidation using a CustomError

success : a -> FieldValidation err a

Returns a successful FieldValidation

validation : err -> (a -> Bool) -> a -> FieldValidation err a

Helps creating a basic FieldValidation function

type YourError
    = MustBeEmpty


emptyValidation : String -> FieldValidation YourError String
emptyValidation =
    validation MustBeEmpty String.isEmpty

Type validation

stringValid : (String -> FieldValidation err a) -> Value -> FieldValidation err a

Helps validating a String Value. If the Value has a different type, fails with a WrongType FieldError.

type YourError
    = MustBeEmpty


validateEmptyField : Value -> FieldValidation YourError String
validateEmptyField =
    stringValid (validation MustBeEmpty String.isEmpty)
boolValid : (Bool -> FieldValidation err a) -> Value -> FieldValidation err a

Helps validating a Bool Value. If the Value has a different type, fails with a WrongType FieldError.

type YourError
    = MustBeChecked


validateCheckedField : Value -> FieldValidation YourError Bool
validateCheckedField =
    boolValid (validation MustBeChecked ((==) True))

Basic Validation

int : (Int -> FieldValidation err a) -> String -> FieldValidation err a

Helps validating a String that can be cast into an Int. If the String cannot be cast, fails with a NotInt FieldError.

doYourValidation : Int -> FieldValidation err a


validateField : Value -> FieldValidation err a
validateField =
    stringValid (int (doYourValidation))
float : (Float -> FieldValidation err a) -> String -> FieldValidation err a

Helps validating a String that can be cast into a Float. If the String cannot be cast, fails with a NotFloat FieldError.

doYourValidation : Float -> FieldValidation err a


validateField : Value -> FieldValidation err a
validateField =
    stringValid (float (doYourValidation))
notEmpty : (String -> FieldValidation err a) -> String -> FieldValidation err a

Helps validating a String that is not empty. If the Stringis empty, fails with an EmptyString FieldError.

doYourValidation : String -> FieldValidation err a


validateField : Value -> FieldValidation err a
validateField =
    stringValid (notEmpty (doYourValidation))
Int
-> Int
-> (String -> FieldValidation err a)
-> String
-> FieldValidation err a

Helps validating a String that has a specific length (> low && < high). If the String has a different length, fails with a WrongLength FieldError.

doYourValidation : String -> FieldValidation err a


validateField : Value -> FieldValidation err a
validateField =
    stringValid (length low high (doYourValidation))
email : (String -> FieldValidation err a) -> String -> FieldValidation err a

Helps validating a String that is an email. If the Stringis not an email, fails with a NotEmail FieldError.

doYourValidation : String -> FieldValidation err a


validateField : Value -> FieldValidation err a
validateField =
    stringValid (email (doYourValidation))
(String -> FieldValidation err a)
-> Value
-> Value
-> FieldValidation err a

Helps validating two String Values that match. If the Strings don't match, fails with a PasswordNotEqual FieldError.

doYourValidation : String -> FieldValidation err a


validateField : Value -> Value -> FieldValidation err a
validateField =
    passwordMatch (doYourValidation)

Form Validation

type FormError comparable err
= FormError comparable (FieldError err)

A FormError represents an error that happened during a FormValidation. It's basically a wrapper around the FieldError with the key associated to the Field

type alias FormValidation comparable err a =
Validation (FormError comparable err) a

A FormValidation represents the Validation of a Form

Common Helpers

valid : a -> FormValidation comparable err a

Returns a successful FormValidation

toTuple : FormError comparable err -> ( comparable, FieldError err )

Converts a FormError into a Tuple

Validate

type alias Validate comparable err a =
Group comparable -> FormValidation comparable err a

Validate represents a function that validates a Form. It takes a Group of Fields and returns a FormValidation

Validate Helpers (accumulate)

Those functions help building a Validate function and aim to ease the FormValidation. They will accumulate the different FormErrors during the validation process.

Group comparable
-> comparable
-> (Value -> FieldValidation err a)
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates a required Field. This is the basic use case and will always validate the Field

...
    |> required fields comparable doYourValidation
    ...
a
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Hardcodes a value. This is useful when you need to harcode a specific value during the validation process

...
    |> hardcoded yourHardcodedValue
    ...
Group comparable
-> comparable
-> (String -> FieldValidation err a)
-> a
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates an optional Field. This will use the validation function if the Field is not empty or use the default value otherwise. Only works on String Value

...
    |> optional fields comparable doYourValidation yourDefaultValue
    ...

optional is equivalent to a specific required:

...
    |> required
        fields
        comparable
        (stringValid <|
            \str ->
                if String.isEmpty str then
                    success yourDefaultValue
                else
                    doYourValidation str
        )
    ...
Group comparable
-> comparable
-> (String -> FieldValidation err a)
-> FormValidation comparable err (Maybe a -> b)
-> FormValidation comparable err b

Validates an optional Field with a Maybe. Same logic than with optional but the default value is Nothing and the validated value is Just

...
    |> optionalMaybe fields comparable doYourValidation
    ...
Group comparable
-> comparable
-> comparable
-> (Value -> Value -> FieldValidation err a)
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates two Fields together. The validation function takes two Field Values and returns one result. It is useful when you need to validate one Field that depends on another, but you only need to store one result

...
    |> twoFields fields comparable1 comparable2 doYourValidation
    ...
Group comparable
-> comparable
-> (Group comparable -> FormValidation comparable err a)
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates a Group of Fields. This can be useful in many different cases but mainly when you need to nest a validation process

 ...
    |> fieldGroup fields comparable doYourValidation
    ...

Validate Helpers (bind)

Those functions are the "binding" equivalent of the previous ones. This means that the FormValidation will fail at the first FormError encountered and won't accumulate the errors. Don't mix those functions with the previous ones

Group comparable
-> comparable
-> (Value -> FieldValidation err a)
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates a required Field (binding)

...
    |> required1 fields comparable doYourValidation
    ...
a
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Hardcodes a value for a Field (binding)

...
    |> hardcoded1 yourHardcodedValue
    ...
Group comparable
-> comparable
-> (String -> FieldValidation err a)
-> a
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates an optional Field (binding)

...
    |> optional1 fields comparable doYourValidation yourDefaultValue
    ...
Group comparable
-> comparable
-> (String -> FieldValidation err a)
-> FormValidation comparable err (Maybe a -> b)
-> FormValidation comparable err b

Validates an optional Field with a Maybe (binding)

...
    |> optionalMaybe1 fields comparable doYourValidation
    ...
Group comparable
-> comparable
-> comparable
-> (Value -> Value -> FieldValidation err a)
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates two Fields together (binding)

...
    |> twoFields1 fields comparable1 comparable2 doYourValidation
    ...
Group comparable
-> comparable
-> (Group comparable -> FormValidation comparable err a)
-> FormValidation comparable err (a -> b)
-> FormValidation comparable err b

Validates a Group of Fields (binding)

 ...
    |> fieldGroup fields comparable doYourValidation
    ...