Changeset.Validate

Validation

Domain-agnostic field predicates as Result String a.

Pipe a predicate (or all of them) into [Changeset.field]. On success you get the same value back; on failure you get a message for that field.

import Changeset
import Changeset.Validate as Validate

Changeset.field "email"
    (Validate.all
        [ Validate.trimNonEmpty
        , Validate.minLength 3
        , Validate.maxLength 254
        ]
        rawEmail
    )

trimNonEmpty is usually what you want for text inputs. Use nonEmpty when leading or trailing spaces must stay meaningful. Numeric helpers (gt, between, and so on) work on any comparable.

Values

nonEmpty

nonEmpty : String -> Result String String

Reject blank strings (no trim). Prefer trimNonEmpty for form fields.

Examples

nonEmpty "" == Err "can't be blank"
nonEmpty "Hi" == Ok "Hi"

trimNonEmpty

trimNonEmpty : String -> Result String String

Trim, then reject blank.

Examples

Validate.trimNonEmpty "  hi  "  == Ok "hi"
Validate.trimNonEmpty "   "     == Err "can't be blank"

minLength

minLength : Int -> String -> Result String String

Require at least n characters (length after any trim you already did).

Examples

Validate.minLength 8 password

maxLength

maxLength : Int -> String -> Result String String

Require at most n characters.

Examples

Validate.maxLength 254 email

member

member : List a -> a -> Result String a

Value must appear in options. Default message is "is invalid". Use memberWith for a custom message.

Examples

Validate.member [ "draft", "published" ] status

memberWith

memberWith : String -> List a -> a -> Result String a

member with a custom error message.

Examples

Validate.memberWith "pick a valid role" [ "admin", "user" ] role

notMember

notMember : List a -> a -> Result String a

Value must not appear in options. Default message is "is reserved".

Examples

Validate.notMember [ "admin", "root" ] username

notMemberWith

notMemberWith : String -> List a -> a -> Result String a

notMember with a custom error message.

Examples

Validate.notMemberWith "username taken" reservedNames username

gt

gt : comparable -> comparable -> Result String comparable

Strictly greater than bound.

Examples

Validate.gt 0 quantity

gtWith

gtWith : String -> comparable -> comparable -> Result String comparable

gt with a custom error message.

Examples

Validate.gtWith "quantity must be positive" 0 quantity

gte

gte : comparable -> comparable -> Result String comparable

Greater than or equal to bound.

Examples

Validate.gte 0 balance

gteWith

gteWith : String -> comparable -> comparable -> Result String comparable

gte with a custom error message.

Examples

Validate.gteWith "too small" 1 count

lt

lt : comparable -> comparable -> Result String comparable

Strictly less than bound.

Examples

Validate.lt 100 score

ltWith

ltWith : String -> comparable -> comparable -> Result String comparable

lt with a custom error message.

Examples

Validate.ltWith "must be under 100" 100 score

lte

lte : comparable -> comparable -> Result String comparable

Less than or equal to bound.

Examples

Validate.lte 100 score

lteWith

lteWith : String -> comparable -> comparable -> Result String comparable

lte with a custom error message.

Examples

Validate.lteWith "max is 100" 100 score

eq

eq : comparable -> comparable -> Result String comparable

Equal to bound.

Examples

Validate.eq expectedToken providedToken

eqWith

eqWith : String -> comparable -> comparable -> Result String comparable

eq with a custom error message.

Examples

Validate.eqWith "confirmation mismatch" password confirm

between

between : comparable -> comparable -> comparable -> Result String comparable

Inclusive range [low, high].

Examples

Validate.between 1 100 score

betweenWith

betweenWith : String -> comparable -> comparable -> comparable -> Result String comparable

between with a custom error message.

Examples

Validate.betweenWith "score out of range" 1 100 score

all

all : List (a -> Result String a) -> a -> Result String a

Run rules in order; first failure wins for this value.

Examples

Validate.all [ Validate.trimNonEmpty, Validate.minLength 3 ] "ab"
-- Err "must be at least 3 characters"