Challenge 2: Error Messages Using Maybe Monad

Test your knowledge by writing code to convey error messages as an array using the Maybe monad.

Problem statement

In the previous lesson, we eliminated exceptions by rewriting the authenticateUser function as a two-track Either monad-based function.

This time, you have to rewrite the function featured there to utilize Maybe monad case analysis.

Requirements

By maintaining all the requirements stated in the previous challenge, you have to perform the following additional requirement in order to complete the challenge:

  • Convey each error message as an array with the error key by using the Maybe monad.

To reiterate, the requirements were:

  1. If the provided username is not valid, then the associative array returned by the function must be:

    error => Sorry, your username is invalid.

  2. If the provided password is not valid, the associative array returned by the function must be:

    error => Sorry, your password is invalid.

  3. If both—the provided username and password—are valid, then the function will perform the following tasks:

    • Add a new field with the name role in USER_DATA, whose value is either admin or customer depending on the value of the admin key (true for the admin and false for the customer).

    • Remove the password from the resultant data.

Note: Unlike the first two constraints, you are’nt required to return a custom string for this one. The test cases for this challenge will automatically verify whether these two tasks are performed on USER_DATA or not.

  • Actual username: kelly92
  • Actual password: trap_223

Sample inputs and outputs

Sample input 1

Username = "kellys92"
Password = "trap_223"

Expected output

[error] => Sorry, your username is invalid

Sample input 2

Username = "kelly92"
Password = "trap223"

Expected output

[error] => Sorry, your password is invalid

Note: The code from the solution tab of the previous challenge can be found hereUntitledConcept2.

Get hands-on with 1200+ tech skills courses.