gege251 / elm-validator-pipeline / Validator.Named

Named validators work exactly the same way, as the simple ones, but every validate function takes a name string. Errors will be accumulated into a Dict, where the key is the given name, and the value is a list of errors.

Ok ValidatedForm
    |> validate "name" (notEmpty "name is required") form.name
    |> validate "email" (isEmail "email is invalid") form.email
    |> validateMany "password"
        [ hasLetter "password needs to contain letters"
        , hasNumber "password needs to contain numbers"
        ]
        form.password
    |> noCheck form.message
    |> checkOnly "approved" (Bool.isTrue "you need to approve") form.approved

Pipeline functions

noCheck : a -> Validated x (a -> b) -> Validated x b

Pipe a value through without perfoming any checks.

validate : String -> Validator x a b -> a -> Validated x (b -> c) -> Validated x c

Validate a value using a validator.

checkOnly : String -> Validator x a b -> a -> Validated x c -> Validated x c

Validate a value without applying it to the pipe.

validateMany : String -> List (Validator x a a) -> a -> Validated x (a -> b) -> Validated x b

Validate a value using a list of validators. Checks are performed from left to right, and will stop on the first failure, returning only the first error.

validateAll : String -> List (Validator x a a) -> a -> Validated x (a -> b) -> Validated x b

Validate a value using a list of validators. Checks are performed from left to right, and will return all errors.

Errors

Named validators return lists of errors in a Dict, where the key is the field name. You don't even need to use these helpers to get errors for a field, you can simply use Dict.get NAME.


type alias Validated error value =
Result (Errors error) value

Validated is an alias for a Result type with Errors.


type alias Errors error =
Dict String (List error)

Named validators return lists of errors in a Dict, where the key is the field name.

hasErrorsOn : String -> Validated x a -> Basics.Bool

Checks if there are any errors for a given field name.

getErrors : String -> Validated x a -> Maybe (List x)

Get errors for a given field.

countErrors : Validated x a -> Basics.Int

Count all the errors.