Now that you have done some readings on validation, it's time to practice. In this activity, we will specifically utilize the express-validator package to validate a registration form. The objectives of this assignment are:
- Understanding how to use express-validator with express
- Practicing different validation types
- Practicing writing custom validations
- Open your terminal and navigate to your dedicated assignments folder.
- Then clone this assignment repo on your local machine.
- Now open the assignment folder on VSCode.
We already started the app.js
file for you. It includes:
- Initial setup for your Express server.
- A GET request to
/
which renders theindex.ejs
file we included in yourviews
folder. - A variable called
usedEmails
that will come into use for your validation rules.
NOTE: Please don't make any change to the usedEmails
variable in the code.
Let’s test our app out! Start the app by running npm start
. Now visit http://localhost:3000
on the browser, you should be able to see this:
Now that you have the starter code for this assignment, it's time to do some Googling and validate each user input in the registration form. We advise you to keep the express-validator documentation open while working on this activity.
- Create a POST request to the endpoint
/users
to handle user registration requests. - Connect the registration form to send the POST request with form data when submitted.
- Validate all user inputs within the POST request.
- Show alerts on the registration form in case of invalid inputs.
- Save emails of successful registrations as used.
Your POST request body must in the format:
{
username: "string",
email: "string",
password: "string",
confirmPassword: "string"
}
HINT: You can create all user input validations using a validation chain of the check()
function provided by express-validator
. In case the user request does not pass one or more validations, you will have to pass a variable to your EJS template and render specific alerts for the user. (We already know how to pass variables to EJS from the Meme Website activity).
The username should pass the following validation rules:
- Must be at least 4 characters long
- Should not include spaces
- Should not be empty
If a user request fails any of these validations, you will have to output specific alerts to inform the user. Your alert messages should be as follows:
- 'Username must be at least 4 characters long'
- 'Username should not include spaces'
- 'Username should not be empty'
NOTE: You may use the Bootstrap alert component to display validation alerts on the registration form.
The email should pass the following requirements:
- Should be unique (Here you will write a custom validation to check the
usedEmails
array) - Should be in valid email format
- Should not be empty
If a user request fails any of these validations, you will have to output specific alerts to inform the user. Your alert messages should be as follows:
- 'Email already exists'
- 'Invalid email'
- 'Email should not be empty'
The password should pass the following requirements:
- Must have at least 5 characters
- Must contain at least 1 number, 1 uppercase and 1 lowercase
- Should match with the confirm password input (Here you will use custom validation to compare the 2 password inputs)
- Should not be empty
If a user request fails any of these validations, you will have to output specific alerts to inform the user. Your alert messages should be as follows:
- 'Password must be at least 5 characters long'
- 'Password must contain a number, uppercase and lowercase'
- 'Passwords are not matching'
- 'Password should not be empty'
Phew, once the user request has finally passed all our validation, it's time for us to save the registered email.
- Using the request body, push the new email to our
usedEmails
array - Show a success alert to the user saying "Congratulations, your account has been successfully created!"
Run npm test
to test your code. If it shows all tests have passed then you're good to go.
Since this is a group assignment and you will be sharing the assignment repo with your group member, you can perform intermediate commits and pushes as required to share your code within your group.
Once you're ready to submit the assignment, follow these steps on your terminal:
- Stage your changes to be committed:
git add .
- Commit your final changes:
git commit -m "solve assignment"
- Push your commit to the main branch of your assignment repo:
git push origin main
After your changes are pushed, return to this assignment on Canvas for the final step of submission.
Now you have learnt how to validate user inputs and handle different types of errors. Validation helps us ensure that any mistakes made by a user does not affect the functionality of our system. We always inform the user about the issue so that they can correct it. Can you think of other scenarios besides user registration form where validation will be helpful?
Make sure to check out another famous validation package called Joi.