iodevs/elm-validate/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Validation

This library helps with validation of input forms.

Definition

type Field raw 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:

  • NotValidated values e.g. in input from, which have not validated yet.
  • Valid a values that they are correct.
  • Invalid String and state for incorrect input values.
type Event raw
= OnSubmit
| OnBlur
| OnRelatedChange
| OnChange raw

Event describe state of input form:

  • OnSubmit validates model data before submitting to server, see validateModel in example.
  • OnBlur validates input form when user leaves an input field.
  • OnRelatedChange validates input form which is tied with another form. For example: password and confirm form.
  • OnChange raw validates input form when user changes value in input field,
= NotSubmitted
| InProcess
| Succeeded
| Failed

Here SubmissionStatus define states for submit data to server:

  • NotSubmitted means that data have not sent yet.

  • InProcess for data being processed.

  • Succeeded if data have been successfully received.

  • Failed or vice versa, data have not been successfully received.

    This also may be used to inform user on screen, see renderStatusin example.

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 -> Field String val

Default setting of Field with Valid a validity.

import Validation exposing (Field, field)


intValue : Field String Int
intValue =
    preValidatedField "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 -> Validator 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
    OnSubmit
    (optional (isPositiveInt "The age has to be positive number."))
    age


-- Field "" (Valid Nothing)

Higher-Order Helpers

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

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

(>&&) : Validator a b -> Validator b c -> Validator a c

Composition of two Validators.

import Validators exposing (isNotEmpty, isEmail)


emailValidation : Validator String String
emailValidation =
    isNotEmpty "An email is required."
        >&& isEmail "Please ensure this is a valid email."