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 StringReject 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 StringTrim, then reject blank.
Examples
Validate.trimNonEmpty " hi " == Ok "hi"
Validate.trimNonEmpty " " == Err "can't be blank"minLength
minLength : Int -> String -> Result String StringRequire at least n characters (length after any trim you already did).
Examples
Validate.minLength 8 passwordmaxLength
maxLength : Int -> String -> Result String StringRequire at most n characters.
Examples
Validate.maxLength 254 emailmember
member : List a -> a -> Result String aValue must appear in options. Default message is "is invalid". Use memberWith for a custom message.
Examples
Validate.member [ "draft", "published" ] statusmemberWith
memberWith : String -> List a -> a -> Result String amember with a custom error message.
Examples
Validate.memberWith "pick a valid role" [ "admin", "user" ] rolenotMember
notMember : List a -> a -> Result String aValue must not appear in options. Default message is "is reserved".
Examples
Validate.notMember [ "admin", "root" ] usernamenotMemberWith
notMemberWith : String -> List a -> a -> Result String anotMember with a custom error message.
Examples
Validate.notMemberWith "username taken" reservedNames usernamegt
gt : comparable -> comparable -> Result String comparableStrictly greater than bound.
Examples
Validate.gt 0 quantitygtWith
gtWith : String -> comparable -> comparable -> Result String comparablegt with a custom error message.
Examples
Validate.gtWith "quantity must be positive" 0 quantitygte
gte : comparable -> comparable -> Result String comparableGreater than or equal to bound.
Examples
Validate.gte 0 balancegteWith
gteWith : String -> comparable -> comparable -> Result String comparablegte with a custom error message.
Examples
Validate.gteWith "too small" 1 countlt
lt : comparable -> comparable -> Result String comparableStrictly less than bound.
Examples
Validate.lt 100 scoreltWith
ltWith : String -> comparable -> comparable -> Result String comparablelt with a custom error message.
Examples
Validate.ltWith "must be under 100" 100 scorelte
lte : comparable -> comparable -> Result String comparableLess than or equal to bound.
Examples
Validate.lte 100 scorelteWith
lteWith : String -> comparable -> comparable -> Result String comparablelte with a custom error message.
Examples
Validate.lteWith "max is 100" 100 scoreeq
eq : comparable -> comparable -> Result String comparableEqual to bound.
Examples
Validate.eq expectedToken providedTokeneqWith
eqWith : String -> comparable -> comparable -> Result String comparableeq with a custom error message.
Examples
Validate.eqWith "confirmation mismatch" password confirmbetween
between : comparable -> comparable -> comparable -> Result String comparableInclusive range [low, high].
Examples
Validate.between 1 100 scorebetweenWith
betweenWith : String -> comparable -> comparable -> comparable -> Result String comparablebetween with a custom error message.
Examples
Validate.betweenWith "score out of range" 1 100 scoreall
all : List (a -> Result String a) -> a -> Result String aRun 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"