PassValidator Kata

Improve our expertise on TDD by implementing the PasswordValidator kata.

Now, we’re going to implement a less academic exercise to reinforce our knowledge of TDD. The exercise is the Password Validation kata. Theoretically, this function could be invoked to validate the user’s password in a registration form. It will accept a password as the input, and it returns a bool value indicating whether the validation succeeded along with the reasons why the validation failed. A password is valid only if it meets all the following constraints:

  • It must be at least eight characters long.
  • It must contain at least two numbers.
  • It must contain at least one capital letter.
  • It must contain at least one special character.

In addition, the function must be able to manage multiple validation errors at the same time.

Password length requirement

The first feature we’re going to implement is only validating a password if it has at least eight characters. If the password meets this rule, we return true without any error. Otherwise, we’ll return false together with the error password must be at least 8 characters.

The Red phase

Let’s take a closer look at how we would enter the Red phase for this requirement:

Get hands-on with 1200+ tech skills courses.