iodevs / elm-validate / Validation

This module helps with validation of input forms.

Definition


type Field raw a
    = Field raw (Validity a)

Define data type. For example:

dist =
    Field 40.5 NotValidated

That means dist has value 40.5 which hasn't validated yet.


type Validity a
    = NotValidated
    | Valid a
    | Invalid String

This type defines three state of Field:


type Event raw
    = OnSubmit
    | OnBlur
    | OnRelatedChange
    | OnChange raw

Event describe state of input form:


type SubmissionStatus
    = NotSubmitted
    | InProcess
    | Succeeded
    | Failed

Here SubmissionStatus define states for submit data to server:


type alias OptionalField raw a =
Field raw (Maybe a)

Sometimes we want to use input form as optional, for example age. In this case the input field can be an empty. But if somebody provides input value then input field will be validated. So this type is used for define optional variable.


type alias Validator a b =
a -> Result String b

It's used for validate variables, see Validators for example.


type alias ErrorMessage =
String

Represents error message for invalid values in input form. This type is used for definition of validator functions.

Helpers

extractError : Field raw a -> Maybe String

Extract error message from Field.

import Validaton exposing (Field)

errorLabel : Field raw a -> Html Msg
errorLabel field =
    div []
        [ field
            |> extractError
            |> Maybe.withDefault ""
            |> text
        ]

field : b -> Field b a

Default setting of Field with NotValidated validity.

import Validation exposing (Field, field)

intValue : Field String String
intValue =
    field "50"

-- Field "50" NotValidated

preValidatedField : (val -> String) -> val -> Field String val

Default setting of Field with Valid a validity. For Field String String use an identity function.

import Validation exposing (Field, field)

intValue : Field String Int
intValue =
    preValidatedField String.fromInt 50

-- Field "50" (Valid 50)

validate : Event raw -> Validator raw a -> Field raw a -> Field raw a

Run validation on Field with Event.

validity : Field raw a -> Validity a

Get validity from Field.

import Validation exposing (Field)

intValue : Field String Int
intValue =
    Field "50" (Valid 50)

validity intValue       -- Valid 50

rawValue : Field b a -> b

Get value from Field.

import Validation exposing (Field)

intValue : Field String String
intValue =
    Field "50" NotValidated

rawValue intValue       -- "50"

optional : Validator String a -> String -> Result String (Maybe a)

Validation of optional variable.

import Validaton exposing (Event(..), OptionalField, field, validate)
import Validators exposing (isPositiveInt)

age : OptionalField String Int
age =
    field ""

validate
    age
        |> optional (isPositiveInt "The age has to be positive number.")
        |> OnSubmit

-- Field "" (Valid Nothing)

invalidate : String -> Field raw a -> Field raw a

Default setting of Field with Valid a validity. For Field String String use an identity function.

import Validation exposing (Field, invalidate, preValidatedField)

intValue : Field String Int
intValue =
    preValidatedField String.fromInt 50

invalidate "Bad value" intValue  -- Field "50" (Invalid "Bad value")

Higher-Order Helpers

applyValidity : Validity a -> Validity (a -> b) -> Validity b

Applying function to particular validation. For instance, see submitIfValid and submit function in example.

composite : Validator a b -> Validator b c -> Validator a c

Composition of two Validators.

import Validators exposing (isEmail, isNotEmpty)

emailValidation : Validator String String
emailValidation =
    composite
        (isNotEmpty "An email is required.")
        (isEmail "Please ensure this is a valid email.")