kutyel / elm-form / Form.Validate

Form validation.

Combinators


type alias Validation customError output =
Form.Field.Field -> Result (Form.Error.Error customError) output

A validation is a function that takes a form field and returns a result being either a validation error or the expected object.

field : String -> Validation e a -> Form.Field.Field -> Result (Form.Error.Error e) a

Access the given field in the group.

field "name" string

map : (a -> b) -> Validation e a -> Form.Field.Field -> Result (Form.Error.Error e) b

Map over the result of the validation.

field "myfield" (string |> map String.trim)

succeed : a -> Form.Field.Field -> Result (Form.Error.Error e) a

A validation that always succeeds. Useful for contextual validation.

andThen : (a -> Validation e b) -> Validation e a -> Form.Field.Field -> Result (Form.Error.Error e) b

Apply a new validation to the result of the validation.

field "myfield" (int |> andThen (minInt 10))

andMap : Validation e a -> Validation e (a -> b) -> Form.Field.Field -> Result (Form.Error.Error e) b

Incremental form validation for records with more that 8 fields.

Form.Validate.succeed SomeRecord
    |> andMap (field "foo" string)
    |> andMap (field "bar" string)

customError : e -> Form.Error.Error e

Helper to create a CustomError.

defaultValue : a -> Validation e a -> Form.Field.Field -> Result (Form.Error.Error e) a

Rescue a failed validation with the supplied value.

mapError : (Form.Error.Error e1 -> Form.Error.Error e2) -> Validation e1 a -> Validation e2 a

Call Result.mapError on validation result.

withCustomError : customErr -> Validation e a -> Validation customErr a

Arrange that if a validation fails, it has the given custom error.

field "customerId"
    (V.int
        |> andThen (minInt 1)
        |> andThen (maxInt 9999)
        |> withCustomError InvalidIdentity
    )

sequence : List (Validation e a) -> Validation e (List a)

Combine a list of validations into a validation producing a list of all results.

Fixed-size forms

map2 : (a -> b -> m) -> Validation e a -> Validation e b -> Validation e m

Validation a form with two fields.

map3 : (a -> b -> c -> m) -> Validation e a -> Validation e b -> Validation e c -> Validation e m

Validation a form with three fields.

map4 : (a -> b -> c -> d -> m) -> Validation e a -> Validation e b -> Validation e c -> Validation e d -> Validation e m

Validation a form with four fields.

map5 : (a -> b -> c -> d -> e -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err m

Validation a form with five fields.

map6 : (a -> b -> c -> d -> e -> f -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err f -> Validation err m

Validation a form with six fields.

map7 : (a -> b -> c -> d -> e -> f -> g -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err f -> Validation err g -> Validation err m

Validation a form with seven fields.

map8 : (a -> b -> c -> d -> e -> f -> g -> h -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err f -> Validation err g -> Validation err h -> Validation err m

Validation a form with eight fields.

Type extractors

list : Validation e a -> Form.Field.Field -> Result (Form.Error.Error e) (List a)

Validate a list of fields.

string : Form.Field.Field -> Result (Form.Error.Error e) String

Validation a String.

int : Form.Field.Field -> Result (Form.Error.Error e) Basics.Int

Validation an integer using String.toInt.

float : Form.Field.Field -> Result (Form.Error.Error e) Basics.Float

Validation a float using String.toFloat.

bool : Form.Field.Field -> Result (Form.Error.Error e) Basics.Bool

Validation a Bool.

maybe : Validation e a -> Form.Field.Field -> Result (Form.Error.Error e) (Maybe a)

Transform validation result to Maybe, using Result.toMaybe.

email : Validation e String

Check if the string is a valid email address.

emptyString : Form.Field.Field -> Result (Form.Error.Error e) String

Validate an empty string, otherwise failing with InvalidString. Useful with oneOf for optional fields with format validation.

Common filters

minInt : Basics.Int -> Basics.Int -> Form.Field.Field -> Result (Form.Error.Error e) Basics.Int

Min value for Int.

maxInt : Basics.Int -> Basics.Int -> Form.Field.Field -> Result (Form.Error.Error e) Basics.Int

Max value for Int.

minFloat : Basics.Float -> Basics.Float -> Form.Field.Field -> Result (Form.Error.Error e) Basics.Float

Min value for Float.

maxFloat : Basics.Float -> Basics.Float -> Form.Field.Field -> Result (Form.Error.Error e) Basics.Float

Max value for Float.

minLength : Basics.Int -> String -> Form.Field.Field -> Result (Form.Error.Error e) String

Min length for String.

maxLength : Basics.Int -> String -> Form.Field.Field -> Result (Form.Error.Error e) String

Max length for String.

nonEmpty : String -> Form.Field.Field -> Result (Form.Error.Error e) String

Fails if String.isEmpty.

format : Regex -> String -> Form.Field.Field -> Result (Form.Error.Error e) String

Validates format of the string.

includedIn : List String -> String -> Form.Field.Field -> Result (Form.Error.Error e) String

Check if the string is included in the given list.

Custom validations

fail : Form.Error.Error e -> Form.Field.Field -> Result (Form.Error.Error e) a

A validation that always fails. Useful for contextual validation.

customValidation : Validation e a -> (a -> Result (Form.Error.Error e) b) -> Form.Field.Field -> Result (Form.Error.Error e) b

Custom validation for your special cases.

oneOf : List (Validation e a) -> Form.Field.Field -> Result (Form.Error.Error e) a

First successful validation wins, from left to right.