Guest User

Untitled

a guest
Mar 21st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import cats._
  2. import cats.data._
  3. import cats.implicits._
  4.  
  5. object ValidateMyExistence {
  6. // Say you are writing a password change application.
  7. // You are given a username, an old password, and a new password
  8. case class PasswordChangeRequest(username: String, oldPassword: String, newPassword: String)
  9. // We have a set of rules we would like to enforce on this data,
  10. // and would like to ensure that all of them are correct,
  11. // or we will return a list of all of the failures
  12. // Rules:
  13. // 1. Username MUST be all lowercase
  14. // 2. Username MUST NOT be empty
  15. // 3. Passwords MUST be atleast 8 characters long
  16. // 4. Passwords MUST contain a Number in the first 8 characters
  17. // 5. Passwords MUST contain a Letter in the first 8 characters
  18. // 6. Passwords MUST NOT contain characters ' \ or :
  19.  
  20. class Username private[ValidateMyExistence] (v: String){
  21. val value: String = v
  22. }
  23.  
  24. private def validateUsername(username: String): ValidatedNel[String, Username] =
  25. (usernameLowercase(username), usernameNotEmpty(username))
  26. .mapN((_, _) => new Username(username))
  27.  
  28. private def usernameLowercase(username: String): ValidatedNel[String, Unit] =
  29. Validated.condNel[String, Unit](
  30. username.matches(".*[a-z]+.*"),
  31. (),
  32. "Username Not All Lowercase"
  33. )
  34. private def usernameNotEmpty(username: String): ValidatedNel[String, Unit] =
  35. Validated.condNel[String, Unit](
  36. !username.isEmpty,
  37. (),
  38. "Username is Empty"
  39. )
  40.  
  41. class Password private[ValidateMyExistence] (v: String){
  42. val value: String = v
  43. }
  44.  
  45. private sealed trait PasswordType
  46. private object PasswordType {
  47. implicit val showPassword: Show[PasswordType] = Show.show[PasswordType]{
  48. case OldPassword => "Old Password"
  49. case NewPassword => "New Password"
  50. }
  51. case object OldPassword extends PasswordType
  52. case object NewPassword extends PasswordType
  53. }
  54.  
  55. private def validatePassword(password: String, passwordType: PasswordType): ValidatedNel[String, Password] =
  56. (
  57. passwordLongerThanEight(password, passwordType),
  58. numberInFirstEightChars(password, passwordType),
  59. letterInFirstEightChars(password, passwordType),
  60. doesNotContainInvalidCharacters(password, passwordType)
  61. ).mapN((_, _, _, _) => new Password(password))
  62.  
  63. private def passwordLongerThanEight(password: String, passwordType: PasswordType): ValidatedNel[String, Unit] =
  64. Validated.condNel[String, Unit](
  65. password.length >= 8,
  66. (),
  67. show"PasswordType: $passwordType - Password Not Longer Than 8 Characters"
  68. )
  69.  
  70. private def numberInFirstEightChars(password: String, passwordType: PasswordType): ValidatedNel[String, Unit] =
  71. Validated.condNel[String, Unit](
  72. password.take(8).matches(".*\\d+.*"),
  73. (),
  74. show"PasswordType: $passwordType - No Number in First 8 Characters"
  75. )
  76.  
  77. private def letterInFirstEightChars(password: String, passwordType: PasswordType): ValidatedNel[String, Unit] =
  78. Validated.condNel(
  79. password.take(8).matches(".*[a-zA-Z]+.*"),
  80. (),
  81. show"PasswordType: $passwordType - No Alpha Character in First 8 Characters"
  82. )
  83.  
  84. private def doesNotContainInvalidCharacters(s: String, passwordType: PasswordType): ValidatedNel[String, Unit] = (
  85. Validated.condNel[String, Unit](!s.contains("\""), (), show"""PasswordType: $passwordType - Contains Invalid Character: '"'"""),
  86. Validated.condNel[String, Unit](!s.contains("\'"), (), show"""PasswordType: $passwordType - Contains Invalid Character: "'" """),
  87. Validated.condNel[String, Unit](!s.contains(":"), (), show"""PasswordType: $passwordType - Contains Invalid Character: ':'""")
  88. ).mapN((_, _, _) => ())
  89.  
  90. case class ValidatedPasswordChangeRequest(username: Username, oldPassword: Password, newPassword: Password)
  91.  
  92. def validatePasswordChangeRequest(p: PasswordChangeRequest): ValidatedNel[String, ValidatedPasswordChangeRequest] =
  93. (
  94. validateUsername(p.username),
  95. validatePassword(p.oldPassword, PasswordType.OldPassword),
  96. validatePassword(p.newPassword, PasswordType.NewPassword)
  97. ).mapN(ValidatedPasswordChangeRequest)
  98.  
  99. }
Add Comment
Please, Sign In to add comment