Verify allows you to validate a model into a structure that makes forbidden states impossible.
This allows you to lift any value into a validator. This is particularly useful to initialize a pipeline.
ok "always ok" Nothing
--> Ok "always ok"Allows you to create a failing Validator.
fail "always fail" Nothing
--> Err [ "always fail" ]Allows you to verify a part of a structure.
import Maybe.Verify exposing (isJust)
validator : Validator String { a | firstName : Maybe String } String
validator =
Verify.ok identity
|> verify .firstName (isJust "You need to provide a first name.")
validator { firstName = Nothing }
--> Err [ "You need to provide a first name." ]
validator { firstName = Just "Stöffel" }
--> Ok "Stöffel"You can use keep if you want a value to be in the verified structure without any verification.
import Maybe.Verify exposing (isJust)
validator : Validator String { a | id : Int, firstName : Maybe String } (Int, String)
validator =
Verify.ok (,)
|> keep .id
|> verify .firstName (isJust "You need to provide a first name.")
validator { id = 1, firstName = Nothing }
--> Err [ "You need to provide a first name." ]
validator { id = 1, firstName = Just "Stöffel" }
--> Ok (1, "Stöffel")Sometimes the verification of a part only makes sense in a bigger context. This means your Validator needs access to the whole structure.
import Maybe.Verify exposing (isJust)
validator : Validator String { a | username : Maybe String, level: Int, strength: Int } (String, Int)
validator =
Verify.ok (,)
|> verify .username (isJust "You need to provide a username.")
|> custom (\{level, strength} ->
if strength > level then
Err [ "Your strength can exceed your level." ]
else
Ok strength
)
validator { username = Just "Ork1", level = 3, strength = 5 }
--> Err [ "Your strength can exceed your level." ]
validator { username = Just "Ork1", level = 6, strength = 5 }
--> Ok ("Ork1", 5)
validator { username = Nothing, level = 3, strength = 5 }
--> Err [ "You need to provide a username."
--> , "Your strength can exceed your level."
--> ]This allows you to combine multible Validators.
import Maybe.Verify exposing (isJust)
import String.Verify exposing (notBlank)
validator { firstName = Nothing }
--> Err [ "You need to provide a first name." ]
validator { firstName = Just " " }
--> Err [ "You need to provide a none empty first name." ]
validator { firstName = Just "Stöffel" }
--> Ok "Stöffel"
validator : Validator String { a | firstName : Maybe String } String
validator =
Verify.ok identity
|> verify .firstName verifyName
verifyName : Validator String (Maybe String) String
verifyName =
isJust "You need to provide a first name."
|> andThen (notBlank "You need to provide a none empty first name.")Fails if a predicate is False.
when hasInitial "error" ""
--> Err [ "error" ]
when hasInitial "error" "Christoph"
--> Ok 'C'
hasInitial : String -> Maybe Char
hasInitial str =
case String.uncons str of
Just (initial, _) -> Just initial
Nothing -> Nothing
This is just an alias for a function from an input to a result. The result is either: