0
0
Expressframework~10 mins

Joi as validation alternative in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import Joi for validation.

Express
const Joi = require('[1]');
Drag options to blanks, or click blank then click option'
Abody-parser
Bexpress
Cjoi
Dvalidator
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'joi' to import Joi.
Forgetting to install the 'joi' package before importing.
2fill in blank
medium

Complete 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'
Astring
Barray
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using Joi.number() for a text field.
Omitting the required() method for mandatory fields.
3fill in blank
hard

Fix 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'
AvalidateBody
Bcheck
Ctest
Dvalidate
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'validateBody' or 'check'.
Not destructuring the 'error' from the validation result.
4fill in blank
hard

Fill 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'
Astring
Brequired
Cnumber
Doptional
Attempts:
3 left
💡 Hint
Common Mistakes
Using Joi.number() for email.
Using optional() instead of required() for mandatory fields.
5fill in blank
hard

Fill 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'
Astring
B8
Crequired
Dnumber
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.