Skip to content

Instantly share code, notes, and snippets.

@Chadtech
Last active October 1, 2018 11:35
Show Gist options
  • Save Chadtech/407b0928bfd7e42cb5394b702f5e962e to your computer and use it in GitHub Desktop.
Save Chadtech/407b0928bfd7e42cb5394b702f5e962e to your computer and use it in GitHub Desktop.
validatePassword : String -> Result String ValidPassword
validatePassword password =
validate password passwordConditions
passwordConditions : List (String -> Bool, String)
passwordConditions =
[ (isBlank, "Password can't be empty")
, (isTooShort, "Password too short")
]
validate : String -> List (String -> Bool, String) -> Result String ValidPassword
validate password conditions =
conditions
|> List.map (checkCondition password)
|> combineErrors password
checkCondition : String -> (String -> Bool, String) -> Maybe String
checkCondition password (condition, error) =
if condition password then
Just error
else
Nothing
combineErrors : String -> List (Maybe String) -> Result String ValidPassword
combineErrors password errors =
case errors of
Just error :: rest ->
Err error
Nothing :: rest ->
combineErrors password rest
[] ->
Ok (ValidPassword (Password password))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment