Last active
October 1, 2018 11:35
-
-
Save Chadtech/407b0928bfd7e42cb5394b702f5e962e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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