Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. //Check if the password is long enough
  2. func isPasswordLongEnough(_ password:String) -> Validation<[String], String> {
  3. if password.characters.count < 8 {
  4. return Validation.Failure(["Password must have more than 8 characters."])
  5. } else {
  6. return Validation.Success(password)
  7. }
  8. }
  9.  
  10. //Check if the password contains a special character
  11. func isPasswordStrongEnough(_ password:String) -> Validation<[String], String> {
  12. if (password.range(of:"[\\W]", options: .regularExpression) != nil){
  13. return Validation.Success(password)
  14. } else {
  15. return Validation.Failure(["Password must contain a special character."])
  16. }
  17. }
  18.  
  19. //Check if the user is different from password, by Jlopez
  20. func isDifferentUserPass(_ user:String, _ password:String) -> Validation<[String], String> {
  21. if (user == password){
  22. return Validation.Failure(["Username and password MUST be different."])
  23. } else {
  24. return Validation.Success(password)
  25. }
  26. }
  27.  
  28.  
  29. //Concating all validations in one that checks all rules
  30. func isPasswordValid(user: String, password:String) -> Validation<[String], String> {
  31. return isPasswordLongEnough(password)
  32. .sconcat(isPasswordStrongEnough(password))
  33. .sconcat(isDifferentUserPass(user, password))
  34. }
  35.  
  36.  
  37. //Examples with invalid password
  38. let result = isPasswordValid(user: "Richi", password: "Richi")
  39. /* ▿ Validation<Array<String>, String>
  40. ▿ Failure : 3 elements
  41. - 0 : "Password must have more than 8 characters."
  42. - 1 : "Password must contain a special character."
  43. - 2 : "Username and password MUST be different."
  44. */
  45.  
  46. //Example with valid password
  47. let result = isPasswordValid(user:"Richi", password: "Ricardo$")
  48. /*
  49. ▿ Validation<Array<String>, String>
  50. - Success : "Ricardo$"
  51. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement