Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import Joi for validation.
Express
const Joi = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'joi' to import Joi.
Forgetting to install the 'joi' package before importing.
✗ Incorrect
Joi is imported using the package name 'joi' to use its validation features.
2fill in blank
mediumComplete the code to define a Joi schema for a required string 'username'.
Express
const schema = Joi.object({ username: Joi.[1]().required() }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Joi.number() for a text field.
Omitting the required() method for mandatory fields.
✗ Incorrect
Use Joi.string() to specify that 'username' must be a string.
3fill in blank
hardFix the error in validating the request body using Joi.
Express
const { error } = schema.[1](req.body); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'validateBody' or 'check'.
Not destructuring the 'error' from the validation result.
✗ Incorrect
The correct Joi method to validate data is 'validate'.
4fill in blank
hardFill both blanks to create a middleware that validates 'email' as a required string and calls next() if valid.
Express
const validateEmail = (req, res, next) => {
const schema = Joi.object({ email: Joi.[1]().[2]() });
const { error } = schema.validate(req.body);
if (error) return res.status(400).send(error.details[0].message);
next();
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Joi.number() for email.
Using optional() instead of required() for mandatory fields.
✗ Incorrect
Use Joi.string() to specify the type and required() to make it mandatory.
5fill in blank
hardFill all three blanks to define a Joi schema for 'password' with minimum length 8, required, and validate it.
Express
const schema = Joi.object({ password: Joi.[1]().min([2]).[3]() });
const { error } = schema.validate(req.body); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using number() instead of string() for password.
Omitting required() making the field optional.
Setting min() to a wrong value or forgetting it.
✗ Incorrect
Password should be a string, have a minimum length of 8, and be required.